我正在尝试编写一个插件,该插件将获取页面ID并返回页面预览。这是我的代码:
function page_preview($atts,$pageid = null) {
extract(shortcode_atts(array(
"pageid" => '0'
), $atts));
$the_query = new WP_Query( 'page_id=' . $pageid . '' );
global $more;
$more = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content=get_the_content();
}
}
return '<div class="callout">' .
'<h4>' .
$title .
'</h4>' .
$thumbnail .
$content . '<a href="' .
get_the_permalink($pageid) .
'">Continue reading</a></div>';
}
add_shortcode( 'pagepreview', 'page_preview' );
并在wpadmin编辑器上调用它,如下所示: [pagepreview pageid = 11] [pagepreview pageid = 13] [pagepreview pageid = 8054] 即,这将显示每个页面ID的页面预览。
&#34;更多&#34;不起作用。
global $more;
$more = 0;
通常可以解决这个问题,但不是我的问题。谁能看到我做错了什么?谢谢。
答案 0 :(得分:1)
您正在获取完整内容,因为
$content=get_the_content();
未应用the_content()
过滤器,而$more=0
必须位于while循环内$the_query->the_post();
之后的一行上。将while循环更改为:
while ( $the_query->have_posts() ) {
$the_query->the_post();
$more=0;
$title=get_the_title();
$thumbnail=get_the_post_thumbnail( $pageid, 'preview' );
$content = apply_filters( 'the_content', get_the_content() );
$content = str_replace( ']]>', ']]>', $content );
}
请参阅WordPress Codex上的get_the_content和read more in pages,了解我的来源。