首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加显示多个时间的快捷代码标题

添加显示多个时间的快捷代码标题
EN

Stack Overflow用户
提问于 2015-09-16 18:09:56
回答 1查看 65关注 0票数 2

嗨,我的代码如下,用于在帖子中添加快捷代码。当我添加两次短代码时,它显示了我在代码“最近的帖子”中添加的两次标题,有没有办法只显示这个标题一次?

代码语言:javascript
复制
/*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;
}

EN

回答 1

Stack Overflow用户

发布于 2015-09-17 03:19:31

定义一个全局变量来检测是否已经添加了title。

代码语言:javascript
复制
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;
        }

希望这能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32605643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档