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
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();
}