• Resolved wordpressnoob

    (@wordpressnoob)


    I setup a page called “Recipies”, I then created a category called “Recipies”. I want the page “Recipies” to display a list of all the posts that are in the category “Recipies”

    I have searched and believe I need to use a page template. After building one and setting my “Recipies” page to use the following template it does not work.

    Setup:
    Using default wordpress theme and the latest stable version.

    Page Title: Recipies
    Category: Name= Recipies ID= 3

    This is the code for my “Recipies” page template:

    <?php
    /*
    Template Name: Recipies
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content" class="widecolumn">
    
     <?php if (have_posts()) : while (have_posts()) : the_post();?>
     <div class="post">
      <h2 id="post-<?php the_ID(); ?>"><?php the_title();?></h2>
      <div class="entrytext">
       <?php the_content('<p class="serif">Read the rest of this page &raquo;
    '); ?>
      </div>
     </div>
     <?php endwhile; endif; ?>
     <?php edit_post_link('Edit this entry.', '', '
    '); ?>
    
    </div>
    <div id="main">
    
    <?php include (TEMPLATEPATH . '/searchform.php'); ?>
    
    <h2>Recipies:</h2>
      <ul>
          <?php
            $my_query = new WP_Query('category_ID=3');
            while ($my_query->have_posts()) : $my_query->the_post();
            $do_not_duplicate = $post->ID;
          ?>
          <?php endwhile; ?>
      </ul>
    
    </div>
    <?php get_footer(); ?>

    Where am I going wrong?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I would look at get_posts() and query_posts()

    You can also name the .PHP files so that they automatically correspond to certain categories.

    Thread Starter wordpressnoob

    (@wordpressnoob)

    Ok, this is getting closer and closer. I used some code from the website and changed a few things around. I’m still alittle confused though.

    I went and renamed the file to “category-3.php” and put the following inside as well…

    <?php
    /*
    Template Name: category-3.php
    */
    ?>
    
    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    
    <div id="content" class="widecolumn">
    <ul>
     <?php
     $myposts = get_posts('category=3');
     foreach($myposts as $post) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> --- <?php the_excerpt(); ?></li>
     <?php endforeach; ?>
     </ul>

    Do I need to query the database before I start this loop? Is that why this is working?

    <!-- Start the Loop. -->
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
     <!-- Display the Title as a link to the Post's permalink. -->
     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
     <!-- Display the Time. -->
     <small><?php the_time('F jS, Y'); ?></small>
    
     <!-- Display the Post's Content in a div box. -->
       <?php the_content(); ?>
    
     <!-- Display a comma separated list of the Post's Categories. -->
     <p class="postmetadata">Posted in <?php the_category(', '); ?>
    
     </div> <!-- closes the first div box -->
    
     <!-- Stop The Loop (but note the "else:" - see next line). -->
     <?php endwhile; else: ?>
    
     <!-- The very first "if" tested to see if there were any Posts to -->
     <!-- display.  This "else" part tells what do if there weren't any. -->
     Sorry, no posts matched your criteria.
    
     <!-- REALLY stop The Loop. -->
     <?php endif; ?>
    </div>
    
    <?php get_footer(); ?>

    It looks really close (sample output to follow):

    * smelly chicken —
    * Gross chicken —

    Recipies
    May 3rd, 2007

    Posted in Uncategorized

    I just want the smelly chicken and the gross chicken to come after the Recipies header.

    Thread Starter wordpressnoob

    (@wordpressnoob)

    Well I nearly have it working, the only problem I am no having is the formatting. It correctly pulls the posts that are of the category “Recipies” and displays them on the page “Recipies”

    The only annoying thing is that they are below the sidebar. Thanks for the help, maybe this code will help someone else do this.

    <?php
    /*
    Template Name: category-3.php
    */
    ?>
    
    <?php get_header(); ?>
    <?php get_sidebar(); ?>
    <div id="content" class="widecolumn">
    
    <!-- Start the Loop. -->
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <div class="post">
    <?php
     $myposts = get_posts('category=3');
     foreach($myposts as $post) :
     ?>
    				<h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    				<small><?php the_time('l, F jS, Y') ?></small>
    
    				<div class="entry">
    					<?php the_content() ?>
    				</div>
    
    				<p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    
    			</div>
     <?php endforeach; ?>
    
     <!-- Stop The Loop (but note the "else:" - see next line). -->
     <?php endwhile; else: ?>
    
     <!-- The very first "if" tested to see if there were any Posts to -->
     <!-- display.  This "else" part tells what do if there weren't any. -->
     Sorry, no posts matched your criteria.
    
     <!-- REALLY stop The Loop. -->
     <?php endif; ?>
    <?php get_footer(); ?>

    I went and renamed the file to “category-3.php” and put the following inside as well…

    Why can’t you work with the category templates? Just trying to understand why all this struggle for someting that is built in WP?

    Thread Starter wordpressnoob

    (@wordpressnoob)

    I can’t make the examples work. If you know how to do what I am trying to accomplish, why not post the solution or point me to an example that will work?

    The reason I am going through a struggle, is because I am trying make something work based on the web examples. Maybe you can make them better, so people like me would understand them.

    Did you click on the link I gave above (before posting back)? Did you read it?
    Category templates do exactly what you are trying to do: display only posts from one category and you can even style them as you wish.

    So, my question was: what is different in what you are trying? Why do you need to re-invent the wheel?
    (maybe I misread your posts but I couldn’t figure out what are you doing besides displaying posts by category. Not to mention – you don’t even need a category template, WP displays ONLY posts from one category when clicked its name…)

    If my assumption is correct, you went wrong by creating the Page at the very beginning. No need for that.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Page Template: Posts by category’ is closed to new replies.