使用Tribe的Events插件从短代码运行查询时遇到问题

时间:2014-09-01 作者:GabrielZee

即使我传入了正确的值,我也不会使用此代码返回任何结果。谁能看看吗?

function webinar_list_shortcode($atts) {
  global $post;
 // Defaults
extract( shortcode_atts ( array (
        \'posts\' => \'5\',
        \'status\' => \'future\',
        \'eventcat\' => \'\'
    ),
    $atts
));

$options = array(
    \'eventDisplay\'=>$status,
    \'posts_per_page\'=>$posts,
    \'tax_query\'=> array(
        array(
            \'taxonomy\' => \'tribe_events_cat\',
            \'tag_ID\' => $eventcat
        )
    )
);

 // query is made
 $query = new WP_Query( $options );

 // Reset and setup variables
 $output = \'\';
 $temp_title = \'\';
 $temp_link = \'\';
 $temp_ex = \'\';
 $temp_content = \'\';
 $temp_thumb = \'\';
 $temp_id = \'\';

 // the loop
 if ( $query->have_posts() ) {
    $temp_id = $post->ID;
    $temp_title = the_title(\'\',\'\',false);
    $temp_link = get_permalink($post->ID);
    $temp_content = get_the_content($post->ID);
    $temp_date = tribe_get_start_date( $post->ID, true, \'l, F j, Y\' );
    $temp_speaker = get_field(\'speaker\');
    //$temp_ex = get_the_excerpt();
      if ( has_post_thumbnail() ) {
        $temp_thumb = get_the_post_thumbnail($post->ID, array(200,200));
      } else {
        $temp_thumb = "";
      }

    // output all findings -
   $output .= "
  <div class=\'et_pb_row webinar-row clearfix\'>
  <div class=\'et_pb_column et_pb_column_1_4\'>
   $temp_thumb
  </div>
  <div class=\'et_pb_column et_pb_column_3_4\'>
   <div class=\'post-$temp_id\' id=\'post-$temp_id\'>
              <h3 class=\'webinar-time\'>
                  $temp_date
              </h3><!--BEGIN .entry-content-->
              <div class=\'webinar-content\'>
              <p><span class=\'purple-text\'>Event: </span>$temp_title</p>
              <p><span class=\'purple-text\'>Speaker: </span>$temp_speaker</p>
              <a href=\'$temp_link\' class=\'button-details\'>View Event Details<span class=\'et-icon\'></span></a>
              </div><!--END .entry-content-->
              </div><!--END .hentry-->
          </div></div>";
 } else {
    $output .= "not found.";
 }
 wp_reset_query();
 return $output;
}

2 个回复
SO网友:TheDeadMedic

您需要将代码包装在一个循环中。而不是if ( $query->have_posts() ) {, 使用:

while ( $query->have_posts() ) {
    $query->the_post();

    // Rest of code

}

wp_reset_postdata();

SO网友:GabrielZee

我确实使用tribe\\u get\\u events()实现了这一点,如下所示:

function webinar_list_shortcode($atts) {
  global $post;
 // Defaults
extract( shortcode_atts ( array (
        \'numposts\' => \'5\',
        \'status\' => \'upcoming\',
        \'eventcat\' => \'\'
    ),
    $atts
));

$options = array(
    \'eventDisplay\'=>$status,
    \'posts_per_page\'=>$numposts,
    \'tax_query\'=> array(
        array(
          \'taxonomy\' => \'tribe_events_cat\',
          \'field\' => \'slug\',
          \'terms\' => $eventcat
        )
    )
);

 // query is made
 $events = tribe_get_events( $options );

 // Reset and setup variables
 $output = \'\';
 $temp_title = \'\';
 $temp_link = \'\';
 $temp_ex = \'\';
 $temp_content = \'\';
 $temp_thumb = \'\';
 $temp_id = \'\';

 // the loop
foreach($events as $post) {
    setup_postdata($post);
    $temp_id = $post->ID;
    $temp_title = the_title(\'\',\'\',false);
    $temp_link = get_permalink($post->ID);
    $temp_content = get_the_content($post->ID);
    $temp_date = tribe_get_start_date( $post->ID, true, \'l, F j, Y\' );
    $temp_speaker = get_field(\'speaker\');
    //$temp_ex = get_the_excerpt();
      if ( has_post_thumbnail() ) {
        $temp_thumb = get_the_post_thumbnail($post->ID, array(200,200));
      } else {
        $temp_thumb = "";
      }

    // output all findings -
   $output .= "
  <div class=\'et_pb_row webinar-row clearfix\'>
  <div class=\'et_pb_column et_pb_column_1_4\'>
   $temp_thumb
  </div>
  <div class=\'et_pb_column et_pb_column_3_4\'>
   <div class=\'post-$temp_id\' id=\'post-$temp_id\'>
              <h3 class=\'webinar-time\'>
                  $temp_date
              </h3><!--BEGIN .entry-content-->
              <div class=\'webinar-content\'>
              <p><span class=\'purple-text\'>Event: </span>$temp_title</p>
              <p><span class=\'purple-text\'>Speaker: </span>$temp_speaker</p>
              <a href=\'$temp_link\' class=\'button-details\'>View Event Details<span class=\'et-icon\'></span></a>
              </div><!--END .entry-content-->
              </div><!--END .hentry-->
          </div></div>";
 }
 wp_reset_query();
 return $output;
}

结束

相关推荐

Echo HTML in custom shortcode

我写了一个我正在努力完善的短代码。它基本上只是得到一个书签列表并打印出来。但我需要根据定义的类型在其周围添加HTML。extract(shortcode_atts(array(\'cat\' => \'\', \'type\' => \'\'), $atts)); $bookmarks = get_bookmarks( array( \'orderby\' => \'name\', \'order\' => \'ASC\',