categories - Display Some text in some specific category not all category

admin2025-01-07  3

I want to display some texts in specific category in single post based on category in wordpress.. how can i do that

for example posts in news category display some text.(not all category)

Thank you

I want to display some texts in specific category in single post based on category in wordpress.. how can i do that

for example posts in news category display some text.(not all category)

Thank you

Share Improve this question asked Oct 29, 2017 at 7:48 DanielDaniel 12 bronze badges 2
  • The same text for all posts? Where do you want the text to appear? – Jacob Peattie Commented Oct 29, 2017 at 8:01
  • yes in particular category only same text – Daniel Commented Oct 29, 2017 at 8:05
Add a comment  | 

2 Answers 2

Reset to default 0

Place this code in single.php where you need to show it:

<?php 
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
    $catid = $cat->term_id;

    if ($catid == 5) {
      echo 'TEXT HERE';
    }
    if ($catid == 7) {
      echo 'ANOTHER TEXT';
    }

}
?>

To show specific text in a single post based on the category in WP set condition for allows content display.

Replace your_category_slug with the slug of the category you want to target. You can use has_category() function to check if the post belongs to a specific category.

if(has_category('your_category_slug')){
    // Content to display if the post is in the specified category
    echo '<p>TEXT HERE</p>';
} else {
    // Default content to display if the post is not in the specified category
    the_content();
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260307a656.html

最新回复(0)