Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i'm making a wordpress theme and want to use a single archive.php page in order to present posts under a specific author, a category or tag. Now, i'm doing it like :

 if (is_author())
  {

  }

  if (is_category())
  {

  }

  if (is_tag())
  {

  }

I know i can divide this in more templates, but i don't want to repeat myself and i generally prefer to do it in a single php file. So, my question is :

How can i get either the category, tag or author in each of these ifs ? It seems that for the author i can use something like :

$author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

and for the category :

get_query_var( 'cat');

But is this indeed the way to do it ? What is a safe way to get that information independent of permalinks ?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

you can use it just like you posted which is safe and would do the job just fine:

if (is_author())
{
    $author = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
}

elseif (is_category())
{
    //this will work in categories tags or custom taxonomy
    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
}

else(is_tag())
{
    //this will work in categories tags or custom taxonomy
    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
}

Now after that as been said i would only recommend this way if this theme is not going to be released to the public and is for your own use, if this theme is to be released then i would divide my code in to separate files for easier customization and keeping with theme structure standards.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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