-1

Is it possible to return a custom message if the tag search returns empty?

On my menu i have a link as: www.domain.com/tag/example

I don't want to remove the link but also it's a bit annoying if the user presses on this and he sees a blank page. At least a message "sorry there no results for this tag" would do!

many thanks Andy

0

I am concerned about one part of your description. You say the user sees a "blank" page. Is the page completely blank? If so, you have a fatal error somewhere and an empty tag Loop isn't the problem. I am assuming you meant that the content is empty but otherwise the page loads fine.

You need to find the Loop for the page that displays the tags. The most likely candidates are index.php or tag.php but that is not an exhaustive list. In that file there should be something like:

if (have_posts()) {
  while(have_posts()) {
   // code to display the post
  }
}

Add an else:

if (have_posts()) {
  while(have_posts()) {
   // code to display the post
  }
} else {
  echo 'no post in that tag';
}

You need to make sure that your else echoes markup matching the Loop markup or you won't have consistent formatting.

Based on the limited information in your question that is the best answer I've got.

2
  • isn't wordpress will bypass tag.php if the requested tag didn't exist? – bangbambang Feb 7 '13 at 1:19
  • @bangbambang , not when I try it. – s_ha_dum Feb 7 '13 at 1:48

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.