- I created an extra content file and inserted custom text for a category (and relevant to that category) to display (on a random basis) in all posts belonging to that category and it is all working fine.
This is the code I used :-
<?php
add_filter('the_content', 'custom_category_text');
function custom_category_text($content) {
global $post;
$office = array( "Pellentesque arcu nisi, pellentesque nec gravida quis.", "Pellentesque arcu nisi." );
$office_random = array_rand($office, 2);
$office1 = array( "Pellentesque arcu nisi, pellentesque nec gravida quis.", "Pellentesque arcu nisi." );
$office1_random = array_rand($office1, 2);
$custom_category_text = '<p> ' . $office[$office_random[0]] . ' ' . $office1[$office1_random[0]] . '</p>';
// Category ID = 1
if (in_category('1')) {
$content = $content . $custom_category_text;
}
return $content;
}
?>
In the above code my category is office and I have identified custom text with the name $office and $office1 ( for 2 paragraphs of custom text) so that I remember what I'm doing.
- I have 2 categories and now want to insert additional random custom text relevant to the 2nd category and so that each instance of custom text displays in all posts identified by its category.
This is the amended code below but the custom text for category ID 2 is not displaying at all.
<?php
add_filter('the_content', 'custom_category_text');
function custom_category_text($content) {
global $post;
$office = array( "Pellentesque arcu nisi, pellentesque nec gravida quis.", "Pellentesque arcu nisi." );
$office_random = array_rand($office, 2);
$office1 = array( "Pellentesque arcu nisi, pellentesque nec gravida quis.", "Pellentesque arcu nisi." );
$office1_random = array_rand($office1, 2);
$home = array( "Pellentesque arcu nisi, pellentesque nec gravida quis.", "Pellentesque arcu nisi." );
$home_random = array_rand($home, 2);
$home1 = array( "Pellentesque arcu nisi, pellentesque nec gravida quis.", "Pellentesque arcu nisi." );
$home1_random = array_rand($home1, 2);
$custom_category_text = '<p> ' . $office[$office_random[0]] . ' ' . $office1[$office1_random[0]] . '</p>';
// Category ID = 1
if (in_category('1')) {
$content = $content . $custom_category_text;
}
return $content;
$custom_category_text = '<p> ' . $home[$home_random[0]] . ' ' . $home1[$home1_random[0]] . '</p>';
// Category ID = 2
if (in_category('2')) {
$content = $content . $custom_category_text;
}
return $content;
}
?>
So that's my problem and I hope someone can help me fix this - the custom text for the 2nd category is not displaying at all.
Many thanks pete