WordPress.org

Forums

[resolved] Sidebar tag check sees only tags from first 9 posts (3 posts)

  1. edow
    Member
    Posted 1 year ago #

    Hi,

    I use the following code in my category sidebar to check which tags are used in the category. It works, but only for the first 9 posts (the first page displays 9 posts.) How can I change this code so it also checks for the other posts in this category?

    <?php
    
    		if (is_category()){
    
    		  $cat = get_query_var('cat');
    
    		  $yourcat = get_category ($cat);
    
    		}
    
    		$tag_IDs = array();
    
    		query_posts('category_name='.$yourcat->slug);
    
    		if (have_posts()) : while (have_posts()) : the_post();
    
    		  $posttags = get_the_tags();
    
    		  if ($posttags):
    
    			foreach($posttags as $tag) {
    
    			if (!in_array($tag->term_id , $tag_IDs)):
    
    			   $tag_IDs[] = $tag->term_id; 
    
    			   $tag_names[$tag->term_id] = $tag->name;
    
    			   $tag_slug[$tag->term_id] = $tag->slug;
    
    			 endif;
    
    			}
    
    		  endif;
    
    		endwhile; endif;
    
    		wp_reset_query();
    
    		$getTag = $_GET["tag"];
    
    		if ($getTag == '') { 
    
    			$tag_showall = 'checked';
    
    			}
    
    		if (!empty($tag_IDs)){
    
    			echo '<h3>Aanbieders</h3>';
    
    			echo '<input type="radio" name="tag" value="" '.$tag_showall.'> Alles weergeven<br>';
    
    		}
    
    		foreach($tag_IDs as $tag_ID){
    
    			$checked = $tag_slug[$tag_ID];
    
    			echo '<input type="radio" name="tag" value="'.$checked.'"' ;
    
    			if((isset($_GET["tag"])) && $_GET["tag"] == $checked) {
    
    			echo ' checked="checked"';
    
    			}
    
    			echo '> '.$tag_names[$tag_ID].'<br>';
    
    		}
    
    		?>
  2. edow
    Member
    Posted 1 year ago #

    When I change this:
    query_posts('category_name='.$yourcat->slug);

    to this:
    query_posts('category_name='.$yourcat->slug.'&paged='.$paged.'');

    and I go to page 2 then it shows the tags from the post from page 2 (which it didn't before) so that is a beginning.

    But then it leaves me with the following problems:

    1. It still doesn't show all the tags from the whole category, but only from the current page
    2. When I'm on page 2 and click on a results it redirects to page 1 (that's what I want), but then it doesn't show the tag in the sidebar, because it's not page 2 anymore
  3. edow
    Member
    Posted 1 year ago #

    I think I got it. I changed the line to:
    query_posts('category_name='.$yourcat->slug.'&paged='.$paged.'&posts_per_page=-1');

    and now it works. I tried posts_per_page=-1 before without luck, but it seems that it works in combination with &paged='.$paged.'.

    Maybe someone can verify that this code is safe to use. Or maybe someone have a better solution.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags