did you put in a global statement to import $terms and $post into the widget logic?
Thread Starter
rtoads
(@rtoads)
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?
oh yes, sorry – not paying attention there! so just $post is needed
Thread Starter
rtoads
(@rtoads)
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')
Thread Starter
rtoads
(@rtoads)
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
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)
Thread Starter
rtoads
(@rtoads)
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
Thread Starter
rtoads
(@rtoads)
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.
you need a final semi-colon, sorry should have said
Thread Starter
rtoads
(@rtoads)
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'));
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.
Thread Starter
rtoads
(@rtoads)
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'));
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
Thread Starter
rtoads
(@rtoads)
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');