• Resolved KrizzyB

    (@krizzyb)


    Good Day!

    I’m currently building a new WordPress website and I’m having an issue with each individual post.

    What I would like to happen is in each post, the archive of the other posts in the same category are listed in the sidebar.

    What actually happens is the post that is currently open is the only one that appears and then the loop seems to stop. My method or attempting to make this work has been to basically copy the PHP code from the Category Template and paste it into the Sidebar.php.

    It does appear to work I just don;t know how to ensure it displays the entire category archive and not just the one post.

    Any suggestions?

    Pages involved:

    One of the sample posts: https://krizzyb.com/video/?p=1
    The category archive containing the 2 samples posts: https://krizzyb.com/video/?cat=3

    Thanks for reading.

Viewing 12 replies - 1 through 12 (of 12 total)
  • You probably need to add a call to query_posts() to the code you copied into sidebar.php. I suspect that it needs to add the ‘posts_per_page’ argument set to the number of entries you want to show. The code should be similar to this:

    <?php
    global $wp_query;
    query_posts(
       array_merge(
          $wp_query->query,
          array('posts_per_page' => 5)
       )
    );
    ?>

    That code would go just before the if (have_posts()) call that usually starts the loop.

    Thread Starter KrizzyB

    (@krizzyb)

    Currently my ‘sidebar.php’ contains:

    <?php
    $category_description = category_description();
    if ( ! empty( $category_description ) )
    echo '<div class="archive-meta">' . $category_description . '</div>';
    
    /* Run the loop for the category page to output the posts.
    * If you want to overload this in a child theme then include a file
    * called loop-category.php and that will be used instead.
    */
    get_template_part( 'loop', 'category' );
    ?>

    and the loop takes place in ‘loop-category.php’

    I think what is happening is, because I’m calling the same loop as the category page but inside of a post it can only see that post and does not output the rest. However I have no idea how to instruct the loop to call the entire category from inside a post.

    Although I may be entirely wrong, I’m currently clueless.

    You are on the right track. Copy loop-category.php to loop-my-category.php and add the query_posts() call there. Then change the sidebar.php to get_template_part('loop, 'my-category' );

    Thread Starter KrizzyB

    (@krizzyb)

    Unfortunately it’s still only showing the current post. I can’t think what could be going wrong.

    It is almost impossible to guess what is wrong. Please put the code for loop-my-category.php in a pastebin and post a link to it here.

    Thread Starter KrizzyB

    (@krizzyb)

    Since I can’t test the code, this is just a guess, but try changing the query to this:

    <?php
    $cat = get_query_var('cat');
    query_posts(
       array(
          'posts_per_page' => 5,
          'cat' => $cat,
       )
    );
    ?>
    Thread Starter KrizzyB

    (@krizzyb)

    Ahh-ha!

    It appears to have worked good sir.

    Except now it appears to be showing both full posts on the page too… I can’t help but be completely confused again =P

    https://krizzyb.com/video/?p=1

    Then you will need to save the old query and restore it at the end. Change the query to this:

    <?php
    global $wp_query;
    $save_query = $wp_query;
    $cat = get_query_var('cat');
    query_posts(
       array(
          'posts_per_page' => 5,
          'cat' => $cat,
       )
    );
    ?>

    and add this as the last line:

    <?php $wp_query = $save_query; ?>

    Thread Starter KrizzyB

    (@krizzyb)

    Done that, but still seems to be messing up. Could it be a problem elsewhere?

    Also the comments section disappears, if that’s a clue to where the problem may lie.

    The only other thing I can suggest is that you change the sidebar query to use WP_Query, but that involves changes in several places.

    Change the query to this:

    <?php
    $cat = get_query_var('cat');
    $my_query = new WP_Query(
       array(
          'posts_per_page' => 5,
          'cat' => $cat,
       )
    );
    ?>

    Then,

    • change all have_posts() to $my_query->have_posts()
    • change all the_post() to $my_query->the_post()
    Thread Starter KrizzyB

    (@krizzyb)

    YOU SIR ARE A GOD!

    That appears to have solved it =P

    Thank you very much for your time and knowledge =)

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Category Archive List in Post?’ is closed to new replies.