WordPress中的简码问题

达米尔

我已经在WordPress中创建了一个用于调用最新博客文章的简码。我裹着标题和内容<h2><p>,但这没有被应用。正在生成html,但在我想要的位置没有标签。我写错了什么?

这是我的代码:

<?php 

//blog posts shortcode

function my_recent_post()
 {
  global $post;

  $html = "";

  $my_query = new WP_Query( array(
       'post_type' => 'post',
       'cat' => '4',
       'posts_per_page' => 1
  ));

  if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

       $html .= "<span>" . the_post_thumbnail( 'medium', array( 'class' => 'img-responsive') ) . "</span>";
       $html .= "<h2>" . the_title() . "</h2>";
       $html .= "<p>" . the_excerpt() . "</p>";

  endwhile; endif;

  return $html;
 }
 add_shortcode( 'blog', 'my_recent_post' );    
 ?>
斯坦尼米尔·斯托亚诺夫(Stanimir Stoyanov)

问题是您使用的是打印html的函数,而不是返回它。试试这个

//blog posts shortcode
add_shortcode( 'blog', 'my_recent_post' );    

function my_recent_post() {
    global $post;

    $html = "";

    $my_query = new WP_Query( array(
        'post_type' => 'post',
        'cat' => '4',
        'posts_per_page' => 1
    ));

   if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

       $html .= "<span>" . get_the_post_thumbnail( $post->ID, 'medium', array( 'class' => 'img-responsive') ) . "</span>";
       $html .= "<h2>" . get_the_title() . "</h2>";
       $html .= "<p>" . get_the_excerpt() . "</p>";

  endwhile; endif;

  return $html;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章