0

All of my categories has a custom single post title type, such: "How to TITLE Free" and then the article.

I want to get another title if category is News. So i won't have "How To TITLE Free" for single posts titles, but instead I will have "TITLE" only.

How can I do this? Thank you.

  • how is that custom single post title type done? is that 'How to ... Free' added to the normal title via code? – Michael Mar 12 at 16:44
  • yes, exactly. i am posting an article, and the title will automatically add "how to .... free" in it. – Vali Dragomir Mar 14 at 15:10
  • please post the code that automatically adds the 'how to ... free' to the title. you will need to add a condition to that code, that stops it from adding it if the post is in the 'News' category... – Michael Mar 14 at 18:53
  • check the code: pastebin.com/7gCH5KTj (row 2) – Vali Dragomir Mar 15 at 14:44
1

change this one line in your code:

<h1 class="article-title entry-title">How To <?php the_title(); ?> Free</h1>

to: CORRECTION:

<h1 class="article-title entry-title"><?php if( in_category( array('news') ) ) { the_title(); } else { ?> How To <?php the_title(); ?> Free<?php } ?></h1>

https://developer.wordpress.org/reference/functions/in_category/

| improve this answer | |
  • Thank you, Michael. But seems to not work. I got this error: "There has been a critical error on your website. Learn more about debugging in WordPress" when trying to access any post. – Vali Dragomir Mar 16 at 21:51
  • apologies, there was an error in the code; I corrected my answer. – Michael Mar 16 at 22:20
  • wow! it really works. Thanks a lot, Michael. – Vali Dragomir Mar 17 at 9:27
0

use get_the_category() to get category of posts. Compare it with your required category like:

$category = get_the_category();
if($category[0]->name == 'news') { 
   echo the_title();
} else {
   echo "title";
}

Hope this helps

| improve this answer | |

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.