WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Step-by-step needed: page displaying a list of posts in a category (3 posts)

  1. TheOrganicSister
    Member
    Posted 5 years ago #

    I've seen a couple topics on how to have a page pull a category but I don't quite understand how to do it.

    Can someone post a step-by-step of how to do it for a codex beginner? What code to use, what to change in the code, where to put it?

    Thank you in advance,
    Tara
    http://theorganicsister.com

  2. MichaelH
    Member
    Posted 5 years ago #

    1. Read Pages.
    2. Read about the template tag, query_posts().
    3. Create a Page Template like this in your theme folder. Save it as currently_playing.php:

    <?php
    /*
    Template Name: CurrentlyPlaying
    */
    ?>
    <?php
    get_header();
    ?>
    	<div id="content" class="narrowcolumn">
    <?php
    $cat=get_cat_ID('Currently Playing'); // get a category ID
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts ('cat='.$cat.'&showposts=1&paged=' . $paged);
    ?>
    	<?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    			<div <?php post_class() ?> 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>
    				<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
    				<div class="entry">
    					<?php the_content('Read the rest of this entry &raquo;'); ?>
    				</div>
    				<p class="postmetadata"><?php the_tags('Tags: ', ', ', ''); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
    
    			</div>
    		<?php endwhile; ?>
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    	<?php else : ?>
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.
    
    		<?php get_search_form(); ?>
    	<?php endif; ?>
    	</div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    4. Create a Page called Currently Playing, and assign the Page Template CurrentlyPlaying you just created.

    For this example using the WordPress Default Theme, so the template in Step 3 was placed in the wp-content/themes/default folder. Also have a category called 'Currently Playing'. Of course you just need to replace Currently Playing with whatever you want to call it. Just so you know, the Page Template and the Page name don't need to be the same as the category name.

  3. TheOrganicSister
    Member
    Posted 5 years ago #

    Thanks for this. I actually found an easier route. I placed a hyperlink to the category in the page title. Worked like a charm. I wil save this though in cse I need it in the future. Thanks again. :)

Topic Closed

This topic has been closed to new replies.

About this Topic