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

如何在Wordpress的特定页面上仅显示特定类别?,wordpress,categories,Wordpress,Categories,我是wordpress开发的新手。我想知道如何在wordpress的特定页面上显示特定类别。我需要在我的项目中执行此操作。将类别列表显示为链接。 基本上,您必须调用此函数wp_list_categories()在您希望看到所列类别的位置 并使用此选项include,它接受要显示的类别ID列表 像这样 $args = array( 'hide_empty' => 0, //Show me all the categories, even the empty ones 'ord

我是wordpress开发的新手。我想知道如何在wordpress的特定页面上显示特定类别。我需要在我的项目中执行此操作。

将类别列表显示为链接。

基本上,您必须调用此函数
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>';
} 
$categories=获取类别(数组)(
'orderby'=>'name',
“订单”=>“ASC”,
'包括'=>'15,16,9'
) );
foreach($categories作为$category){
$category_link=sprintf(
'',
esc_url(获取分类链接($category->term_id)),
esc_attr(sprintf(_uu('View all posts in%s','textdomain'),$category->name)),
esc_html($category->name)
);
echo“”.sprintf(esc_html_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
echo“”.sprintf(esc_html_uuuuuuuu('Description:%s','textdomain'),$category->Description)。“

”; echo“”.sprintf(esc_html_uuuuuuuuu('Post Count:%s','textdomain'),$category->Count)。“

”; }
wp\u列表\u类别
get\u类别
WordPress函数
get\u categories()
wp\u list\u categories()
几乎相同。它们使用的参数几乎相同,但它们之间存在差异:

函数的作用是:返回与查询参数匹配的类别对象数组。
wp\u list\u categories()
将类别列表显示为链接。 WordPress开发者文档:

获取类别():


wp\u list\u categories():将类别列表显示为链接。

基本上,您必须调用此函数
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>';
} 
$categories=获取类别(数组)(
'orderby'=>'name',
“订单”=>“ASC”,
'包括'=>'15,16,9'
) );
foreach($categories作为$category){
$category_link=sprintf(
'',
esc_url(获取分类链接($category->term_id)),
esc_attr(sprintf(_uu('View all posts in%s','textdomain'),$category->name)),
esc_html($category->name)
);
echo“”.sprintf(esc_html_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
echo“”.sprintf(esc_html_uuuuuuuu('Description:%s','textdomain'),$category->Description)。“

”; echo“”.sprintf(esc_html_uuuuuuuuu('Post Count:%s','textdomain'),$category->Count)。“

”; }
wp\u列表\u类别
get\u类别
WordPress函数
get\u categories()
wp\u list\u categories()
几乎相同。它们使用的参数几乎相同,但它们之间存在差异:

函数的作用是:返回与查询参数匹配的类别对象数组。
wp\u list\u categories()
将类别列表显示为链接。 WordPress开发者文档:

获取类别():

wp\u list\u categories():

我就是这么做的

[product_categories ids="170, 189"]
您可以在类别的URL中获取类别ID。只需转到您的类别,在url中您将看到类似于“product_cat&tag_ID=165&post_type=product”的内容,ID=#是ID:P

这就是我所做的

[product_categories ids="170, 189"]

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

欢迎使用堆栈溢出。请注意,不鼓励询问一般帮助或建议的问题:请参阅。这不是一个代码编写服务,你应该已经研究过你的问题,并试图在发布之前解决它。请阅读并欢迎来到Stack Overflow。请注意,不鼓励询问一般帮助或建议的问题:请参阅。这不是一个代码编写服务,你应该已经研究过你的问题,并试图在发布之前解决它。请阅读和