Title: Displaying name of current page  in query_posts?
Last modified: August 19, 2016

---

# Displaying name of current page in query_posts?

 *  [Justyna Ratajczak](https://wordpress.org/support/users/jusi/)
 * (@jusi)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/)
 * 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.

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090174)
 * Not positive this will work but give it a try
    `$post->post_name`
 *  Thread Starter [Justyna Ratajczak](https://wordpress.org/support/users/jusi/)
 * (@jusi)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090204)
 * 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?
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090209)
 * 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
 *  Thread Starter [Justyna Ratajczak](https://wordpress.org/support/users/jusi/)
 * (@jusi)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090223)
 * 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](http://www.web-templates.nu/2008/08/25/wp-extra-template-tags/)
   which has a `_category_slug()` – guess I could use that instead.
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090236)
 * 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.
 *  Thread Starter [Justyna Ratajczak](https://wordpress.org/support/users/jusi/)
 * (@jusi)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090245)
 * 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.
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090247)
 * 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.
 *  Thread Starter [Justyna Ratajczak](https://wordpress.org/support/users/jusi/)
 * (@jusi)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090421)
 * 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
       ?>
       ```
   
 *  [cebradesign](https://wordpress.org/support/users/cebradesign/)
 * (@cebradesign)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090506)
 * Jusi,
    That little piece of code has saved me plenty of research! hehe
 * **THANK YOU VERY MUCH!**
    🙂
 *  [edobart](https://wordpress.org/support/users/edobart/)
 * (@edobart)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090558)
 * 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
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090559)
 * 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).
 *  [negs](https://wordpress.org/support/users/negs/)
 * (@negs)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090562)
 * Wow this is EXACTLY what I was looking to do myself. Awesome.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Displaying name of current page in query_posts?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 12 replies
 * 6 participants
 * Last reply from: [negs](https://wordpress.org/support/users/negs/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/displaying-name-of-current-page-in-query_posts/#post-1090562)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
