0
  1. 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.

  1. 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

1
  • Please properly indent and format your code, it is really unreadable and impossible to debug. Commented Sep 17, 2014 at 16:11

1 Answer 1

0

This doesn't actually look like a Wordpress problem, but rather a lack of PHP skills unfortunately. Your problem lies in this piece of code

$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;

The code for category 2 will never work as you have already returned the variable, and you are setting two different values to two instances of $custom_category_text as well. You can fix this with proper use of an if else statement. You can try something like this

if (in_category('1')) { // Category ID = 1
    $custom_category_text = '<p> ' . $office[$office_random[0]] . ' ' . $office1[$office1_random[0]] . '</p>';
    $content = $content . $custom_category_text;
}elseif(in_category('2')) { // Category ID = 2
    $custom_category_text = '<p> ' . $home[$home_random[0]] . ' ' . $home1[$home1_random[0]] . '</p>';
    $content = $content . $custom_category_text;
}

return $content;

Just replace the original code at the top of my answer with this

1
  • My pleasure, if my solution worked, please consider accepting my answer so that this issue can be marked as resolved :-) Commented Sep 17, 2014 at 18:17

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.