List posts with the same category name as the page title name
-
OK… I have a bunch of general pages, then a bunch of posts related to those pages. The posts are in a categories that are named the same as their related Page. What I would like to do is generate a list of the posts on the Page.
I have Pages: “Topic 1” and “Topic 2”. I have categories “Topic 1” (slug: topic-1) and “Topic 2” (slug: topic-2). And I have a bunch of posts with those categories.
I want to list “Topic 1” posts on the “Topic 1” page.
I know I can do this:
<?php query_posts('category_name=topic-1&showposts=10'); ?> <?php while (have_posts()) : the_post(); ?> <?php the_title(); ?> <?php endwhile; ?>
But because I have MANY pages to do like this, I would like to be able to use the same page template for all of them.
So how can I dynamically generate “topic-1” to use in query_posts?
I tried defining a variable by getting the page permalink and then removing the site URL, which would end up being the same as the category slug, something like:
<?php $topic = "the_permalink()"; $topic2 = substr($topic, 24); ?>
But the_permalink() isn’t generated and so I’m stuck.
I’m just starting with PHP and would really appreciate any help! (If I haven’t been clear enough, let me know.)
Thanks, Christina
- The topic ‘List posts with the same category name as the page title name’ is closed to new replies.