Php WordPress中的短码问题
我在WordPress中创建了一个短代码,用于调用最新的博客帖子。 我已经用Php WordPress中的短码问题,php,wordpress,Php,Wordpress,我在WordPress中创建了一个短代码,用于调用最新的博客帖子。 我已经用和包装了标题和内容,但这并不适用。html正在生成,但没有我想要的标记。我写错了什么 这是我的代码: <?php //blog posts shortcode function my_recent_post() { global $post; $html = ""; $my_query = new WP_Query( array( 'post_type' => 'post
和
包装了标题和内容,但这并不适用。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' );
?>
问题在于您使用的函数是打印html,而不是返回html。 试试这个
//博客文章短代码
添加快捷码(“博客”、“我最近的帖子”);
函数my_recent_post(){
全球$员额;
$html=“”;
$my\u query=新的WP\u查询(数组(
“post_type”=>“post”,
“cat”=>“4”,
“每页帖子”=>1
));
如果($my_query->have_posts()):而($my_query->have_posts()):$my_query->the_post();
$html.=“”。获取帖子缩略图($post->ID,'medium',数组('class'=>'img responsive'))”;
$html.=“”。获取标题();
$html.=“”。获取摘录()。“”;
endwhile;endif;
返回$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;
}