Support » Fixing WordPress » Display the posts of one category in a Page SOLVED !!

  • After a day of reading posts that just confused me more, I finally worked out a simple solution to this common yet unsolved query.

    The code (coughs politely..)

    <?php
    query_posts(‘cat=1’);
    while (have_posts()) : the_post();
    the_content();
    endwhile;
    ?>

    and that’s it! You change the category number to whatever you like by changing (‘cat=1’) to whatever cat number you want. I think you can also change cat=1 to catname=boats’n’hoes but I am not sure. You will have to look that up. I got this code from the docs page : http://codex.wordpress.org/The_Loop under the heading loop examples. Their example is long and uncommon in my opinion so I striped out the bits i need.

    NOW..to implement this code, make a template page (copy pages.php and rip out it’s content but leave the generic stuff like get_sidebar etc), and put this code (above) bang in the center.

    Then..write
    <?php /* Template Name: Boats’n’Hoes */ ?>
    at the top of the page before anything else.

    Then save template page as boatsnhoes.php and put it in the same place pages.php was located.

    An example of my page is as follows :

    <?php
    /*
    Template Name: Boats’n’Hoes
    */
    ?>
    <?php get_header(); ?>
    <div id=”main”>
    <div id=”content” class=”narrowcolumn”>

    <?php
    query_posts(‘cat=1’);
    while (have_posts()) : the_post();
    the_content();
    endwhile;
    ?>

    </div>

    <?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>

    cool ? That’s it. Search no longer. High 5 !

Viewing 15 replies - 16 through 30 (of 36 total)
  • This code repeats my posts infinitely on my page.

    Yo… This does nothing for me 🙁 how do I implement this fix to teh WordPress glitch at my WordPRess site if I don’t know php? Is there a patch or ?

    This is what solved for me. The Blog in Blog plug in was the answer and worked perfectly!

    well toddsdonald you can download the well commented file here

    i forgot to put “-” toddsdonald. change the name of file “my template.php” to “my-template.php”. sorry.

    This is a FANTASTIC work-around, but I’m having one final little problem that I can’t seem to figure out. For some reason, the content of my posts post twice, once within the div they are supposed to be contained in, but also above it in the header space. Here’s a link to what I’m talking about: http://www.austinwritesmusic.com/interviews/

    If anyone can help me, I’d be super grateful.

    At mores, and well anyone else who reads this.

    I took your idea one step further because it was annoying to use category_name=’.get_the_title() because the article title often for me had a space and this messed the query up. However I set my article permalinks to be one word and the category name could be the same thing. Ie. for posting study plans to a page called “Youth Bible Study” the permalink was “youth” I then named the category “Youth” with slug “youth”
    This also worked with categories with a space in them for instance a page called “Holy Days” with permalink “holydays” would have a category name of “Holy Days” and a slug “holydays”. Notice how this time the query doesn’t care that category name has a space in it.
    This is because the page permalink and the category slug are the same!

    Here is the code to generate the page:

    <?php /*
    Template Name: ListPostsOnPage(articleGET_PERMALINK=CATEGORY_NAME)
    */ ?>
    
    <?php get_header(); ?>
    
    <div id="main">
    
    <div id="contentwrapper">
    
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="topPost">
      <h2 class="pageTitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
      <div class="topContent"><?php the_content('(continue reading...)'); ?></div>
    <div class="cleared"></div>
    </div>
    	<?php endwhile; else: endif; ?>
    
    <?php query_posts('category_name='.get_permalink().'&post_status=publish,future');?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php if (function_exists('wp_list_comments')): ?>
    <div <?php post_class(topPost); ?>>
    
    <?php else : ?>
    <div class="topPost">
    <?php endif; ?>
    
      <h2 class="topTitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
      <p class="topMeta">by <?php the_author_posts_link(); ?> on <?php the_time('M.d, Y') ?>, under <?php the_category(', '); ?></p>
      <div class="topContent"><?php the_content('(continue reading...)'); ?></div>
      <span class="topComments"><?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?></span>
      <span class="topTags"><?php the_tags('<em>:</em>', ', ', ''); ?></span>
      <span class="topMore"><a href="<?php the_permalink() ?>">Read More...</a></span>
    <div class="cleared"></div>
    </div> <!-- Closes topPost --><br />
    
    <?php endwhile; ?>
    
    <?php else : ?>
    
    <div class="topPost">
      <h2 class="topTitle"><a href="<?php the_permalink() ?>">Not Found</a></h2>
      <div class="topContent"><p>Sorry, but you are looking for something that isn't here. You can search again by using <a href="#searchform">this form</a>...</p></div>
    </div> <!-- Closes topPost -->
    
    <?php endif; ?>
    
    <div id="nextprevious">
    <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    <div class="cleared"></div>
    </div>
    </div> <!-- Closes contentwrapper-->
    
    <?php get_sidebar(); ?>
    <div class="cleared"></div>
    
    </div><!-- Closes Main -->
    
    <?php get_footer(); ?>

    This way you could rename the page to pretty much anything as long as the permalink is the same and it will still load the category posts. For instance I could change the page name from “Holy Days” to “God’s Holy Days” or “BLAH BLAH blah”, whatever you like, It also seems that the category name doesnt have to be the same as the category slug.

    Check it out here(under construction)
    or if that link dies it means i went to production try this link.

    Hope this helps and thanks for figuring out the basic structure,

    Josh

    This is awesome guys, this is a problem a lot of people have (allowing multiple posts to different pages) I just got it working for comicpress, I wanted to separate my blog and comic on different pages and you guys made it happen! GREAT COLLABORATION EFFORT

    here is what I did:

    1) instead of copying page.php and modifying it like the 1st post, I modified index.php (since comicpress has some different things in there configuration b/c it figures you will somehow want to separate the comic and blog in some way, or at least I ponder!!) I copied this locally and began editing stuff around->

    2)I ended up with this sean-page.php and uploaded to my wp-content comic press theme for comicpress (took me like 5 tries before I got what I wanted)

    <?php
    /*
    Template Name: sean-page.php
    */
    ?>
    
    <?php get_header();  ?>
    
    <?php if (is_cp_theme_style('gn,v3c,v')) { ?>
    	<div id="content-wrapper">
    <?php } ?>
    
    <?php if (is_cp_theme_style('gn,v3c')) get_sidebar('left'); ?>
    
    <?php if (is_cp_theme_style('v3c,v')) { ?>
    	<div id="content" class="narrowcolumn">
    		<div class="column">
    <?php } ?>
    
    		<?php if (is_cp_theme_style('gn')) { ?>
    			<div id="pagewrap-right">
    		<?php } ?>
    
    	<?php if (is_cp_theme_style('3c,standard')) { ?>
    		<div id="content-wrapper">
    	<?php } ?>
    
    	<?php get_sidebar('overblog'); ?>
    
    	<?php if (is_cp_theme_style('3c')) get_sidebar('left'); ?>
    
    	<?php if (is_cp_theme_style('gn,standard,3c')) { ?>
    		<div id="content" class="narrowcolumn">
    			<div class="column">
    	<?php } ?>
    
    	<?php
    	if (have_posts()) {
    		global $blog_postcount;
    		$blog_query = 'showposts='.$blog_postcount.'&cat=-"'.exclude_comic_categories().'"&paged='.$paged; 
    
    		$posts = query_posts($blog_query);
    		while (have_posts()) : the_post();
    
    			display_blog_post();	
    
    		endwhile;
    
    		comicpress_pagination();
    	} ?>
    
    			<div class="clear"></div>
    		</div>
    	</div>
    
    <?php
    if (is_cp_theme_style('3c,v3c,gn,standard,v')) {
    	get_sidebar('right'); ?>
    <?php } ?>
    
    	<?php if (is_cp_theme_style('gn')) { ?>
    		</div>
    	<?php } ?>	
    
    	<div class="clear"></div>
    
    </div> <!-- end pageright-wrapper / content-wrapper -->
    
    <?php get_footer() ?>

    3) I used this on my BLOG page under TEMPLATE on the right-hand side where you can choose another theme. (this should work for all comicpress users trying to do two separate pages, one for comics, one for a blog (posts).

    check out my upcoming comic where I am using this design CAPS OFF PLEASE . COM

    Jimmy

    (@j_stewart89)

    I need a little help with this if anyone is able to provide some guidance. The below code displays the “posts” twice, the first time they are displayed exactly as I need them formated, but at the bottom of the page, all of the posts are displayed again using the what appears to be default display… basically the code is causing the posts to be duplicated…

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
      <h3 class="pageTitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
      <?php the_content('(continue reading...)'); ?>
    
    	<?php endwhile; else: endif; ?>
    
    <?php query_posts('cat=10&post_status=publish,future');?>
    
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    <?php if (function_exists('wp_list_comments')): ?>
    
    <?php else : ?>
    <?php endif; ?>
    
      <h3 class="topTitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
      <p class="topMeta">by <?php the_author_posts_link(); ?> on <?php the_time('M.d, Y') ?></p>
      <?php the_content('(continue reading...)'); ?>
      <span class="topComments"><?php comments_popup_link('Leave a Comment', '1 Comment', '% Comments'); ?></span>
    
    <!-- Closes topPost --><br /><br /><br />
    
    <?php endwhile; ?>
    
    <?php else : ?>
    
      <h3 class="topTitle"><a href="<?php the_permalink() ?>">Not Found</a></h3>
      <p>Sorry, but you are looking for something that isn't here. You can search again by using <a href="#searchform">this form</a>...</p>
    <!-- Closes topPost -->
    
    <?php endif; ?>
    
    <div id="nextprevious">
    <?php next_posts_link('&laquo; Older Entries') ?>
    <?php previous_posts_link('Newer Entries &raquo;') ?>
    <div class="cleared"></div>
    </div>

    I honestly wish I’d never found or heard of wordpress. It’s the biggest piece of junk I’ve ever come across.

    If you don’t want to use the software, why are you posting on the “WordPress” forums, is there something you feel you need to share?

    Rhino666 needs a hug.

    Thanks so much for all your hard work and efforts guys. This is going to allow my site to do what I’ve been searching for for a long time!

    First off, this rocks. It has set me up way better than earlier attempts trying to use plugins. So kudos.

    @j_stewart89 and @catwitt:

    Overall I’m pretty inexperienced with PHP and only starting out. What’s causing your problem however has to do with where you query (sic?) the_content and where the loops start/end.

    At first I started the loop (if have posts) and then I added both the get_content and the the query_posts command (added with a ‘if function exists’ in there as well referring to a thumbnail plugin).

    But! Now I have it. It works and here is a basic summary of what you need:

    First comes the template thing

    <?php /*Template Name: TemplateName*/?>

    Then comes the standard page markup

    <?php get_header(); ?>

    Now you start your first loop

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

    And you end it too

    <?php endwhile; endif; ?>

    That gets you your regular page contents, like an intro

    Then you run your query

    <?php query_posts('category_name=CategoryName'); ?>

    Now comes the second loop that makes for the listing of posts

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

    Here’s where I also call for the thumbnail plugin

    <?php if ( function_exists('jr_post_image') ) { jr_post_image($id); } ?>

    Some more info which I’ll style later so it sits nicely next to the thumbs

    <?php the_date('F Y'); ?>
    <a href="<?php the_permalink()?>"><?php the_title()?></a>
    <?php the_excerpt(); ?>

    And again you end the loop

    <?php endwhile; endif; ?>

    And now you can continue with sidebars, widgets or footers.

    Hope this helps. Works for me but no doubt some experts could elaborate on this or tell us where we still go wrong a bit.

    A good source on this topic QUERRY POSTS is also this userpage:

    http://codex.wordpress.org/User:JamesVL/query_posts

    Cheers

    The easiest way I have found (no coding involved!) to do this is to use the Page Links To plugin as described in this thread.

Viewing 15 replies - 16 through 30 (of 36 total)
  • The topic ‘Display the posts of one category in a Page SOLVED !!’ is closed to new replies.