0

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;

2 Answers 2

1

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).

1
  • 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
    Commented May 29, 2012 at 1:35
0

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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