Forums

[resolved] Category Archive List in Post? (13 posts)

  1. KrizzyB
    Member
    Posted 7 months ago #

    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: http://krizzyb.com/video/?p=1
    The category archive containing the 2 samples posts: http://krizzyb.com/video/?cat=3

    Thanks for reading.

  2. vtxyzzy
    Member
    Posted 7 months ago #

    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.

  3. KrizzyB
    Member
    Posted 7 months ago #

    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.

  4. vtxyzzy
    Member
    Posted 7 months ago #

    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' );

  5. KrizzyB
    Member
    Posted 7 months ago #

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

  6. vtxyzzy
    Member
    Posted 7 months ago #

    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.

  7. KrizzyB
    Member
    Posted 7 months ago #

    Here you go sir:

    http://pastebin.com/MJ3S3P7K

  8. vtxyzzy
    Member
    Posted 7 months ago #

    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,
       )
    );
    ?>
  9. KrizzyB
    Member
    Posted 7 months ago #

    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

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

  10. vtxyzzy
    Member
    Posted 7 months ago #

    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; ?>

  11. KrizzyB
    Member
    Posted 7 months ago #

    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.

  12. vtxyzzy
    Member
    Posted 7 months ago #

    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()
  13. KrizzyB
    Member
    Posted 7 months ago #

    YOU SIR ARE A GOD!

    That appears to have solved it =P

    Thank you very much for your time and knowledge =)

Reply

You must log in to post.

About this Topic