0

I try to display posts from specific category only in a author page, using get_option function like in the code below but it does not work:

<?php query_posts('cat='.get_option('admin_cat')); ?>

However, if using the id number, works perfect.

<?php query_posts( 'cat=6' ); ?> 

How can I use get_option function instead of ID number?

Later edit:

I apologize, it was my mistake that I wrote in admin option the category name instead of its id. However a code would be helpful to read both variants, like:

if admin option the category is an id
display its id
if in admin option category is a name
display its name

Thanks

  • 1
    in any case you shouldn't use query_posts. try doing a var_dump of your get_option call – paul Jun 9 '13 at 12:56
1

You can't use the category name to search for categories. You can use the slug. Be aware of the difference. A slug is normalized to lower case with spaces replaced by dashes. The slug for "Foo A" would be "foo-a". Using "Foo A" will not work.

$admin_cat = get_option('admin_cat');

if (ctype_digit("$admin_cat")) { // this is an ID
  $qry = array('cat' => (int)$admin_cat);
} else {
  $qry = array('category_name' => $admin_cat);
}
$my_query = new WP_Query($qry);

As stated in a comment to your question, do not use query_posts.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.