• I’m using the Widget Logic plugin to load adverts dependent upon the category.

    For example, this is the code I have at present for a category:

    is_category(31) || is_single() && in_category(31)

    I have one of these set for every category.

    This is working great except that if a post is in 2 or more categories, it is getting the ads for both sets of categories it belongs to. You can see the problem here: https://www.testtrack.tv/red-bull-hell-barge/ – see the three lots of header adverts (as it belongs to three categories)?

    Does anyone know of a way to amend the logic so the widget only shows if the item belongs to a single category only?

Viewing 5 replies - 1 through 5 (of 5 total)
  • you could use

    wp_get_object_terms($post->ID, 'category');

    which gets you an array of all categories. That way you could test the number of categories a post is in. Remember to use global $post in widget logic.

    Thread Starter benfrain

    (@benfrain)

    Hi alanft, thanks for that. Unfortunately, whilst I’m a 10th Dan Black Belt ‘Copy and Paste’ Master, my PHP skills are weak. Could you elaborate a little?

    EDIT: In fact, I’m humbled slightly by realising you are indeed the Widget Logic Grandmaster himself *bows humbly*… Will get a donation over to your worthy cause in due course. Great plugin, especially if I had the PHP chops to get it to do what I need!

    Thread Starter benfrain

    (@benfrain)

    I happened across this blog post: https://spenserbaldwin.com/wordpress/categorys-of-current-post-in-wordpress/ and contacted the author who was gracious and kind enough to explain things a little further. So I now have this in the Widget Logic box:

    $cats = wp_get_object_terms( $post->ID, 'category' ); if( count( $cats ) > 1 ){ if( is_category( 26 ) || is_single() && in_category( 26 ) ); }

    Heeding the global $post point made by alanft I have also tried this:

    global $post; $cats = wp_get_object_terms( $post->ID, 'category' ); if( count( $cats ) > 1 ){ if( is_category( 26 ) || is_single() && in_category( 26 ) ); }

    But I don’t see the contact of the Widget, even on posts belonging to just a single category.

    Can a seasoned PHP’er tell me which part I have wrong?

    yr real close there. the end point is to return true or false. and WL does what’s meant to be a nice thing, but might confuse this, which is:

    If there is no ‘return’ in the text, an implicit ‘return’ is added to the start and a ‘;’ is added on the end.

    So you need to add some explicit ‘return’s otherwise your code will end up starting ‘return global $post;’ which will not evaluate ‘true’ (I think).

    So first trick (looking at your requirements in the top post is “only shows if the item belongs to a single category only”, which would translate as

    if( count( $cats ) > 1 ) return false;

    then after that you can check for your is/in_category stuff. Making:

    global $post; $cats = wp_get_object_terms( $post->ID, 'category' ); if( count( $cats ) > 1 ) return false; return ( is_category( 26 ) || is_single() && in_category( 26 ) );

    phew. let me know how that works out for you.

    Thread Starter benfrain

    (@benfrain)

    Hi Alan, thanks for the follow up. I really thought that would do it but alas no. I get the same blank space as before. (cue sad trombone noise)

    Any further thoughts?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: Widget Logic] – anyone figure the logic to do this…?’ is closed to new replies.