Support » Fixing WordPress » List posts with the same category name as the page title name

  • Resolved lalaalaaa

    (@lalaalaaa)


    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

Viewing 7 replies - 1 through 7 (of 7 total)
  • Perhaps I could us a long line of if statements (if page title is this then query post with category this) but then I might as well create a unique page template for each — I imagine there must be something simple I’m missing!

    I can define the name I want, so now the issue is just getting it to work inside the query:

    $topic = sanitize_title($post->post_title);

    Maybe..

    $topic = str_replace( ' ' , '-' , strtolower( sanitize_title( $post->post_title ) ) );
    query_posts('category_name='.$topic.'&posts_per_page=10');

    Brilliant!!

    You’ve made my day; thank you!

    You’re welcome.. 🙂

    Hey did you use the page of posts template for this?

    I am trying to do the same thing but for just 2 pages.

    alvaron

    (@alvaron)

    I don’t know if it’s exactly what you want, but maybe this workaround can help you… it displays a list of posts, and a thumbnail for the first image and you can choose a category.

    wpworks.wordpress.com/2011/02/01/display-wordpress-post-list-with-custom-size-thumbnails/

    Best regards,

    Alvaro

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘List posts with the same category name as the page title name’ is closed to new replies.