WordPress.org

Support

Support » How-To and Troubleshooting » query_posts and category_name

query_posts and category_name

  • I am working on a custom category view for one of my blogs and am having a bit of difficulty with query_posts using category_name as a parameter.

    Reading through the Codex, it implies that you can use the actual category name for the category_name parameter — example: query_posts(category_name=Category Name).
    http://codex.wordpress.org/Template_Tags/query_posts#Category_Parameters

    However, after trying variations of it, I’ve discovered that it’s not actually accepting Category Name, but it does accept the category nice name — example: query_posts(category_name=category-slug).

    Can anyone explain (or direct me to) a quick and easy way of grabbing the current category’s nice name without needing to add multiple lines of code?

    (By the way, is this a bug or is the Codex just wrong?)

    Thanks for any help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Are you doing this:

    query_posts(category_name=Category Name);

    or this:

    query_posts('category_name=Category Name');

    Note the quotes around the query string arguments. Anyway, it should work with the full name as WordPress just ‘sanitizes’ it, as it does when generating the url slug, before making use of it in the query.

    The second:
    query_posts(‘category_name=Category Name’);

    I probably should have specified that in my original post, sorry.

    (I also tried it the first way you mentioned for good measure. LOL)

    Note the quotes around the query string arguments. Anyway, it should work with the full name as WordPress just ‘sanitizes’ it, as it does when generating the url slug, before making use of it in the query.

    If it’s supposed to be as you say, then it’s most certainly a bug with WordPress because I tried it both ways and neither way worked (only when I put in the already sanitized category-name slug will it work).

    For now, since I was unable to find a function to snag the category nice name and the_category_ID() has been deprecated, I had to fall back to a clunkier solution.

    Just in case anyone encounters the same problem, here’s the code I ended up using:

    foreach(get_the_category() as $category) { $cat=$category->cat_ID; }
    query_posts("cat=$cat");

    A few more lines than I’d like, but it works. 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘query_posts and category_name’ is closed to new replies.