WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
[resolved] True condition that fails (16 posts)

  1. rtoads
    Member
    Posted 1 year ago #

    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/

  2. alanft
    Member
    Plugin Author

    Posted 1 year ago #

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

  3. rtoads
    Member
    Posted 1 year ago #

    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?

  4. alanft
    Member
    Plugin Author

    Posted 1 year ago #

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

  5. rtoads
    Member
    Posted 1 year ago #

    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')
  6. rtoads
    Member
    Posted 1 year ago #

    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

  7. alanft
    Member
    Plugin Author

    Posted 1 year ago #

    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)

  8. rtoads
    Member
    Posted 1 year ago #

    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

  9. rtoads
    Member
    Posted 1 year ago #

    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.

  10. alanft
    Member
    Plugin Author

    Posted 1 year ago #

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

  11. rtoads
    Member
    Posted 1 year ago #

    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'));
  12. alanft
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  13. rtoads
    Member
    Posted 1 year ago #

    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'));

  14. alanft
    Member
    Plugin Author

    Posted 1 year ago #

    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

  15. rtoads
    Member
    Posted 1 year ago #

    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');
  16. alanft
    Member
    Plugin Author

    Posted 1 year ago #

    :-)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.