Add archive/category results to single post page

The question:

Have posts, want related categories to display under them.

Post example: Artist name.

Category example: posts with the Artist name.

My current version, arrived at based on a comment from @birgire, adds a WP_Query block under the single.php page template. It currently returns all the posts categorized to the artist, but returns them as full posts instead of the default post thumbnail above an excerpt.

<?php
    $args = array(
        'post_type' => 'pdsh_posts',
        'posts_per_page' => -1,
        'orderby' => 'title',
        'order'   => 'ASC',
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => $post->post_name,
            ),
        ),
    );
    $my_query = new WP_Query( $args );
    while ( $my_query->have_posts() ) : $my_query->the_post();
        //the_excerpt();
        get_template_part( 'template-parts/content', get_post_format() );
    endwhile; 
?>

Wondering if this a good solution easily completed with css or am I missing some fundamental step?

This mockup shows what I’m going for:

Add archive/category results to single post page

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Figured it out. A conditional in content.php in template-parts/ checked whether the content is_archive. I assume the functionality behind is_archive is whether the file was launched from archive.php. Since it wasn’t it didn’t fire.

Final step was redirecting to a custom content-post-type.php in template-parts that doesn’t have the is_archive check. Seems like it all works right now. Thanks to birgire for the nudge.

So my final single.php goes:

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

        get_template_part( 'template-parts/content', get_post_format() );

            the_post_navigation();

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) :
            comments_template();
        endif;

    endwhile; // End of the loop.
    ?>
        
    <!--ADD AN ARCHIVE FOR php echo $post->post_name -->
        
    <?php 
        $page_title = $wp_query->post->post_title;
        $args = array(
            'post_type' => 'pdsh_posts',
            'posts_per_page' => -1,
            'orderby' => 'title',
            'order'   => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field'    => 'slug',
                    'terms'    => $page_title,
                    //'terms'    => $post->post_name,//
                ),
            ),
        );
        $my_query = new WP_Query( $args );
        while ( $my_query->have_posts() ) : $my_query->the_post();
            //the_excerpt();
            get_template_part( 'template-parts/content-pdsh', get_post_format() );
        endwhile; 
    ?>

Where “pdsh_posts” is a custom post type.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment