WordPress.org

Ready to get started?Download WordPress

Forums

Displaying name of current page in query_posts? (13 posts)

  1. Jusi
    Member
    Posted 4 years ago #

    Hi, I'm looking for some way to make a query that would display posts from a category of the same name as the page it would be displayed on. Sounds complicated so let me give you an example:

    I have a page called News and a category called News as well, I would like to display post from the News category on the News page. However, I want it to be as dynamic as possible (usable on any page), so I can't hardcode the names into the query.

    I came up with this code so far:

    <?php
     $catvar = "" // here would be page name
     $query= 'category_name=' . $catvar. '';
     query_posts($query); // run the query
     ?>

    So basically all I need is a way to display name of the current page inside this code. How to achieve this?

    Thanks in advance for all help.

  2. stvwlf
    Member
    Posted 4 years ago #

    Not positive this will work but give it a try
    $post->post_name

  3. Jusi
    Member
    Posted 4 years ago #

    I haven't tried that yet, but I'm not sure it will work.

    First of all, $post->post_name returns the slug of the page/post, not the name itself, while the category_name parameter of query_posts requires the name. So the "Latest News" page will be be displayed as "latest-news" in my variable and the query will look for "latest-news" category, instead of "Latest News".

    So the question is, is there anyway to pass a category slug into query_posts and/or get a page *name* instead of slug?

  4. stvwlf
    Member
    Posted 4 years ago #

    get_category_by_slug() returns a category object when passed a category slug. From the object you can retrieve the associated cat ID and use in query_posts

  5. Jusi
    Member
    Posted 4 years ago #

    Oh, that makes sense! Sorry for a newbie questions but will the slugs of page and category with the same name will be exactly the same then? If so, I guess I have my solution. Will test it later.

    I also found a plugin called WP Extra Template Tags which has a _category_slug() - guess I could use that instead.

  6. stvwlf
    Member
    Posted 4 years ago #

    Your questions are good ones.

    What you have to be aware of is that WP will alter a post or category slug, without telling you, if the slug you either enter or it automatically computes, is already in use. That is, if you try to create two categories with the slug 'news' WP will make the second one 'news-2'. You need to pay attention to that doing it this way as it could occasionally break the connection you will be assuming exists.

  7. Jusi
    Member
    Posted 4 years ago #

    Yes, I understand I can't make two categories with the same slug. I was more worried about a Page, let's call it "Latest News" and a category with the same name. Will both have a slug of "latest-news"?

    I don't really plan to make two pages or categories with the same name, so the case you described shouldn't happen.

  8. stvwlf
    Member
    Posted 4 years ago #

    yes, both should have the same name. To test, create the page and the category and some test posts, and run the code you've come up with to confirm it works.

  9. Jusi
    Member
    Posted 4 years ago #

    Thanks for all the help, Stvwlf. Here's the final code I used - it works great:

    <?php
       $pageslug = $post->post_name; // name of the page (slug)
       $catslug = get_category_by_slug($pageslug); // get category of the same slug
       $catid = $catslug->term_id;  // get id of this category
       $query= 'cat=' . $catid. '';
       query_posts($query); // run the query
    ?>
  10. cebradesign
    Member
    Posted 4 years ago #

    Jusi,
    That little piece of code has saved me plenty of research! hehe

    THANK YOU VERY MUCH!
    :)

  11. edobart
    Member
    Posted 4 years ago #

    Hi jusi,
    I'm a WP newbie and I have to do the same think you describe.
    The problem is your code is'nt working for me.
    $catslug and $catid are empty
    So $query is only cat=
    (I tried to echo the veriables)

    Is the problem in get_category_by_slug
    ?I can't understand!
    TNX

  12. Mark / t31os
    Moderator
    Posted 4 years ago #

    Is this post/page name the same as the category slug? It could be case-sensitive..

    Remember, slug is not the same as the name (unless of course you typed them identically).

  13. negs
    Member
    Posted 4 years ago #

    Wow this is EXACTLY what I was looking to do myself. Awesome.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.