WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic Pagination using $count (7 posts)

  1. darrensa
    Member
    Posted 4 years ago #

    Hi,

    I've been battling with trying to exclude posts from a category on the Tanuka theme. Have finally got it working with this code:

    <?php if (have_posts()) :
    	if ( $is_top_single ) $GLOBALS['more'] = false; //important
    	while (have_posts()) : the_post(); if (in_category('6')) continue; ?>
    
        <?php static $count = 0;
    if ($count == "7") { break; }
    else { ?>

    ......etc, and then:

    <?php $count++; } ?>

    So basically it's removing posts from category 6, but then the counter is adding up posts which are being listed and then breaks after 7, so I can have a consistent amount of posts on the homepage.

    Only problem is that this breaks the pagination now, as I've set wordpress posts to show '30' to be safe (so all 7 from the categories (excluding cat 6) are show).

    Now my question is, is it possible to use pagination when count=="7", so that it paginates 7 posts per page, EXCLUDING category 6.

    Hope this makes sense...?

  2. MichaelH
    Member
    Posted 4 years ago #

    If you change

    <?php if (have_posts()) :

    to

    <?php query_posts($query_string . '&cat=-6'); ?>
    <?php if ( have_posts() ) :

    you could use the normal page navigation offered by template tag, previous_posts_link() and next_posts_link(). The WordPress Default theme's wp-content/themes/default/index.php has examples fo these two template tags.

  3. darrensa
    Member
    Posted 4 years ago #

    Hi Michael,

    Thanks for the reply. Unfortunately this does not work correctly with the "Tanzaku" theme I am using. I have tried everything and the above way seems like the only possible route...

  4. darrensa
    Member
    Posted 4 years ago #

    I guess the easiest way would perhaps be to edit the includes file (link-template.php?) to exclude cat6 from the wordpress 'reading options' count? I just have no idea where to start within that file...

  5. darrensa
    Member
    Posted 4 years ago #

    Another possible route which seems to work is to use the "Advanced Category Excluder", which excludes category 6 from everywhere. The only problem is that it's not possible to call manually now (as ACE overrides it....) Any know how to override the ACE settings with code to show cat6 in a separate div?

  6. darrensa
    Member
    Posted 4 years ago #

    Turns out ACE doesn't work with wordpress 2.9.1. I just want to exclude cat6 from EVERYTHING (search bars, archives, etc) and just have it show when called in the code....

    But using <?php query_posts($query_string . '&cat=-6'); ?> breaks the theme. Must be something to do with the information already inside the $query_string....?

  7. macelfresh
    Member
    Posted 4 years ago #

    Hi,

    I used something like this, to display hashes

    <?php
                    //will hold hashes of posts, by index in their cat
                    $hash = Array();
    		$args=array(
    				'orderby' => 'count'
    		);
                    $i = 0;
                    //will hold cat ID and count of posts in it
    		$nb_prj_cat = Array();
                    //find all categories
    		foreach((get_categories($args)) as $category) {
    			//desired cat
    			if(($category->cat_ID == 5) || ($category->cat_ID == 6)){
    //count posts in cat
    				$nb_post = $category->category_count;
    				//how many projects in cat
                                    $nb_prj_cat[$i] = array($category->cat_ID , $nb_post);
                                    //initiate hash
    				$hash[$i] = 0;
    				$i++;
    				//echo $nb_post;
    				//echo $category->cat_ID;
    			}
    		}
    <?php if (have_posts()) :  while (have_posts()) : the_post(); ?>
    
    			<?php
    //we read it for each post
    //retrieve categories of post
    			$cat = wp_get_post_categories($post->ID, $args);
                          //read category considering its amount of posts
                           for($i = 0; $i < count($nb_prj_cat[$i]); $i++){
    							//if cat of the post == one of the categories above
                                   if ($cat[0] == $nb_prj_cat[$i][0]){
    					$hash[$i]++;
                                            display html...

    I guess this works for a little amount of categories, and a bit abstract because of the 3 arrays, but maybe you can inspire from it somehow

Topic Closed

This topic has been closed to new replies.

About this Topic