• Hi, I’m working offline on a WP blog which is made up of static pages and posts.

    I have created a Page called ‘Warwick’
    I have also created a Category called ‘Warwick’.

    I have created a template file called Warwick.php and have used this as the Page Template for the Warwick Page.

    Inside the Warwick page, I have a specialised introduction to the page. Underneath this, I’d like to list all posts which have the Category ‘Warwick’.

    This is the code at the top of the WordPress Loop:

    <?php query_posts('category_name=Warwick'); ?><!-- Filter to show only Warwick Catergory Posts -->
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    However, when I open this page, I get the search result error stating that no posts matched my criteria.

    What am I doing wrong here?!

    Thanks, Richard

Viewing 3 replies - 1 through 3 (of 3 total)
  • Might consider this:

    If page title and category name are the same, display the posts in that category:

    <?php
    if (is_page()) {
      $cat=get_cat_ID($post->post_title); //use page title to get a category ID
      $posts = get_posts ("cat=$cat&showposts=5");
      if ($posts) {
        foreach ($posts as $post):
          setup_postdata($post); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <?php endforeach;
      }
    }
    ?>

    Thread Starter John

    (@mav3000)

    Hi Michael,

    Thanks very much for the code. Unfortunately I don’t understand much of it. Could you tell me what it does, and where in my template I need to put it?

    Sorry for being a newbie!

    Richard

    That would pretty much be the basis of your Page Template. Of course you would want header/footer/sidebar calls, and you might want to embelish the loop I’ve included (e.g. use the_content). In fact you don’t even need to create a Page Template for each Page as that will use the Page Name to determine the Category.

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem listing category posts on a Page’ is closed to new replies.