0

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

  • attachments are a type of post, you query for them with WP_Query or get_posts – Milo Jun 1 '18 at 17:03
  • Hello Milo, Thanks for your reply and help. When I use this code, it shows me all attachements..and it's logical : <?php $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $post ) { echo the_attachment_link( $post->ID, false ); } } ?> But as soon as I try to sort them out using tax_query, it is not showing anything at all.. drives me mad ! – Francois Jun 4 '18 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.