WordPress.org

Ready to get started?Download WordPress

Forums

Loop with excluded category not working properly (7 posts)

  1. knoxfrombxl
    Member
    Posted 4 years ago #

    Hi all, this is my first post so please forgive me if it's not in the right place or if the answer has already been given (i've made some research but didn't find anything on it). Here we go !

    I'm currently working on my personal website. I'm a composer and my website will feature a work page as well as a blog. Works are posts in "Work" category and blog posts are in "Blog" category or a child of it.

    On index.php, archive.php, category.php, search.php, i use exactly the same code, but the results are not same ! Here is my (simplified) loop :

    <?php if (have_posts()) : ?>
      <?php while (have_posts()) : the_post(); ?>
        <?php if (in_category('work')) continue; ?>
        <?php the_title(); ?>
        <?php the_content(); ?>
        <?php previous_posts_link('&laquo; Newer Entries') ?>
        <?php next_posts_link('Older Entries &raquo;') ?>
      <?php else : ?>
        <p>Sorry, but you are looking for something that isn't here.</p>
        <?php include (TEMPLATEPATH . "/searchform.php"); ?>
    <?php endif; ?>

    The <?php if (in_category('work')) continue; ?> line is because i do not want works to show up on any page of the blog.

    On archive.php, category.php and search.php, everything works just fine. I have 10 blog posts showing with adequate previous posts link. (we're on page 1)
    On index.php (blog home), i only have 6 blog posts showing with previous posts link. The thing is i have 11 matching blog posts, so the page should show up 10 blog posts. The interresting fact is that within the 10 last posts (incuding works), there are ... 4 works.

    The loop is behaving as it was not showing work posts but count it anyway, which does not occur on other pages !

    Does someone have an idea on how to solve this ?
    Is this a bug ?

    Thank you very much !

  2. knoxfrombxl
    Member
    Posted 4 years ago #

    I've searched a bit more.
    This solution seems to work but i cannot use index.php for categories or search results, because of the query_posts() function always returning the 10 most recent posts.

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
            <?php query_posts("cat=-6&paged=$paged"); ?>
    		<?php if (have_posts()) : ?>
    			<?php while (have_posts()) : the_post(); ?>
    //etc

    Am i forced to create archive.php, category.php and search.php or a is_home() condition or do you have another idea ?

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    Use a filter to exclude the category unless directly requested?

    function singlecatout($query) {
    	$excludecatID = 57; // Change to suit ID
    	if ($query->is_category && $query->query_vars->cat !== $excludecatID || !$query->query_vars->cat)
    	{
    		$query->set('cat', '-'.$excludecatID.'');
    	}
    	return $query;
    }
    add_filter('pre_get_posts', 'singlecatout');

    The category is then excluded unless it has been directly queried, ie. A link was clicked to view that category...

  4. Shane G
    Member
    Posted 4 years ago #

  5. knoxfrombxl
    Member
    Posted 4 years ago #

    @ t31os : thanks for the trick ! In my case, i will never ever call that specific category in the blog (thus in index.php), i don't need to filter.

    @ Shane : i tried this, it works on every page *but* index.php, don't ask me why. This is why i had to go with query_posts().

    Solution in my case is to use query_posts()on index.php and create all other templates within my theme (archive, search, category.php) with the original loop.

    Thanks for help !

  6. knoxfrombxl
    Member
    Posted 4 years ago #

    @ t31os : your code works like a charm in the blog but messes up everything on the "work" page which only call works. But thanks for the tip, i'll be using it on another project :)

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Happy to help in any case.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic