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.

In a theme I'm building, I have this structure for detecting what type of page something is. However, if I visit mydomain.com/category/ or mydomain.com/tag/, all I get is the "nothing found" message I have in the have_posts()'s else statement. Is there a way to detect when these links are visited and display something other than what's in the final else statement?

if(is_category()):
   echo '<h1>title here</h1>';
elseif(is_tag()):
   echo '<h1>title here</h1>';
elseif(is_author():
   echo '<h1>title here</h1>';
elseif(is_archive()):
   echo '<h1>title here</h1>';
endif;

if(have_posts()):
     //the loop
else:
    echo '<h1>Nothing Found</h1>';
endif;
share|improve this question

2 Answers 2

There is no good way to do this, but it can be done in a way. When you visit /category/ it serves it as though you are visiting a page called category. You can look for this like:

global $wp_query;
if ($wp_query->query_vars['pagename'] == "category")
{
  // This is base category
}
else if ($wp_query->query_vars['pagename'] == "tag")
{
// this is tag base
}

But this should never be done. It is basically a junk query; a 404, and will send a 404 response code, so you will need to look at sending another response code. For that reason (if you decide to use it) this code should go in header.php although it could go high in 404.php (before any output).

share|improve this answer
    
When the example code I posted is used, there is no actual 404 error generated. Would it be better to redirect those pages to the home page instead, and how would that be done? –  Force Flow May 29 '12 at 1:35
up vote 0 down vote accepted

If I put this above the htaccess block of wordpress rules, I'm able to redirect the tag and category base pages back to the homepage.

RewriteCond %{HTTP_HOST} ^mydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^category/?$ "http://mydomain.com" [R=301,L]
RewriteRule ^tag/?$ "http://mydomain.com" [R=301,L]
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.