我正在尝试创建一个显示最新帖子的短代码。我使用了下面的代码作为短代码
function my_recent_posts_shortcode( $atts ) {
extract( shortcode_atts( array(
'limit' => 5
), $atts,'recent-posts' ) );
$q = new WP_Query(
array('posts_per_page'=>'.$limit.','post_type'=>'post')
);
$list = '';
while($q->have_posts()):$q->the_post();
$list .= '<div class="post">
<img class="img_border img_border_b img_fl" src="'.get_template_directory_uri().'/images/blog/01.jpg" alt="Post Image 1" />
<div class="post_content">
<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>
'.the_content().'
<a class="more" href="fullpost.html">More</a>
</div>
<div class="clear"></div>
</div>';
endwhile;
wp_reset_query();
return $list;
}
add_shortcode( 'recent-posts', 'my_recent_posts_shortcode' );之后,我已经创建了一个页面,并为此选择了默认的页面模板?这是我的page.php模板循环
<div id="templatemo_content" class="left">
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?Php the_content();?>
<?php endwhile; ?>
<?php endif; ?>
</div>以下是我的短代码
[recent-posts limit="3"]但是当我看到结果时,它会显示一个'pre‘标签。
请告诉我解决办法。
发布于 2014-05-12 16:36:56
the_content()正在输出您的内容,请在您的my_recent_posts_shortcode函数中用apply_filters('the_content',get_the_content())替换它,替换后将有效!!
问候
发布于 2014-12-03 22:30:46
检查所讨论的页面的编辑模式,在哪里安装了您的短代码。如果您使用的是wysiwyg编辑器,并且您处于“可视”模式,那么您的短代码可能具有预包装器。在“文本”模式下检查它。
<pre>[Blah_shortcode]</pre>https://stackoverflow.com/questions/23614167
复制相似问题