The MU forums have moved to WordPress.org

Make certain categories go into certain pages and get taken off the main page (30 posts)

  1. cpkid2
    Member
    Posted 15 years ago #

    I'd like to have posts from category X automatically be inserted into a separate page (while still being included on the main page) and have posts from category Y automatically be inserted into another separate page and NOT showing up on the main page with the other posts. How can this be done?

    Also, if I make a custom page template for each of those separate pages, will it affect anything at all?

  2. dsader
    Member
    Posted 15 years ago #

    First, doing this in single blog WP is the same as WPMU.

    Create your customized loops on a category template page excluding/including posts from categories as you wish.

    Now if you want a blog to index posts from the entire site(all public blogs), there are a couple popular approaches out there.

    I use Donncha's Sitewide Tags Plugin to aggregate all posts into main blog, then I can use standard WP template tags to manipulate the page structure/layout.

  3. cpkid2
    Member
    Posted 15 years ago #

    dsader,

    Thank you for responding. After having gone over those two pages you listed (loop and categories), I've figured out how to make category Y not show up on the main page by inserting:

    <?php if (in_category('4')) continue; ?>

    ...in my index.php after the loop. But I'm confused as to how I would make this same category (y) show up on a separate page. I will go over the two pages again but would really appreciate any help on your end.

    Thanks again!

  4. cpkid2
    Member
    Posted 15 years ago #

    Ok, I can't figure out how to make posts from certain categories appear in other pages. I would really be thankful for any guidance!

  5. dsader
    Member
    Posted 15 years ago #

    Any link to any category makes a page with just that category:
    If you have a "horses" category

    http://blog.farm.tld/category/horses/

    Yields a page full of horses only posts. etc etc etc

    If you build a categories.php in your template folder, you can output the "horse-ish" posts any way you like.

  6. dsader
    Member
    Posted 15 years ago #

    To create a new loop look at altering the query object or create a new query object altogether

    <?php $my_query = new WP_Query('category_name=special_cat&showposts=10'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <!-- Do special_cat stuff... -->
    <?php endwhile; ?>
  7. cpkid2
    Member
    Posted 15 years ago #

    I understand that if I create a category-x.php file, I can output it anyway I want but I still don't understand how to control where it's outputted.

    For now, I've created a custom "How-To's" template thinking this is a step in the right direction. Basically, whenever I create a post and mark it under the 'how-tos' category, I'd like it to appear in the "How To's" page. I believe I can style the way those posts are outputted later by using the category-x.php method mentioned above. I've included my custom template if you'd care to look at it and help me out here. I'm really stressing out here with this lol...

    <?php
    /*
     Template Name: How Tos
    */
    ?>
    
    <?php get_header(); ?>
    
    	<div id="page">
    	<div class="content">
    
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><a class="page-h2"><?php the_title(); ?></a></h2>
    			<div class="entry">
    
    				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    			</div>
    			<div class="postmetadata">
    				<?php if( function_exists('the_tags') )
    					the_tags(__('Tags: '), ', ', '<br />');
    				?>
    
    				<?php if('open' == $post->comment_status) { ?><a href="#respond"><?php _e('Comment') ?></a> (<?php comments_rss_link('RSS'); ?>)<?php } ?>
    				<?php if('open' == $post->ping_status) { ?>&nbsp;|&nbsp;&nbsp;<a href="<?php trackback_url(true); ?> " rel="trackback"><?php _e('Trackback') ?></a><?php } ?>
    				<?php edit_post_link(__('Edit'), '&nbsp;|&nbsp;&nbsp;', ''); ?>
    			 </div>
    		</div>
    	<?php comments_template(); ?>
    		<?php endwhile; endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>
  8. dsader
    Member
    Posted 15 years ago #

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    becomes

    <?php query_posts('category_name=how-tos'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  9. cpkid2
    Member
    Posted 15 years ago #

    Thank you SO much. I need to buy you a beer.

  10. cpkid2
    Member
    Posted 15 years ago #

    Is there a way I can just show the excerpts?

  11. cpkid2
    Member
    Posted 15 years ago #

    Nevermind, I changed the_content to the_excerpt. Thanks again!

  12. cpkid2
    Member
    Posted 15 years ago #

    I'm not sure if this is possible but is there a way to show a longer excerpt (more words) JUST for the first post?

    If that's not possible, is there a way I can isolate just the first excerpt (maybe a thick border around it) with CSS?

    Also, is there a way I can control how many excerpts appear on the page with the rest appearing in the sidebar under a list? Ideally, I'd like maybe around 10 per page and a section for "previous How-To's" in the sidebar.

  13. dsader
    Member
    Posted 15 years ago #

    Alter the_excerpt() for all posts in a query? Yes add the following changing 55 to whatever number of characters you want:

    So

    <?php the_excerpt(); ?>

    becomes

    <?php add_filter('excerpt_length', create_function('','return "55";')); ?>
    <?php the_excerpt(); ?>

    The rest sounds a lot like the examples detailed here: http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

    Use the query arg showposts=1 to limit the posts displayed in the first query, use it again in the second list but exclude the first post. Make the third list excluding the previous two lists.

  14. cpkid2
    Member
    Posted 15 years ago #

    Actually, I just wanted to change the first excerpt but I figured it out from the code you posted. Here is what worked for me:

    <?php
    /*
     Template Name: How Tos
    */
    ?>
    
    <?php get_header(); ?>
    
    	<div id="page">
    	<div class="content">
    
    		<!-- Only Show the How To Category -->
    		<?php query_posts('category_name=how-tos'); ?>
    		<?php if (have_posts()) : ?>
    		<?php $postcount = 0; ?>
    		<?php while (have_posts()) : the_post(); ?>
    		<?php $postcount++; ?>
    
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    
    				<div class="postmetadata" style="border-bottom:1px dotted #888; padding-bottom:10px; padding-top:10px; line-height:18px;">
    				<!-- post author -->
    				by <?php the_author() ?>
    				<!-- the date and time -->
    				on <?php the_time(get_option('date_format')) ?>
    				<!-- post category
    				under <?php the_category(', ') ?>.-->
    				<div id="home-ratings" style="float:right;"><?php if(function_exists('the_ratings')) { the_ratings(); } ?></div>
    				</div>
    			<div class="entry">
    
    				<?php if (function_exists('tweetmeme')) echo tweetmeme(); ?>
    
    				<?php if ($postcount == 1) : ?>
    				<?php add_filter('excerpt_length', create_function('','return "100";')); ?>
    				<?php the_excerpt(); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    				<?php else : ?>
    				<?php add_filter('excerpt_length', create_function('','return "55";')); ?>
    				<?php the_excerpt(); ?>
    
    				<?php endif; ?>
    
    			</div>
    			<div class="postmetadata">
    				<div style="text-align:right;" class="read-more"><a href="<?php the_permalink(); ?>" title="Read the rest of <?php the_title(); ?>" class="more-link">Read more &raquo;</a></div>
    				<?php if( function_exists('the_tags') )
    					the_tags(__('Tags: '), ', ', '<br />');
    				?>
    
    				<?php if('open' == $post->comment_status) { ?><a href="#respond"><?php _e('Comment') ?></a> (<?php comments_rss_link('RSS'); ?>)<?php } ?>
    				<?php if('open' == $post->ping_status) { ?>&nbsp;|&nbsp;&nbsp;<a href="<?php trackback_url(true); ?> " rel="trackback"><?php _e('Trackback') ?></a><?php } ?>
    				<?php edit_post_link(__('Edit'), '&nbsp;|&nbsp;&nbsp;', ''); ?>
    			 </div>
    		</div>
    	<?php comments_template(); ?>
    		<?php endwhile; endif; ?>
    
    	</div>
    
    <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>

    Thanks again. I still haven't figured out how to control the actual amount of posts that show up on this page (with the rest being in a list in the sidebar) but I'll go over that link you posted again.

  15. cpkid2
    Member
    Posted 15 years ago #

    Wait, wouldn't the posts per page be controlled universally through the Wordpress admin? Does that also apply to custom templates?

  16. dsader
    Member
    Posted 15 years ago #

    Don't use of the $postcount to tally posts that are already queried and sorted. Also using showposts in the template and the blog option is ignored leaving other queries on other pages as is.

    Here's my example that works in a basic template of mine:

    http://pastebin.com/f2ad2ac4a

    Note the use of "showposts" as a query arg in the 3 different "query_posts" to create three loops: a single longer excerpt, a list of 5 shorter excerpts, and a ul-li sidebar list.

    Note how the $do_not_duplicate and $do_not_duplicate_2 are excluded from the query with a continue(the latter excluded as an array).

  17. cpkid2
    Member
    Posted 15 years ago #

    I looked through your example and it took me awhile but I think I understand it. However, I tried using the code but am left with no content section. All that shows is the header, sidebar, and footer. Is there something in the code I need to change in order to adapt it to my site?

    -edit- Nevermind, the category name was wrong. I got it working now. I still don't understand why tallying posts using postcount after the posts have been queried is wrong, though. If you'd care to share, I'm all ears. If not, I understand you're probably a busy person and I thank you for all the help you've given me to this point. :)

  18. cpkid2
    Member
    Posted 15 years ago #

    How would I get loop 3 to show up in the sidebar?

    Before, I used to hard code it in like this:

    <div id="sidebar">
    	<ul>
    		<li></li>
    		<li></li>
    		<li></li>
    		<li></li>
    	</ul>
    </div>

    But now I don't know what to do since it's part of loop 3.

  19. cpkid2
    Member
    Posted 15 years ago #

    Also, I've been reading about multiple loops extensively and I'm wondering is $do_not_duplicate and $do_not_duplicate_2 random variables that can be interchanged with anything? I noticed in the WP documents, they used $do_not_duplicate[] instead.

  20. dsader
    Member
    Posted 15 years ago #

    $do_not_duplicate denotes a single ID.
    $do_not_duplicate[] builds an array of IDs in the second loop where there should be more than one ID excluded in the third loop.

    What they are named is irrelevant. $how_to_post and $how_to_posts[] would do the same.

    Copy paste loops anywhere you like.
    May need to redeclare global vars
    global $post, $do_not_duplicate, $do_not_duplicate_2;

    Good luck.

    Oh,
    If you'll want to hide that widget/loop when not on your how-to page use is_page_template:
    http://codex.wordpress.org/Conditional_Tags#Variable_Sidebar_Content

  21. cpkid2
    Member
    Posted 15 years ago #

    Are you saying I can copy and paste loop 3:

    ?>
    	<ul>
    		<?php
       		query_posts('category_name=how-tos&showposts=16'); //16 more posts minus those about to be excluded
       			while (have_posts()) : the_post();
    				if( $post->ID == $do_not_duplicate ) continue; //exclude the first single ID again
      				if( in_array($post->ID, $do_not_duplicate_2) ) continue; //exclude the second IDs in array
       			update_post_caches($posts);
    		?>
    			<li id="post-<?php the_ID(); ?>">
    				<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
    			</li>
    	       <?php endwhile; ?>
    	 </ul>
    	 <?php

    ...into sidebar.php and use (is_page('How-To')) to output the above code only when on the How-To page? If so, that makes sense to me.

    The only thing I'm confused about is redeclaring global variables.

    This is what my current sidebar.php looks like:

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2>Connect</h2>
    		<center>
    		<a title="Follow Us On Twitter!" href="http://www.twitter.com" style="margin-right: 10px;" target="_blank"><img alt="Follow Us On Twitter!" src="http://www.image.com/image.png"/></a>
    		<a title="Add Us On Facebook!" href="http://www.facebook.com" target="_blank"><img alt="Add Us On Facebook!" src="http://www.image.com/image.png"/></a>
    		</center>
    		</li>
    	</ul>
    </div>

    Can you guide me as to how I'd be able to insert loop 3 in there with the redeclared global variables? I'm assuming I can't just copy and paste loop 3.

  22. dsader
    Member
    Posted 15 years ago #

    Cut and paste, or you'll have 2 of loop 3. Add more <ul> and <li> to match your markup.

    Paste global $post, $do_not_duplicate, $do_not_duplicate_2; into the sidebar above your new loop. Given that the variables aren't defined in the snippet added to the sidebar, it may not pick up the variables as they where defined in the template's main section. "global" tells php to go looking for them. If you're not certain beforehand if you need the global declared - test, break it, test again.

  23. cpkid2
    Member
    Posted 15 years ago #

    I've been trying to get this working for a couple hours now and so far no success...

    Here is my attempt at putting loop 3 into the sidebar.php along with is_how-tos()

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2>Connect</h2>
    		<center>
    		<a title="Follow Us On Twitter!" href="http://www.twitter.com" style="margin-right: 10px;" target="_blank"><img alt="Follow Us On Twitter!" src="http://www.image.com/image.png"/></a>
    		<a title="Add Us On Facebook!" href="http://www.facebook.com" target="_blank"><img alt="Add Us On Facebook!" src="http://www.image.com/image.png"/></a>
    		</center>
    		</li>
    
    		<?php if (is_how-tos()) {
    			echo "<li>";
    		<h2>Previous How-To's</h2>
    		global $post, $do_not_duplicate, $do_not_duplicate_2;
    		<?php
       		query_posts('category_name=how-tos&showposts=16'); //16 more posts minus those about to be excluded
       			while (have_posts()) : the_post();
    				if( $post->ID == $do_not_duplicate ) continue; //exclude the first single ID again
      				if( in_array($post->ID, $do_not_duplicate_2) ) continue; //exclude the second IDs in array
       			update_post_caches($posts);
    		?>
    			<li id="post-<?php the_ID(); ?>">
    				<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
    			</li>
    	       <?php endwhile; ?>
    			echo "</li>";
    			}
    		?>
    
    	</ul>
    </div>
  24. dsader
    Member
    Posted 15 years ago #

    What is is_how-tos()?

    You want an existing conditional tag like
    is_page('how-tos')
    or maybe
    is_page_template('how-tos.php') - filename of template

    Get the loop to output correctly first, then add a working conditional tag.

    You are almost there.

  25. cpkid2
    Member
    Posted 15 years ago #

    I've been working at this for hours and I really don't know what I'm doing wrong. I can't get the loop to output correctly. Can you give me a hand on this?

    <div id="sidebar">
    	<ul>
    		<li>
    		<?php login_with_ajax(); ?>
    		</li>
    
    		<li>
    		<h2>Connect</h2>
    		<center>
    		<a title="Follow Us On Twitter!" href="http://www.twitter.com" style="margin-right: 10px;" target="_blank"><img alt="Follow Us On Twitter!" src="http://www.image.com/image.png"/></a>
    		<a title="Add Us On Facebook!" href="http://www.facebook.com" target="_blank"><img alt="Add Us On Facebook!" src="http://www.image.com/image.png"/></a>
    		</center>
    		</li>
    
    		<?php
    		if (is_page('how-tos')) {
    			echo "<li><h2>Previous How-To's</h2>";
    
    				<?php
    				global $post, $do_not_duplicate, $do_not_duplicate_2;
    				query_posts('category_name=how-tos&showposts=16'); //16 more posts minus those about to be excluded
    				while (have_posts()) : the_post();
    				if( $post->ID == $do_not_duplicate ) continue; //exclude the first single ID again
      				if( in_array($post->ID, $do_not_duplicate_2) ) continue; //exclude the second IDs in array
    				update_post_caches($posts);
    				?>
    				<li id="post-<?php the_ID(); ?>">
    				<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
    				</li>
    				<?php endwhile; ?>
    
    			echo "</li>";
    		}
    		?>
    </div>
  26. cpkid2
    Member
    Posted 15 years ago #

    Still stuck on this...I would really appreciate some help. I have followed the codex but the loop is still not outputting correctly.

  27. dsader
    Member
    Posted 15 years ago #

    Sorry, I'm stuck, what I've put in pastebin for you works for me.

    Just a dumb question, but how many "how-tos" posts do you have on the blog already? Perhaps the first loops are picking off all you have in the category and the third loop is empty. Sorry if that sounds dumb, but I dunno.

  28. cpkid2
    Member
    Posted 15 years ago #

    Actually, what I meant was the sidebar is showing an error with the code I posted above.

    Parse error: syntax error, unexpected '<' in sidebar.php on line 19

    I guess the code is wrong on where < ?php starts on line 19 but I don't know how to fix it. Do you have any ideas?

  29. dsader
    Member
    Posted 15 years ago #

    You've got an extra <?php on line 19. Remove it. I'm starting to think you are over your head here.

  30. cpkid2
    Member
    Posted 15 years ago #

    Yeah, this is a bit tough for me. I was originally going to go w/ my original sidebar until you suggested a better alternative. So I'm hoping you can help me work this out, although I'm spending hours on it everyday trying to understand it for myself. Also, I've tried removing <?php on line 19 but nothing changed and the error now shifted to line 34 which is the last line in the code. Anyway, I really appreciate your help and if you can help me figure this out, awesome. If not, no worries. You've helped me gain some insight as to how these templates are put together.

About this Topic