Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+4 votes
1.0k views
in Plugins by

Hi,

I'm using this code to add the "noindex" metatag to some pages. The question is, how can I add it to unanswered questions?

class qa_html_theme_layer extends qa_html_theme_base {
public function head_metas() {
  qa_html_theme_base::head_metas();

  if ( $this->template=='question' || $this->template=='qa' )
   $this->output('<meta name="robots" content="index,follow"/>');
  else
   $this->output('<meta name="robots" content="noindex,follow"/>');
 }
}

Are there any variable, function, object or something I can use as a condition to know if answers == 0 or something like that?

Regards.

Q2A version: 1.8.3
by
Actually there is nothing to worry about unanswered as google will recognize your site as qa with qa markup schema microdata. Every question is / will be unanswered until the first answer is posted.
If you are still bothered I recommend find a way to exclude unanswered questions from the sitemap.
by
That's a very good point!
I can confirm this in Google Search Console, I'm seeing all the "valid questions" indexed and marked as questions.

Excluding things for sitemap I think is not very effective in this case, Google follows link anyway.
by
Can you share your solution in answer please ?
by
Don't understand!

The answer is above, calculate these variables "count_answers and count_comments" and check if greater than zero.

1 Answer

+1 vote
by

When you are on the unanswered page, $this->template will be 'unanswered'.

If you mean on an individual question, the simplest way would be to check $this->content['q_view']['raw']['acount'] which has the number of answers.

However, I would not recommend it. It's a bad idea for the same page to switch between indexing and no-indexing, it may make search engines slower to update after you get an answer.

Plus you're blocking many other  pages with your approach. The tags/categories pages can be useful if people search for a certain topic. Getting visitors onto any page of your site means they might stay around.

by
Thanks, Scott!
I solved this with: count(@$this->content['a_list']['as']);

The "raw" array you mentioned, seems like is not properly documented in the official site, I will try it. And yes, I'm aware of the SEO implications.
...