• Hi
    I’m trying to display a list of posts from a specific category on one page. I have created a new template by duplicating and renaming the page_blog.php file so that the posts are displyed in the same format and I have linked the page to the new template. I am now trying to only display posts from one category on this page but I’m not sure how to do this. Can someone help?
    Thanks

    <?php
    /*
    Template Name: Dropout Blog
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content">
    
    	<div id="contentleft">
    
    		<div class="postarea">
    
    			<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("cat=" .st_option('blog_cat'). "&showposts=" .st_option('blog_cat_num'). "&paged=$paged"); while ( have_posts() ) : the_post() ?>
    
    			<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
                <div class="postauthor">
                	<p><?php _e("Posted by", 'structuretheme'); ?> <?php the_author_posts_link(); ?> on <?php the_time('F j, Y'); ?> &middot; <a href="<?php the_permalink(); ?>#comments"><?php comments_number('Leave a Comment', '1 Comment', '% Comments'); ?></a>&nbsp;<?php edit_post_link('(Edit)', '', ''); ?></p>
                </div>
    
    			<!--The blog page is currently using the Limit Post funtion and is currently set to display 450 characters. You can increase or decrease it as you wish.-->
    
                <?php the_content_limit(450, "Read More"); ?><div style="clear:both;"></div>
    
    			<div class="postmeta">
    				<p><?php _e("Filed under", 'structuretheme'); ?> <?php the_category(', ') ?> &middot; <?php _e("Tagged with", 'structuretheme'); ?> <?php the_tags('') ?></p>
    			</div>
    
    			<?php endwhile; ?>
    
    			<div id="prevLink"><p><?php previous_posts_link(); ?></p></div>
    			<div id="nextLink"><p><?php next_posts_link(); ?></p></div>
    
    		</div>
    
    	</div>
    
    <?php include(TEMPLATEPATH."/sidebar_right.php");?>
    
    </div>
    
    <!-- The main column ends  -->
    
    <?php get_footer(); ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • @stefan83

    Use the following code where ever you want. Before adding the code into the file, please make sure that you have a copy of that file.

    <?php wp_list_categories('title_li=&orderby=id');
    query_posts('cat=enter the category number here');
    ?>

    Thanks

    A quick aside: Do you know about category-xxxxxx.php? This would, by default, display only posts from the xxxxxx category and you don’t need to create any redirects, rewrites, or includes.

    Don’t know your level of experience with WordPress so I thought I’d throw this in. ??

    Thread Starter Stefan

    (@stefan83)

    Thanks Chikku, unfortunately I can’t get this to work. The page is still showing all the posts from the default category and not from the category I’ve specified. Any ideas?

    <?php
    /*
    Template Name: Dropout Blog
    */
    ?>
    <?php wp_list_categories('title_li=&orderby=id');
    query_posts('cat=5');
    ?>
    <?php get_header(); ?>
    
    <div id="content">
    
    	<div id="contentleft">
    
    		<div class="postarea">
    
    			<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("cat=" .st_option('blog_cat'). "&showposts=" .st_option('blog_cat_num'). "&paged=$paged"); while ( have_posts() ) : the_post() ?>
    
    			<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
                <div class="postauthor">
                	<p><?php _e("Posted by", 'structuretheme'); ?> <?php the_author_posts_link(); ?> on <?php the_time('F j, Y'); ?> &middot; <a href="<?php the_permalink(); ?>#comments"><?php comments_number('Leave a Comment', '1 Comment', '% Comments'); ?></a>&nbsp;<?php edit_post_link('(Edit)', '', ''); ?></p>
                </div>
    
    			<!--The blog page is currently using the Limit Post funtion and is currently set to display 450 characters. You can increase or decrease it as you wish.-->
    
                <?php the_content_limit(450, "Read More"); ?><div style="clear:both;"></div>
    
    			<div class="postmeta">
    				<p><?php _e("Filed under", 'structuretheme'); ?> <?php the_category(', ') ?> &middot; <?php _e("Tagged with", 'structuretheme'); ?> <?php the_tags('') ?></p>
    			</div>
    
    			<?php endwhile; ?>
    
    			<div id="prevLink"><p><?php previous_posts_link(); ?></p></div>
    			<div id="nextLink"><p><?php next_posts_link(); ?></p></div>
    
    		</div>
    
    	</div>
    
    <?php include(TEMPLATEPATH."/sidebar_right.php");?>
    
    </div>
    
    <!-- The main column ends  -->
    
    <?php get_footer(); ?>
    <?php endwhile;?>

    If PHP codes does not come in to play with you, then you can switch on to amazing plugins!

    Thread Starter Stefan

    (@stefan83)

    Hi New Nine Media, no I don’t, could youplease explain a little further?

    The code i posted before is wrong so sorry about that. The following is now giving me an error message: Parse error: syntax error, unexpected T_ENDWHILE in /home/dropout/public_html/wp-content/themes/structure_theme_white/blog.php on line 49

    <?php
    /*
    Template Name: Dropout Blog
    */
    ?>
    <?php wp_list_categories('title_li=&orderby=id');
    query_posts('cat=5');
    ?>
    <?php get_header(); ?>
    
    <div id="content">
    
    	<div id="contentleft">
    
    		<div class="postarea">
    
    			<?php $page = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("cat=" .st_option('blog_cat'). "&showposts=" .st_option('blog_cat_num'). "&paged=$paged"); while ( have_posts() ) : the_post() ?>
    
    			<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
                <div class="postauthor">
                	<p><?php _e("Posted by", 'structuretheme'); ?> <?php the_author_posts_link(); ?> on <?php the_time('F j, Y'); ?> &middot; <a href="<?php the_permalink(); ?>#comments"><?php comments_number('Leave a Comment', '1 Comment', '% Comments'); ?></a>&nbsp;<?php edit_post_link('(Edit)', '', ''); ?></p>
                </div>
    
    			<!--The blog page is currently using the Limit Post funtion and is currently set to display 450 characters. You can increase or decrease it as you wish.-->
    
                <?php the_content_limit(450, "Read More"); ?><div style="clear:both;"></div>
    
    			<div class="postmeta">
    				<p><?php _e("Filed under", 'structuretheme'); ?> <?php the_category(', ') ?> &middot; <?php _e("Tagged with", 'structuretheme'); ?> <?php the_tags('') ?></p>
    			</div>
    
    			<?php endwhile; ?>
    
    			<div id="prevLink"><p><?php previous_posts_link(); ?></p></div>
    			<div id="nextLink"><p><?php next_posts_link(); ?></p></div>
    
    		</div>
    
    	</div>
    
    <?php include(TEMPLATEPATH."/sidebar_right.php");?>
    
    </div>
    
    <!-- The main column ends  -->
    
    <?php get_footer(); ?>
    <?php endwhile;?>

    Ok. Let’s start by assuming you have a category called “books” (category id is, say, 15).

    If you create a category.php template file in your theme, your website will access category.php when someone views the “books” category. You can then edit the category.php file to change the display to whatever you want.

    Take our site, for example. We have a category called “tutorials” here: https://www.newnine.com/tutorials. The design of our tutorials category is in category.php, which differs from the design in single.php (the template file for a single post). That’s why the designs look slightly different between our tutorials category and an individual post (like this one: https://www.newnine.com/tutorials/29-remove-category-from-urls-in-wordpress).

    We didn’t create any special redirects or anything. We simply copied the index.php file, renamed it to category.php, and WordPress took care of the rest — sending all “tutorial category” requests to the new category file.

    Now…if you want to get even more specific (maybe a different design for each category), you can create category-xxx.php for the “xxx” category, category-yyy.php for the “yyy” category, and so on.

    See the WordPress template heirarchy: https://codex.www.ads-software.com/images/1/18/Template_Hierarchy.png

    For the books category, WordPress would first look for a category-books.php file. If none exists, it would look for the category-15.php. If that doesn’t exist, it will look for category.php. No category.php? It will look for archive.php, and then finally settle on index.php.

    You can create redirects to do other things, but for a category page, the functionality is all there with no extra redirect coding. Just create the file and design away.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display category posts on a wp page’ is closed to new replies.