使用函数get_terms()获取友情链接分类

制作友情链接的时候,需要获取友情链接的分类,WordPress使用get_terms()可以获取友情链接分类,定制个性化友情链接页面。get_terms()同时也可以获取文章分类、链接分类和自定义分类,get_terms()代码如下:

<?php get_terms( $taxonomies, $args ) ?>

$taxonomies
该参数是你想要获取的分类类别,可选值包括:”category”,”link_category”,”my_taxonomy”,他们分别代表文章分类、链接分类以及你自定义的分类,其中my_taxonomy是你自定义的分类名称。

$args
该参数是可选参数,具体说明可以查看:get_terms()

列出所有不带链接的链接分类:

<?php
$terms = get_terms("link_category");
 $count = count($terms);
 if ( $count > 0 ){
     echo "<ul>";
     foreach ( $terms as $term ) {
       echo "<li>" . $term->name . "</li>";

     }
     echo "</ul>";
 }
?>

列出所有带上链接的自定义分类:

<?php
$args = array( 'taxonomy' => 'my_term' );

$terms = get_terms('my_term', $args);

$count = count($terms); $i=0;
if ($count > 0) {
    $cape_list = '<p class="my_term-archive">';
    foreach ($terms as $term) {
        $i++;
     $term_list .= '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
     if ($count != $i) $term_list .= ' · '; else $term_list .= '</p>';
    }
    echo $term_list;
}
?>

函数get_terms() 位于 wp-includes/taxonomy.php

发表评论

游客欢迎您123