treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Show posts with same category name as page name

  • I'm trying to come up with a solution for a photography site I am working on using WP.

    Here's what I'm up to:

    1) I have a Gallery page, with lots of different sub-pages.
    2) Each of these sub-pages is a gallery.
    3) For example the New York sub-page will show all posts with a category name of New York.
    4) I want to use only ONE page template for each of these gallery sub-pages. So I need a way to dynamically show all posts with a category name equal to the name of the sub-page that is showing those posts.

    Does that make sense? Maybe there's a better way of going about this? Custom fields? Bottom line is I don't want to have to create a new page template every time a new photo gallery is added.

    The following piece of code worked for me, displaying all posts from category three, but of course means that I will have to create a new page template each time I want to add a photo gallery. Not ideal.
    <?php
    /*
    Template Name: gallerypage
    */
    ?>
    <?php query_posts('cat=3&showposts=10'); if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php the_excerpt(); ?>
    <?php endwhile; endif; ?>


    I tried using wp-title to grab the name of the gallery and find posts that have that same name:
    <?php  query_posts('<?php wp_title(); ?>'); if (have_posts()) : while (have_posts()) : the_post(); ?>


    ...but it didn't seem to work for me. It just returned posts from my blog category.

    Anyone have any ideas?

    Didn't wo
  • query_posts(array(
    'category_name' => $post->post_title,
    'showposts' => 10
    ));
  • Dave, thanks very much for the code. It works great and is a huge help to me.

    Thanks!