• On our wordpress website we’ve got a template that didn’t come with the category section fully working – when you click a category it shows all posts regardless of the category. I’ve had a crack at it and have got all posts appearing for the category but with no pagination, which is required.

    Basically I’ve got all posts going through an IF statement, and only being shown if they are in the selected category. Ones that are not in the category just don’t echo anything out.

    I want to be able to show only 5 at a time as is the usual thing to do on our websites. I imagine it might be something to do with the wp_query bit. Below is what I’ve got for that:

    $wp_query = new WP_Query(); $wp_query->query(‘showposts=’.$totalposts . ‘&paged=’.$paged);

    $totalposts is the amount of published posts. The full list of variables I’ve got defined is below.

    • Total amount of published posts
    • Selected Category name
    • Selected Category ID
    • The post’s associated category ID
    • An incrementor counting the posts on the page as they are echoed out (counting only ones in the category

    Any help is greatly appreciated, thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter jimleeder

    (@jimleeder)

    Update – the code below shows posts from the category and how many it should show as I specified, but the pagination doesn’t work. It goes to /page2/ which is blank.

    <?php

    /**
    * Template Name: Archive
    *
    */

    get_header();?>

    <!–blogcontent–>
    <section class=”strapline”>
    <div class=”container12 heading”>
    <div class=”column12 heads”>
    <h1>Category: <?php single_cat_title( ”, true ); ?></h1>
    </div>
    </div>
    </section>

    <div class=”container12″>
    <div class=”column8 news”>
    <?php

    // The current selected category defined in $maincat
    $maincat = get_category(get_query_var(‘cat’))->name;
    $catid = get_cat_ID($maincat); //the selected category ID

    $countallposts = wp_count_posts(); //counts all published posts
    $totalposts = $countallposts->publish; //result of all published posts

    $i = query_posts(‘category_name=”‘.$maincat.'”‘);
    $noofposts = count($i);

    $postcatcount = 0; //count the posts with the selected category

    $temp = $wp_query; $wp_query= null;
    $wp_query = new WP_Query(); $wp_query->query(‘showposts=5’ . ‘&paged=’.$paged);

    $args = ‘cat=’ . $catid . ‘&posts_per_page=3’; //arguments – does the category and amount per page

    query_posts($args); //shows only posts in selected category

    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <?php $cats = get_the_category();
    $postcat = $cats[0]->name;
    // category name for individual post defined in $postcat

    ++$postcatcount; //plus one to the amount of posts in the selected category

    ?>

    <h2>” title=”Read more”><?php the_title(); ?></h2>
    <?php the_excerpt(); ?> <?php echo “COUNT: “; echo $postcatcount; ?>
    <hr>

    <?php endwhile; ?>

    <?php if ($paged > 1) { ?>

    <nav id=”nav-posts”>
    <div class=”row”>
    <div class=”column4 alpha prev”><?php // next_posts_link(); ?> <?php echo paginate_links( $args ); ?></div>
    <div class=”column4 omega next”><?php // previous_posts_link(); ?></div>
    <?php echo “Yes theres more than 5 – Count total: “; echo $postcatcount; echo ” Paged data = “; echo $paged; ?>
    </div>
    </nav>

    <?php }else{ ?>

    <nav id=”nav-posts”>
    <div class=”row”>
    <div class=”prev”><?php // next_posts_link(‘« Previous Posts’); ?> <?php echo paginate_links( $args ); ?></div>
    <?php echo “No theres less than 5 – Count total: “; echo $postcatcount; echo ” Paged data = “; echo $paged; ?>
    </div>
    </nav>

    <?php } ?>

    <?php wp_reset_postdata(); ?>

    </div>

    <div class=”column4″><?php dynamic_sidebar(‘primary-sidebar’); ?></div>

    </div>
    <!–blogcontent end–>

    <?php get_footer(); ?>

    Thread Starter jimleeder

    (@jimleeder)

    Have got it working 99%. Correct posts and pagination works using the code pasted below. Goes inbetween the $wp_query bit and the while loop.

    global $query_string; //this function solves the category error of showing all posts
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string.'&paged='.$paged.'&posts_per_page=5');

    Only thing is when you click onto the last page of results, if it has less than 5 pages, they don’t show. So if a category has 17 posts, the last 2 posts won’t show on the final page. Instead it keeps you on the penultimate page of results.

    Thread Starter jimleeder

    (@jimleeder)

    Have sorted that bit out, but have also discovered that if a a category has 2 pages of results (5 per page, so between 6 – 10 posts in category) then it doesn’t go back to the 2nd page, when you click the link it stays on page 1.

    However with any higher number of pages it goes through them all ok.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Archive.php – Show only posts from user's selected category’ is closed to new replies.