Jump to content

Wordpress - archive.php show only posts from user's selected category


jimleeder123

Recommended Posts

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.


Link to comment
Share on other sites

Below is my code. Showing posts from the category and the amount of posts specified but the pagination links are not working. It goes to /page2/ but it is a blank page when there should be some posts there. Its archive.php by the way.

<?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><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></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(); ?>
Edited by jimleeder123
Link to comment
Share on other sites

Got it working. Found a bit of code on the Wordpress forum that goes after the $wp_query bit but before the while loop.

 

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.
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');
Edited by jimleeder123
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.