0

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:

enter image description here

7
  • Maybe this is where the "Multiple Loops Example" sections of codex.wordpress.org/The_Loop might be used...
    – markw
    Jun 4 at 19:36
  • Concatenating the string "category-" with the title "c-c-beck" to populate this line... <?php query_posts( 'category_name=category-c-c-beck' ); ?> ?
    – markw
    Jun 4 at 19:44
  • Tried just <?php query_posts( 'category_name=category-c-c-beck&posts_per_page=10' ); ?> followed by the loop from archive.php but got "Nothing Found. It seems we can’t find what you’re looking for." returned.
    – markw
    Jun 5 at 5:20
  • Dont use query_posts because of its side effects. Look into WP_Query for secondary loops. Alternative approach is using js+rest api.
    – birgire
    Jun 5 at 10:32
  • Oh cool. This kinda worked... <?php $args = array( 'post_type' => 'pdsh_posts', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'c-c-beck', ), ), ); $my_query = new WP_Query( $args ); while ( $my_query->have_posts() ) : $my_query->the_post(); get_template_part( 'template-parts/content', get_post_format() ); endwhile; ?>
    – markw
    Jun 5 at 18:16
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.

2
  • Actually, this is not yet a solution. It works for existing posts but doesn't for new posts. New posts are confused about where the archives are coming from. It puts the artist post under the artist post, instead of posts about the characters associated with the artist under the artist post.
    – markw
    Jun 6 at 23:13
  • My mistake. It still seems to work. The problem lay with category slugs. Some new posts for creators had a "-2" in its slug because new categories named after the new artist did not have a special suffix in the slug to differentiate them.
    – markw
    Jun 6 at 23:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.