Php 添加显示多个时间的短代码标题

Php 添加显示多个时间的短代码标题,php,wordpress,Php,Wordpress,嗨,下面是我在帖子中添加快捷码的代码。当我添加两次短代码时,它会显示两次标题,就像我在代码“最近的帖子”中添加的一样。有没有办法只显示一次标题的顶部意思 /*shortcode start*/ add_shortcode( 'recent-posts', 'PL_recent_posts' ); function PL_recent_posts( $atts ) { extract( shortcode_atts( array( 'numbers' => '5', 'o

嗨,下面是我在帖子中添加快捷码的代码。当我添加两次短代码时,它会显示两次标题,就像我在代码“最近的帖子”中添加的一样。有没有办法只显示一次标题的顶部意思

/*shortcode start*/
add_shortcode( 'recent-posts', 'PL_recent_posts' );

function PL_recent_posts( $atts  ) {
extract( shortcode_atts( array(
    'numbers' => '5',
    'order' => 'ASC',

), $atts ) );

$rposts = new WP_Query( array( 'posts_per_page' => $numbers, 'orderby' => 'date' , 'colorss' =>     $color ) );


if ( $rposts->have_posts() ) {
    $html = '<h3>Recent Posts</h3><ul class="recent-posts">';
    while( $rposts->have_posts() ) {
        $rposts->the_post();
        $html .= sprintf(
            '<li><a href="%s" title="%s">%s</a></li>',
            get_permalink($rposts->post->ID),
            get_the_title(),
            get_the_title()
        );
    }
    $html .= '</ul>';
}
wp_reset_query();

return $html;
}
/*短码开始*/
添加_短码('recentposts','PL_recentposts');
职能部门最近的职位($atts){
提取(短码)附件(数组)(
“数字”=>“5”,
“订单”=>“ASC”,
)美元(附件);;
$rposts=新的WP_查询(数组('posts_per_page'=>$number,'orderby'=>date','colorss'=>$color));
如果($rposts->have_posts()){
$html='recentposts
    '; 而($rposts->have_posts()){ $rposts->the_post(); $html.=sprintf( “
  • ”, 获取永久链接($rposts->post->ID), 获取标题(), 获取标题() ); } $html.='
'; } wp_reset_query(); 返回$html; }

定义一个全局变量以检测是否已添加标题

function PL_recent_posts( $atts ) { 
    global $title_added;
    ...
    if ( $rposts->have_posts() ) {
        if ( $title_added ) {
            $html = '<ul class="recent-posts">';
        } else {
            $html = '<h3>Recent Posts</h3><ul class="recent-posts">';
            $title_added = true;
        }
函数PL\u最近发布($atts){
新增全球$title_;
...
如果($rposts->have_posts()){
如果(增加标题){
$html='
    '; }否则{ $html='recentposts
      '; $title_added=true; }

希望这对您有所帮助。

重新格式化您的代码。将此$html='Recent Posts
    ;放在上面的if语句无法重现此问题。上面的代码可以在干净的wp安装中正常工作。它可以正常工作,但问题是,当我在“Recent Posts”中两次添加短代码时也显示两次,就像你们在屏幕上看到的那个样。我需要的是。我需要多次使用短代码,不想多次出现“最近的帖子”。