Support » Plugin: PHP Code Widget » [Plugin: PHP Code Widget] didnt work

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Samuel Wood (Otto)

    (@otto42) Admin

    Works fine for everybody else.

    What a useful reply that is Samuel.

    It doesn’t work for me either, I’m trying to use the following code, which works fine in the template, but not when I use it in the widget:

    <?php $custom_taxterms = wp_get_object_terms( $post->ID, 'tribe_events_cat', array('fields' => 'ids') );
    $args = array(
    'post_type' => 'tribe_events',
    'post_status' => 'publish',
    'posts_per_page' => 3,
    'orderby' => 'rand',
    'tax_query' => array(
            'taxonomy' => 'tribe_events_cat',
            'field' => 'id',
            'terms' => $custom_taxterms
    'post__not_in' => array ($post->ID),
    $related_items = new WP_Query( $args );
    if ($related_items->have_posts()) :
    echo '<div>';
    while ( $related_items->have_posts() ) : $related_items->the_post();
        	<h4><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
            <h5><meta itemprop="startDate" content="<?php echo tribe_get_start_date( null, false, 'Y-m-d-h:i:s' ); ?>"/><?php echo tribe_get_start_date(); ?></h5>
            <p><?php echo excerpt(20); ?></p>
    echo '</div>';
    Plugin Author Samuel Wood (Otto)

    (@otto42) Admin

    If your code doesn’t work in the widget, then something is wrong with your code. The widget just runs the code as-is, it doesn’t perform magic tricks.

    In your case, the “$post” variable is not defined because you failed to declare it as a global. Code in templates run in the global variable scope. Code in the widget will run inside a function scope, and you need to account for that difference.

    Much better thanks.

    I appear to have fixed it by adding a simple global $post; at the top of the loop.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: PHP Code Widget] didnt work’ is closed to new replies.