WordPress.org

Ready to get started?Download WordPress

Forums

Displaying cats with same title as page (6 posts)

  1. alvarix
    Member
    Posted 5 years ago #

    Hi,

    This is the second time I have to do this, so I wanted to see if there is a less hacky way than what I've conjured:

    if (preg_match('/about/',$query_string)) {
    	$mycat=substr($query_string,22);
    }
    query_posts("category_name=$mycat);

    ..basically grabbing the url and chopping out the page slug to use for my cat query.

    Thanks,
    alvar

  2. doc4
    Member
    Posted 5 years ago #

    alvarix,

    Can you explain a little more? If you're able to do this I assume you can just use a WP query as well.

    <?php $my_query = new WP_Query('cat=6&showposts=1');
             while ($my_query->have_posts()) : $my_query->the_post();
             $do_not_duplicate = $post->ID; ?>

    Why doesn't this work?

    http://www.doc4design.com

  3. alvarix
    Member
    Posted 5 years ago #

    doc4,
    Thanks, you know a lot more about wordpress than I do it seems, but I don't think you are solving my problem. In your example I would need to grab cat=6 dynamically from the current page, which means I cant use the numeric ids, rather the slug strings. Eg:

    I have a page named about that I want to automatically display posts in the about category as well. Hence the chopping of the url in my example, to parse the about page url to create the category query.

    Ideally the solution would work across multiple page/cat intersections, ie as a template for multiple pages.

  4. doc4
    Member
    Posted 5 years ago #

    alvarix,

    I'm a little confused as to what you are try to do. I understand you are checking the URL to retrieve a specific category. I have resorted to this in an instance where the only difference between the pages was the URL but I wouldn't recommend this as an option if at all possible to avoid it.

    Your example didn't display.

    If you have a category named About and a page named About why not call on posts specifically in the About category on the About template? It doesn't seem necessary to use preg_match in this case because the page is so specific.

    <?php $my_query = new WP_Query('category_name=about&showposts=1');
             while ($my_query->have_posts()) : $my_query->the_post();
             $do_not_duplicate = $post->ID; ?>
  5. alvarix
    Member
    Posted 5 years ago #

    The reason for the pregmatch is because this query occurs on a template that spans several pages (ie. about, video, books) so I need to create the query dynamically.

  6. alvarix
    Member
    Posted 5 years ago #

    bump.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.