如何在Wordpress中的特定页面上仅显示特定类别?

时间:2017-08-17 07:40:21

标签: wordpress categories

我是wordpress开发的新手。我想知道如何在wordpress中的特定页面上显示特定类别。我需要在我的项目中这样做。

2 个答案:

答案 0 :(得分:1)

会将类别列表显示为链接。

基本上,您必须在要查看列表类别的位置调用此函数wp_list_categories();

并使用此选项include接受要显示的类别ID列表。

喜欢这个

$args = array(
    'hide_empty' => 0, //Show me all the categories, even the empty ones
    'orderby' => 'count', //which accepts a string. You can pick from the following options: ID to order the categories by their ID (no, really?), name to sort them alphabetically (the default value), slug to sort them in the alphabetical order of their slugs, and count to order by the number of posts they contain.
    'order' => 'DESC', //The chosen order can be reversed by setting DESC (descending) as a value for the order option (by default this option is set to ASC (ascending)).
    'include' => '15,16,9'
);

wp_list_categories($args);

使用get_categories();功能

显示类别的另一种方法
$categories = get_categories( array(
    'orderby' => 'name',
    'order'   => 'ASC',
    'include' => '15,16,9'
) );

foreach( $categories as $category ) {
    $category_link = sprintf( 
        '<a href="%1$s" alt="%2$s">%3$s</a>',
        esc_url( get_category_link( $category->term_id ) ),
        esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
        esc_html( $category->name )
    );

    echo '<p>' . sprintf( esc_html__( 'Category: %s', 'textdomain' ), $category_link ) . '</p> ';
    echo '<p>' . sprintf( esc_html__( 'Description: %s', 'textdomain' ), $category->description ) . '</p>';
    echo '<p>' . sprintf( esc_html__( 'Post Count: %s', 'textdomain' ), $category->count ) . '</p>';
} 

wp_list_categoriesget_categories之间的差异 WordPress函数get_categories()wp_list_categories()几乎相同。他们使用几乎相同的参数,但它们之间存在差异:

get_categories()函数返回与查询参数匹配的类别对象数组。 wp_list_categories()会将类别列表显示为链接。 WordPress开发人员文档:

get_categories(): http://codex.wordpress.org/Function_Reference/get_categories

wp_list_categories(): http://codex.wordpress.org/Function_Reference/wp_list_categories

答案 1 :(得分:0)

这就是我所做的

[product_categories ids="170, 189"]

您可以在类别的URL中获取类别ID。只需转到您的类别,在url中,您会看到类似“ product_cat&tag_ID = 165&post_type = product”的信息,Id =#是ID:P

相关问题