这是模板中的短代码:
function shortcode_frame_left( $atts, $content = null)
{
return \'<span class="frame alignleft">\'. do_shortcode($content) . \'</span>\';
}
add_shortcode(\'frame_left\', \'shortcode_frame_left\');
以下是如何在帖子内容中使用:
[frame_left] <a href="YOUR-URL"><img src="YOUR-URL" /></a> [/frame_left]
我正在尝试在模板文件中使用此短代码来显示帖子缩略图。我的尝试:
if (has_post_thumbnail()) {
apply_filters( \'the_content\', "[frame_left]".the_post_thumbnail()."[/frame_left]");
}
以及
if (has_post_thumbnail()) {
$thumbnail = \'[frame_left]\'. the_post_thumbnail() . \'[/frame_left]\';
echo do_shortcode("$thumbnail");
}
我做错了什么?
最合适的回答,由SO网友:Jan Fabry 整理而成
the_post_thumbnail()
回显缩略图,不返回任何内容。您可能想使用get_the_post_thumbnail()
以字符串形式返回。您的代码当前与此等效:
if (has_post_thumbnail()) {
// Echo the thumbnail
the_post_thumbnail();
// Apply the filter but do nothing with the result.
apply_filters( \'the_content\', "[frame_left]"."[/frame_left]");
}
if (has_post_thumbnail()) {
// Echo the thumbnail
the_post_thumbnail();
$thumbnail = \'[frame_left]\' . \'[/frame_left]\';
// Echo the <span> with an empty content
echo do_shortcode("$thumbnail");
}