Support » Plugin: Widget Logic » True condition that fails

  • Resolved rtoads

    (@rtoads)


    I love your plug-in, but here’s some code that should evaluate to true for a particular post (I echoed it out on the page) and yet the widget fails to appear:

    $terms = get_the_term_list( $post->ID , 'Placement'); strpos($terms,"art-home") && strpos($terms,"writing-home") && strpos($terms,"design-home")

    Basically, I want to widget to appear if and only if all three of the strings appear in the term list. The code evaluates to three positive integers, yet doesn’t evaluate to true. I also tried explicitly stating “> 0” which is also true. What’s going on?

    http://wordpress.org/extend/plugins/widget-logic/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Contributor alanft

    (@alanft)

    did you put in a global statement to import $terms and $post into the widget logic?

    Aha. I see now I need to global in $post. Thank you. But since $terms is defined inline, isn’t it available to the logic? If not, where would I put the global statement?

    Plugin Contributor alanft

    (@alanft)

    oh yes, sorry – not paying attention there! so just $post is needed

    Hi. Back again with another true condition that fails. It may be failing because I’ve somehow got the syntax wrong, but the condition of the post’s slug (either slug 0 or slug 1) being ‘writing-home’ is definitely true on the two different posts I tested. I wrote the logic two ways and both fail:

    Version 1:

    global $post;$term_list = wp_get_post_terms($post->ID, 'Placement', array("fields" => "all"));$primary_placement = $term_list[0]->slug;if($primary_placement == "home"){$primary_placement = $term_list[1]->slug;}
    $primary_placement == 'writing-home'

    Version 2:

    global $post;$term_list = wp_get_post_terms($post->ID, 'Placement', array("fields" => "all"));$primary_placement0 = $term_list[0]->slug;$primary_placement1 = $term_list[1]->slug;
    $primary_placement0 == 'writing-home' || ($primary_placement0 == 'home' && $primary_placement1 == 'writing-home')

    I took a look at the PHP error log, and here’s what I get when I run Version 1 above:

    PHP Parse error: syntax error, unexpected T_GLOBAL in /wp-content/plugins/widget-logic/widget_logic.php(284) : eval()’d code on line 1

    Plugin Contributor alanft

    (@alanft)

    you don’t explicitly return a value – which might be the issue. stick in something like

    return ($primary_placement == ‘writing-home’)

    in version 1 for instance, and it might work. (Though I’ve not followed through the logic of the wp_get_post_terms bit entirely)

    This code:

    global $post;$term_list = wp_get_post_terms($post->ID, 'Placement', array("fields" => "all"));$primary_placement = $term_list[0]->slug;if($primary_placement == "home"){$primary_placement = $term_list[1]->slug;}
    return ($primary_placement == 'writing-home')

    produces this PHP error:

    PHP Parse error: syntax error, unexpected $end in /mnt/stor10-wc1-ord1/803637/www.pyragraph.com/web/content/wp-content/plugins/widget-logic/widget_logic.php(284) : eval()’d code on line 2

    This really seems to be a parsing error. The PHP code works when I execute it elsewhere. I just want to be sure I’m following Widget Logic’s rules for crafting the code. Am I? Thanks.

    Plugin Contributor alanft

    (@alanft)

    you need a final semi-colon, sorry should have said

    OK, I guess I don’t understand what the semicolon rules are in Widget Logic tests. If I only use WP conditional built-ins, I don’t need a semicolon? But if I use a PHP variable I do?

    How would I mix testing with built-ins like in_category(‘writing’) and the returned variable? Like this?

    global $post;$term_list = wp_get_post_terms($post->ID, 'Placement', array("fields" => "all"));$primary_placement = $term_list[0]->slug;if($primary_placement == "home"){$primary_placement = $term_list[1]->slug;}
    return ($primary_placement == 'writing-home' && in_category('writing'));
    Plugin Contributor alanft

    (@alanft)

    a longer explanation is here

    http://wordpress.org/extend/plugins/widget-logic/other_notes/

    but in brief, the shorthand of putting in a statement with a simple true/false value like is_home() was so clearly useful that I made it so that WL adds a “return … ;” around text that doesn’t have an explicit “return” in it by default.

    Thanks. Sorry to be dense, but would the syntax of a compound test then be as I wrote it above? That is:

    return ($primary_placement == 'writing-home' && in_category('writing'));

    Plugin Contributor alanft

    (@alanft)

    yes it would.

    there’s some chance there’s problem with the ‘;’ in your if statement too by the way – though i’m not confident about that. but as the block “inside” the if is a single statement you could just remove the {} braces and it will work ok. I think

    This code appears to work properly!

    global $post;$term_list = wp_get_post_terms($post->ID, 'Placement', array("fields" => "all"));$primary_placement = $term_list[0]->slug;if($primary_placement == "home") $primary_placement = $term_list[1]->slug;
    return (!is_category('writing') and !is_category('resource-center') and !is_home() and $primary_placement == 'writing-home');
    Plugin Contributor alanft

    (@alanft)

    🙂

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘True condition that fails’ is closed to new replies.