WordPress.org

Ready to get started?Download WordPress

Forums

PHP Code Widget
[resolved] didnt work (5 posts)

  1. Mark Shirley
    Member
    Posted 2 years ago #

  2. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 2 years ago #

    Works fine for everybody else.

  3. flint_and_tinder
    Member
    Posted 1 year ago #

    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(
        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();
    ?>
        <div>
        	<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>
        </div>
    <?php
    endwhile;
    echo '</div>';
    endif;
    wp_reset_postdata();
    ?>
  4. Samuel Wood (Otto)
    Tech Ninja
    Plugin Author

    Posted 1 year ago #

    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.

  5. flint_and_tinder
    Member
    Posted 1 year ago #

    Much better thanks.

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic