首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含作为css类的链接列表类别

包含作为css类的链接列表类别
EN

Stack Overflow用户
提问于 2012-07-25 22:57:23
回答 1查看 424关注 0票数 0

我想在我的wordpress链接列表中呈现一些额外的css类,特别是我想将链接类别呈现为css类,例如:

代码语言:javascript
复制
link       : http://www.foobar.com/
gategories : friends, colleagues
name       : Foo Bar

当前呈现为:

代码语言:javascript
复制
<a href="http://www.foobar.com/" target="_blank">Foo Bar</a>

但我希望它呈现为:

代码语言:javascript
复制
<a href="http://www.foobar.com/" target="_blank" class="friends colleagues">Foo Bar</a>

我知道您使用以下函数来构建链接列表,但我不知道如何修改它来做我需要的事情:

代码语言:javascript
复制
function wp_list_bookmarks($args = '') {
    $defaults = array(
        'orderby' => 'name', 'order' => 'ASC',
        'limit' => -1, 'category' => '', 'exclude_category' => '',
        'category_name' => '', 'hide_invisible' => 1,
        'show_updated' => 0, 'echo' => 1,
        'categorize' => 1, 'title_li' => __('Bookmarks'),
        'title_before' => '<h2>', 'title_after' => '</h2>',
        'category_orderby' => 'name', 'category_order' => 'ASC',
        'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
        'category_after' => '</li>'
    );

    $r = wp_parse_args( $args, $defaults );
    extract( $r, EXTR_SKIP );

    $output = '';

    if(1) {
        //output one single list using title_li for the title
        $bookmarks = get_bookmarks($r);

        if ( !empty($bookmarks) ) {
            if ( !empty( $title_li ) ){
                $output .= str_replace(array('%id', '%class'), array("linkcat-$category", $class), $category_before);
                $output .= "$title_before$title_li$title_after\n\t<ul class=\"xoxo blogroll $category\">\n";
                $output .= _walk_bookmarks($bookmarks, $r);
                $output .= "\n\t</ul>\n$category_after\n";
            } else {
                $output .= _walk_bookmarks($bookmarks, $r);
            }
        }
    }

    $output = apply_filters( 'wp_list_bookmarks', $output );

    if ( !$echo )
        return $output;
    echo $output;
}

?>

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-28 10:20:07

首先,你不应该修改Wordpress的核心函数。然而,在书签的例子中,我并不真的责怪你想要这样做。他们有点让人头疼。

我会使用get_bookmarks()从头开始构建它。下面是一个有效的示例:

代码语言:javascript
复制
foreach(get_bookmarks() as $bm)
{
    $terms = get_the_terms($bm->link_id, 'link_category');
    $classes = array('wp_link');

    if($terms)
    foreach($terms as $term)
        $classes[] = $term->slug;

    echo '<a class="'.implode(' ', $classes).'" href="'.$bm->link_url.'"'.($bm->link_target ? 'target="'.$bm->link_target.'"' : '').'>'.$bm->link_name.'</a><br/>';
}

只需将它放在模板中您想要生成书签的任何位置。或者,您可以将其包装在自定义函数调用中,然后从模板中调用该函数。

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

https://stackoverflow.com/questions/11652630

复制
相关文章

相似问题

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