wordpress API

wordpress自定义taxonomy和term,分类列表新方法

日期:2017-05-14 阅读:1902

dmandwp系统 - wordpress系统和DM系统区块建站>>


进入网易云课堂播放
    |    更多视频教程>

https://codex.wordpress.org/Taxonomies

https://www.sitepoint.com/custom-wordpress-taxonomies/

 

付费插件:
https://wp-types.com/ (太大了,不好用)

-----------------

https://codex.wordpress.org/Function_Reference/get_term_by  

https://codex.wordpress.org/Function_Reference/get_term  

---------------

// Get term by name ''news'' in Categories taxonomy.
$category = get_term_by('name', 'news', 'category')

// Get term by name ''news'' in Tags taxonomy.
$tag = get_term_by('name', 'news', 'post_tag')

// Get term by name ''news'' in Custom taxonomy.
$term = get_term_by('name', 'news', 'my_custom_taxonomy')

// Get term by name ''Default Menu'' from theme's nav menus.
// (Alternative to using wp_get_nav_menu_items)
$menu = get_term_by('name', 'Default Menu', 'nav_menu');

-----------
By id (term_id, not post_id):

// Get term by id (''term_id'') in Categories taxonomy.
get_term_by('id', 12, 'category')

-------------------------

https://developer.wordpress.org/reference/functions/get_the_terms

get_the_terms( int|object $post, string $taxonomy )
比如:$terms = get_the_terms( get_the_ID(), 'category' ); -- 因为一个post可能不只一个分类。所以是复数。

----------------

the category: 这个不是category的taxonomy,而是指当前的term.可能是多个。 

https://developer.wordpress.org/reference/functions/the_category/

get_the_category_list() 会产生带html的分类列表。
或用:
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
    foreach( $categories as $category ) {
        $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
    }
    echo trim( $output, $separator );
}

------------------------

取当前分类下的term,不是post下的term:

https://codex.wordpress.org/Function_Reference/get_category

$thisCat = get_category(get_query_var('cat'));
echo $thisCat->term_id;
echo $thisCat->parent;  ---通过这个,然后可以区分是产品还是新闻。从而给不同的列表。

但还是用 pod插件吧。 https://wordpress.org/plugins/pods/

 

 

<<点击返回