• 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 : https://codex.www.ads-software.com/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 - 1 through 15 (of 36 total)
  • Love your work! Thanks for sharing this ??

    Thread Starter antistandard

    (@antistandard)

    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

    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(); ?>

    Thread Starter antistandard

    (@antistandard)

    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.

    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.

    Thread Starter antistandard

    (@antistandard)

    fair call

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

    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’);
    ?>

    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: https://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.

    I found a plug-in that allows you to insert one post based on the tag. It is here: https://www.ads-software.com/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.

    Great post… really helped me.

    Good work.

    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….

    Nevermind, I got it cause I AM AWESOME!

    Well done RockTheCross, care to share?

    angelajude

    (@angelajude)

    @mores

    your solution works beautifully. thank you tons!

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