WordPress.org

Forums

Exclude posts from main Loop (14 posts)

  1. karl19
    Member
    Posted 9 years ago #

    Hi there,

    I've searched the forum and while I've found this question repeatedly, I've not found it answered in a way that helps my particular situation.

    I've built a site for a journalist, where he has a number of categories to which he adds his published writing, e.g. travel, environment, culture. On the front page there is a loop of the three most recent publications. Now, he would like to exclude a couple of the posts - essentially when he feels the latest publication isn't strong enough to go on the front page, he doesn't want it listed in that loop of three-most-recent, but still wants to add it to the site/archive.

    Since each post is published in one or more categories based on topic, I can't simply exclude a category from being listed on the front page. One round-about way of doing it would be to create a new category - frontpage - and add the preferred three posts to it, then only show this category on the frontpage, but not in the archive. I could then show the three latest posts from this category.. hm, thinking aloud, that might work?

    Ideally, a way to have a checkbox saying "Show on frontpage" would be better I think, since it would exclude having to create more categories, but I've searched for such a plugin and can't find any.

    Well, I will try the idea I described above, but will still post this query in case I forget how to do it and end up searching the forum again (!), or in case someone else has the same question. Or if someone knows of a better solution!

    The site in question is http://www.michaellevitin.com/

  2. The frontpage category idea is a good one. I'd go that way.

    The plugin you're looking for to do that is here:
    http://ryowebsite.com/?p=46

  3. karl19
    Member
    Posted 9 years ago #

    Thanks for the link Otto42! I'd come across it before, but somehow it didn't seem to click that I could use it. It would probably have worked, but I ended up using manual exclusion based on Cat-ID number instead. Hence, in my sidebar and on my archive page, I added the exclude call.

    <?php wp_list_categories('title_li=&exclude=9'); ?>

    Then on the front page, I edited the querypost to only show that specific category.

    <?php if (is_home()) { query_posts('showposts=3&cat=9'); } ?>

    This works quite well and solved the problem I had above! The only one remaining issue is that now that I have a new category (Frontpage) that I add a post to, this also gets listed in the "Posted in.." section under the post. I've tried to exclude it, but don't quite know how. Not sure a category can be excluded from the the_category output?

    <?php the_category(', ') ?>

    If I could limit the output to not show this specific category in the list, that would solve the last problem!

  4. You can use get_the_category_list(', ') to get that comma separated list, which you can then use string functions to edit and such before echoing it out.

  5. karl19
    Member
    Posted 9 years ago #

    Many thanks again, very kind that you took the time to reply! Have done a trawling of Google for related posts with get_the_category_list, but must admit I'm stuck. Unfortunately I don't know string functions well enough to exclude part of the output from the array. They mention it here http://www.afroginthevalley.com/2006/10/23/wordpress-question-how-not-to-display-a-specific-category-name.html but can't seem to wield it into useable form on my site. If you've got an example how I would exclude a category by number or name (9 or Frontpage, for example) by using string functions, I would be eager ears!

    Karl

  6. Errr... maybe something like this?

    <?php
    echo str_replace('Frontpage ,','',get_the_category_list(', '));
    ?>
  7. karl19
    Member
    Posted 9 years ago #

    Otto42, thanks again!

    The code didn't quite work at first, but it was just a matter of removing the space and comma after Frontpage. I'm not sure if there's a way to remove the last comma in the category list (now it's outputting one after the last one too, since I presume it's expecting Frontpage to appear), but that's a small issue and I've added a dot after each category instead, which is a working compromise.

    Again, many thanks for the help and good luck with your work (and your new home - saw on your site that you've just moved).

  8. Try this instead...

    <?php
    $list = get_the_category_list(', ');
    $list = str_replace(', Frontpage','',$list);
    $list = str_replace('Frontpage, ','',$list);
    $list = str_replace('Frontpage','',$list);
    echo $list;
    ?>

    That should take care of all the possibilities, I think.

  9. karl19
    Member
    Posted 9 years ago #

    Thanks, now I'm starting to figure out how string replacements work too.. well, in theory. To me the examples above should work - in my case it would be the first replace that would do the trick, since the Frontpage category is coming last, but it doesn't seem to work. Checking the source code, this

    $list = get_the_category_list(', ');

    was outputting double blanks after the comma, but getting rid of that didn't help either. Testing version two (= not the last category) with a category which gets printed first, doesn't seem to work either. The last replace works, but outputs the last comma - in essence I guess it's the same as the previous echo statement..

  10. DSGJohnDoe
    Member
    Posted 8 years ago #

    Hi, I have the same problem and for exclude from categories listing in post I search for the function that makes it and as each category is stored in an array you only have to delete the categori index in that array.

    Mine is the cateogory 17 so...

    I add :

    //my hack
    	unset($categories[17]);

    the full view from the file wp-includes/category-template.php with the hack is :

    function get_the_category($id = false) {
    global $post, $category_cache, $blog_id;
    
    	$id = (int) $id;
    	if ( !$id )
    		$id = (int) $post->ID;
    
    	if ( !isset($category_cache[$blog_id][$id]) )
    		update_post_category_cache($id);
    
    	$categories = $category_cache[$blog_id][$id];
    
    	//my hack
    	unset($categories[17]);	
    
    	if ( !empty($categories) )
    		usort($categories, '_get_the_category_usort');
    	else
    		$categories = array();
    
    	return $categories;
    }
  11. Paul Menard
    Member
    Posted 8 years ago #

    I'd like to throw in my solutions. It simple and less code.

    In your theme folder create a functions.php is one does not exist. In that file place the following:

    function myBlogPostsFilter($query)
    {
    global $wp_query;

    // I just want to work with searches not all global queries.
    if ($query->is_search)
    {
    // The line below sets/unsets the categories to search
    // To exclude a category just provide the cat_id with
    // a preceeding '-'.
    $query->set('cat','-3,-4,-5,-6,-7');
    }
    return $query;
    }
    add_filter('pre_get_posts','myBlogPostsFilter');

  12. nathanielstern
    Member
    Posted 8 years ago #

    the unset($categories[17]); solution that DSGJohnDoe posted seems to be the easiest, but is there a way to do it without chaning the core code - either as a plugin or in the functions.php file?

  13. nathanielstern
    Member
    Posted 8 years ago #

    am gonna start a new thread, as this has the wrong topic and i wont be able to say it's "resolved" when it is....

  14. nathanielstern
    Member
    Posted 8 years ago #

    also see cheap workaround in this post

Topic Closed

This topic has been closed to new replies.

About this Topic