最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Get custom title if category

programmeradmin3浏览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.

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.

Share Improve this question asked Mar 12, 2020 at 8:11 Vali DragomirVali Dragomir 13 bronze badges 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. – Vali Dragomir 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/7gCH5KTj (row 2) – Vali Dragomir Commented Mar 15, 2020 at 14:44
Add a comment  | 

2 Answers 2

Reset to default 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/reference/functions/in_category/

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

发布评论

评论列表(0)

  1. 暂无评论