register_taxonomy_for_object_type (images) : how to use it ?

I’m struggling and I need help. Perhaps I am not on the right path to achieve what I want.

I have a custom taxonomy called ‘sources’. That is used to link my articles to its its original source.

I intend to create a page for each single source (taxonomy term) giving a description, website link, logo as well as articles taken from that source underneath.

I used a function to link attachment (image in my case) to my custom taxonomy.

My single source page (or taxonomy term page) works fine. Here is the code :

<?php if ( have_posts() ) : ?>

<header class=”page-header”>
<div class=”sources”>
<h4>
<?php echo strip_tags( get_the_term_list($post->ID, ‘sources’ ) ); ?>
</h4>
<?php
$term_description = term_description();
if ( ! empty( $term_description ) ) :
printf( ‘<div class=”taxonomy-description”>%s</div>’, $term_description );
endif;
?>
/div>

<?php while ( have_posts() ) : the_post(); ?>

<div class=”sources-image”>
<?php
$image = wp_get_attachment_image($attachment->ID, ”, false, $attr);
if ( ! empty ($image) ) :
echo wp_get_attachment_image($attachment->ID, ‘sources-thumb’, false, $attr);
endif;
?>

<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

</header><!– .page-header –>

<div class=”posts-layout”>

<?php
$tax_term_details = get_queried_object();
$tax_term_ID = $tax_term_details->term_id;
$tax_term_slug = $tax_term_details->slug;

$args = array(
‘post_type’ => ‘post’, // only post, no attachment
‘tax_query’ => array(
array(
‘taxonomy’ => ‘sources’,
‘field’ => ‘slug’,
‘terms’ => $tax_term_slug,
)
)
);
$query = new WP_Query( $args );
?>

<?php while ( $query-> have_posts() ) : $query->the_post(); ?>

<?php
get_template_part( ‘template-parts/content’, ‘sources’ );
?>

<?php endwhile; ?>
</div>

<?php the_posts_navigation(); ?>

<?php else : ?>

<?php get_template_part( ‘template-parts/content’, ‘none’ ); ?>

<?php endif; ?>

</main><!– #main –>
</div><!– #primary –>

My sources page

I have a problem to display all the ‘sources’ (all taxonomy terms) with their image. No problem to display the terms but I cannot get the images.

<?php

$args = array(
‘post_status’ => ‘inherit’,
‘posts_per_page’ => -1,
‘post_type’ => ‘attachment’,
);

$sources = get_terms( ‘sources’, $args );

?>

I tried with wp_get_attachment_image but it is not working as I’am not on a term page.

Anyone knows how to do this?

Please don’t tell me to use a plugin at this stage. It’s also for learning purposes so any explanations would help me improve 🙂

Thanks a lot

Read more here:: register_taxonomy_for_object_type (images) : how to use it ?

Leave a Reply

Your email address will not be published. Required fields are marked *