Treehouse: Grow your CSS skills. Land your dream job.

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

  • # February 27, 2010 at 8:04 pm

    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('‘); 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

    # February 28, 2010 at 5:59 am
    ‘category_name’ => $post->post_title,
    ‘showposts’ => 10
    # March 2, 2010 at 12:59 am

    Dave, thanks very much for the code. It works great and is a huge help to me.


Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.