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.

4
  • how is that custom single post title type done? is that 'How to ... Free' added to the normal title via code?
    – Michael
    Commented Mar 12, 2020 at 16:44
  • yes, exactly. i am posting an article, and the title will automatically add "how to .... free" in it. Commented Mar 14, 2020 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
    Commented Mar 14, 2020 at 18:53
  • check the code: pastebin.com/7gCH5KTj (row 2) Commented Mar 15, 2020 at 14:44

2 Answers 2

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/

3
  • 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. Commented Mar 16, 2020 at 21:51
  • apologies, there was an error in the code; I corrected my answer.
    – Michael
    Commented Mar 16, 2020 at 22:20
  • wow! it really works. Thanks a lot, Michael. Commented Mar 17, 2020 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

5
  • This code is not working. Nothing changes. So, for any single post in all of my categories I have a custom title like: "How To <?php the_title(); ?> Free" I want to exclude "How To" on News category, and let <?php the _title();?> only Commented Mar 12, 2020 at 15:53
  • Can you share your entire code used on template?? Commented Mar 13, 2020 at 9:36
  • this is single.php pasteio.com/xAxR4pebwEVK Commented Mar 13, 2020 at 10:21
  • url not working Commented Mar 13, 2020 at 11:24
  • check this one: pastebin.com/7gCH5KTj Commented Mar 13, 2020 at 14:41

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.