Forums

[resolved] Exclude posts from a certain category from beign shown while... (9 posts)

  1. mabufo
    Member
    Posted 2 years ago #

    hi guys. My blog is laid out so there are two columns on the main page (http://www.theangrywalrus.com/blog). What I would like to do is to edit the theme's index page so that on the left hand side, posts from a certain category are excluded from being shown - and then on the right side allow those excluded posts to be shown with a little bit of a change in format - perhaps displaying up to 5 or so in a column (but that's for later). If you don't understand what I'm saying, try clicking the link to get a mental picture.

    Okay, the index page is here:

    <?php get_header(); ?>
    
    	<div id="primary" class="twocol-stories">
    		<div class="inside">
    			<?php
    				// Here is the call to only make two posts show up on the homepage REGARDLESS of your options in the control panel
    				query_posts('showposts=2');
    			?>
    			<?php if (have_posts()) : ?>
    				<?php $first = true; ?>
    				<?php while (have_posts()) : the_post(); ?>
    					<div class="story<?php if($first == true) echo " first" ?>">
    						<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
                <div class="details">
                  Posted by <?php the_author_posts_link() ?> on <?php the_time('M d Y');?> |
    				<?php the_category(', ') ?> <?php edit_post_link('Edit this', '', ''); ?>
                </div>
                <?php the_excerpt() ?>
                 <div class="details">
                  <?php comments_popup_link('no comments', '1 comment', '% comments'); ?> for now | <span class="read-on"><a href="<?php the_permalink() ?>">read on</a></span>
    						</div>
    					</div>
    					<?php if($first==true) { $first= false;} else {echo '<div class="clear"></div>';$first= true; } ?>
          <?php endwhile; ?>
    
    			<?php else : ?>
    
    				<h2 class="center">Not Found</h2>
    				<p class="center">Sorry, but you are looking for something that isn't here.</p>
    				<?php include (TEMPLATEPATH . "/searchform.php"); ?>
    
    			<?php endif; ?>
    
    			<div class="clear"></div>
     		</div>
    	</div>
    	<!-- [END] #primary -->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    I can see plainly that there is a loop that displays all the posts possible - so I would assume that I need to ditch the loop first. From there though, I don't really know how exactly to exclude posts based on category from being displayed. Any help would be nice.

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    I've marked your duplicate topic with a modlook tag, so ignore that one.
    http://wordpress.org/support/topic/292366?replies=1

    Onto your question, see here.
    http://codex.wordpress.org/Template_Tags/query_posts#Exclude_Categories_From_Your_Home_Page

  3. mabufo
    Member
    Posted 2 years ago #

    Sorry, I must have clicked submit more than once - I didn't mean to do that.

    In the research that I did - I actually stumbled across that page - but I also found one about setting up a second loop to do something like this at this page: http://codex.wordpress.org/The_Loop . So I figured I would try it and see how far I got.

    I set up a test blog that currently has 4 test posts. Two of the are in the category I want excluded (called 'special'), and two of them are categorized, and thus should be shown. The special posts are the most recent.

    as far as I can see the second loop that displays the special posts works just fine - you know, because it displays them. But the first loop that displays the posts that are not in the special category is not displaying any posts. I only want 1 non special post to be displayed next to the 5 special posts.

    Here is the loop code I'm looking at. I think the problem sits with the wrapper query function's arguments. I don't know exactly how it works but I think that because show posts is set to 1 - it only looks at the post that was most recently added - and because the most recent post is not special (see the negation in the argument list) - it doesn't put anything into the posts array - but I'm not really sure if that's what's happening or how to fix it. Here's that code:

    <?php
    				// Here is the call to only make two posts show up on the homepage REGARDLESS of your options in the control panel
    				//THIS IS SET TO ONE DUE TO THE NEW STYLE EDIT AND THE ADDITION OF A SECOND LOOP///////
    
    				$special_cat = get_cat_ID("special"); // gets the ID of the category we want to make 'special'
    				$negated_special_cat = $special_cat * -1;
    				query_posts('showposts=1', "cat=$negated_special_cat"); //called with the negated ID to exclude those posts
    			?>

    Now here is the first loop making use of the above query:

    <?php if (have_posts()) : ?>
    				<?php $first = true; ?>
    
    				<?php while (have_posts()) : the_post(); ?>
    				<?php if (in_category($special_cat) && is_home() ) continue;  //we have to exclude the special category from the left?> 
    
    					<div class="story<?php if($first == true) echo " first" ?>">
    						<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    						<div class="details">
    						Posted by <?php the_author_posts_link() ?> on <?php the_time('M d Y');?> |
    						<?php the_category(', ') ?> <?php edit_post_link('Edit this', '', ''); ?>
    						</div>
    						<?php the_excerpt() ?>
    						<div class="details">
    						<?php comments_popup_link('no comments', '1 comment', '% comments'); ?> for now | <span class="read-on"><a href="<?php the_permalink() ?>">read on</a></span>
    						</div>
    					</div>
    					<?php if($first==true) { $first= false;} else {echo '<div class="clear"></div>';$first= true; } ?>
    
    				<?php endwhile;//end of loop ?>
    
    ...
    ...
    ...
    
    <?php else : ?>

    And here is the second loop that actually is displaying posts - but if my assumption is correct this one is going to have the same problem as the first if the newest 5 posts aren't in the right category.

    <!--BEGIN SECOND LOOP (QUERY POSTS IN SPECIAL CATEGORY ONLY!!!!)-->
    				<?php $my_query = new WP_Query('category_name=special&showposts=5'); //makes a new query object because we can't use the old one ?>
    				<div class="story<?php if($first == true) echo " first" ?>">
    				<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    						<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    						<div class="details">
    						Posted by <?php the_author_posts_link() ?> on <?php the_time('M d Y');?> |
    						<?php the_category(', ') ?> <?php edit_post_link('Edit this', '', ''); ?>
    						</div>
    						<?php the_excerpt() ?>
    						<div class="details">
    						<?php comments_popup_link('no comments', '1 comment', '% comments'); ?> for now | <span class="read-on"><a href="<?php the_permalink() ?>">read on</a></span>
    						</div>
    
    				<?php endwhile;//end of loop ?>

    is my assumption about the problem correct? If it is, how do I fix my query call above the first loop so it grabs the newest post that isn't in the special category? And likewise, how do I grab the 5 newest posts in the special category? If that actually is not the problem like I think it is, how do I fix the first loop so it actually displays a non special post?

    Thank you guys.

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    Can you just paste the contents of the whole file in pastebin.
    http://wordpress.pastebin.com/

    That way i can see your template markup... and all accompanying code..

  5. mabufo
    Member
    Posted 2 years ago #

    Of course: http://wordpress.pastebin.com/m49c3d3e8

    That is the index.php file.

  6. mabufo
    Member
    Posted 2 years ago #

    As I suspected, the problem lies with the initial query_posts() call: query_posts('showposts=1', "cat=$negated_special_cat");

    Because showposts is only set to 1, it only looks at the most recent post - which is in my special category - so it skips over it and ends up not returning anything (or something to that effect). Increasing the showpost amount seems a tad hackish - but is there a way I can change that call so it works properly? Perhaps if I set the control panel setting on how many posts to display, and removed the showpost parameter? Because i only want it to show ONE non special post.

    EDIT: okay, I removed the showposts=1 from the param list of the query wrapper function, and changed the control setting to show one post, and it worked!

    My question now is: will this new query assignment return the 5 most recent special posts - or will it return whatever posts happen to be in the special category from the 5 most recent posts?
    <?php $my_query = new WP_Query('category_name=special&showposts=5'); ?>

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    Not sure where you got your previous code from, but give this a try.

    http://wordpress.pastebin.com/f7a517d60

    You don't need any extra queries.

    Left column excludes special cat and grabs 10 posts (set to your desired amount). NOTE: If one isn't set (remove the "showposts" bit) then the amount is determined by the limit set via the admin area.

    Right column grabs 5 posts from special cat (set to desired amount).

    I've tested this locally on WP 2.8.2 with the default theme. Works as i would expect, but please let me know if any problems... :)

    p.s. i can add comments back into the code if you like having them there, it was quicker for me to fiddle when there's less lines (hence the removal)...

  8. mabufo
    Member
    Posted 2 years ago #

    I'm going to have to at least learn some basic control structures in php for use in the future. Thanks for the assist.

  9. Mark / t31os
    Moderator
    Posted 2 years ago #

    Sorry i didn't mean to be condescending before... *my bad*..

    Did the code work out alright? :)

Topic Closed

This topic has been closed to new replies.

About this Topic