WordPress.org

Ready to get started?Download WordPress

Forums

Display the posts of one category in a Page SOLVED !! (37 posts)

  1. antistandard
    Member
    Posted 5 years ago #

    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 !

  2. testcricket
    Member
    Posted 5 years ago #

    Love your work! Thanks for sharing this :)

  3. antistandard
    Member
    Posted 5 years ago #

    No problem testcricket.

    In addition, if you add a sort order to the query, you can sort results in ascending (asc) or descending (dec).

    eg :

    <?php query_posts('cat=1'.'&orderby=date&order=asc');

    no worries

  4. mores
    Member
    Posted 5 years ago #

    Something I created for a client was a template that lists posts from a category that has the same name as the page.
    So, create a Page called "funnies" if you want to list all posts in the "funnies" category.
    Oh, and the original content of the Page is displayed too, if you need an introductory text.

    <?php /*
    Template Name: ListPostsInCategoryThatHasSameNameAsPage
    */ ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    <div id="main">
    
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><?php the_title(); ?></h1>
    		<?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    	<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    	<p><?php the_content(); ?>
    	<?php endwhile; else: endif; ?>
    
    </div>
    </div>
    
    <?php get_footer(); ?>
  5. antistandard
    Member
    Posted 5 years ago #

    Hi Mores,

    Cool. So the php code inserts the page name in place of the the cat name id ?

    That makes sense. So if you have a page called tree.php, the cat name becomes tree and it lists all posts in the category trees. Plus you get to change the text before and after the post.

    Nice.

  6. mores
    Member
    Posted 5 years ago #

    Well ... not quite ;)

    The template can be named anything you want - call it "pagecat.php" or something.
    Then upload to your theme's directory.
    THEN, write a Page - and in the options under the editor, you'll find a "template" dropdown. Select "ListPostsInCategoryThatHasSameNameAsPage" ;)

    Then give the Page a title, for example, "tree".
    The Page will then show up in the navigation, and when you click on it, it will display all posts of the "tree" category under the actual Page content.

    The only thing I think you got wrong was the "tree.php", since the template file "pagecat.php" can be used over and over again, that's the beauty of it.
    Other than that, your analysis was correct.

  7. antistandard
    Member
    Posted 5 years ago #

    fair call

  8. texasreason
    Member
    Posted 5 years ago #

    I ma having a little problem with this code, the formatting is off and is there a way to paginate the page?

  9. henlego
    Member
    Posted 5 years ago #

    for me just making the php file, putting a template name and putting this part after referrer hints (i am using Inanis Glass theme)
    <?php
    query_posts('cat=4');
    ?>

  10. ontargetpro
    Member
    Posted 5 years ago #

    I'm sure I'm doing something wrong, but I can't tell what! I created the template page as directed and called it alerts.php

    I have a page called Alert Test here: http://www.ocpantherpride.com/?page_id=53. The page itself has a little content, just a sentence and an image. In admin I told the page to use the template "alerts".

    It does include the posts from the category indicated in my alerts.php template but does not include the content from the original Alert Test page.

    What am I doing wrong that makes it so the content of the page does not display? I've tried several similar codes and always have the same result. Help?? Thanks.

  11. ontargetpro
    Member
    Posted 5 years ago #

    I found a plug-in that allows you to insert one post based on the tag. It is here: http://wordpress.org/extend/plugins/get-post/ . Not exactly the same, but it works... Still would be very grateful for assistance in getting the above code to work.

  12. docwheeler
    Member
    Posted 5 years ago #

    Great post... really helped me.

    Good work.

  13. RockTheCross
    Member
    Posted 4 years ago #

    My question is, how can I edit the code so that it will only display the title and date, and then link to the post....

  14. RockTheCross
    Member
    Posted 4 years ago #

    Nevermind, I got it cause I AM AWESOME!

  15. Brimfulof
    Member
    Posted 4 years ago #

    Well done RockTheCross, care to share?

  16. angelajude
    Member
    Posted 4 years ago #

    @mores

    your solution works beautifully. thank you tons!

  17. deadhippo
    Member
    Posted 4 years ago #

    This code repeats my posts infinitely on my page.

  18. toddsdonald
    Member
    Posted 4 years ago #

    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 ?

  19. toddsdonald
    Member
    Posted 4 years ago #

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

  20. iwingmusic
    Member
    Posted 4 years ago #

    well toddsdonald you can download the well commented file here

  21. iwingmusic
    Member
    Posted 4 years ago #

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

  22. CatWitt
    Member
    Posted 4 years ago #

    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.

  23. yeshua1984
    Member
    Posted 4 years ago #

    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

  24. seanx820
    Member
    Posted 4 years ago #

    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

  25. Jimmy
    Member
    Posted 4 years ago #

    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>
  26. Rhino666
    Member
    Posted 4 years ago #

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

  27. Mark / t31os
    Moderator
    Posted 4 years ago #

    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?

  28. redefinetheexp
    Member
    Posted 4 years ago #

    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!

  29. nilsgeylen
    Member
    Posted 4 years ago #

    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.

  30. groovyKimo
    Member
    Posted 4 years ago #

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

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

    Cheers

Topic Closed

This topic has been closed to new replies.

About this Topic