【问题标题】:WordPress: Get terms from authorWordPress:从作者那里获取条款
【发布时间】:2020-12-25 17:03:11
【问题描述】:

在作者存档页面上,我想显示作者发布的每个分类(在我的案例产品类别中)(在我的案例中是 WooCommerce 产品)。

为此,我使用以下代码:

$posts = get_posts( array('post_type' => 'product', 'posts_per_page' => -1, 'author' => $author_id) );
$author_categories = array();
//loop over the posts and collect the terms
foreach ($posts as $p) {
    $author_categories = wp_get_object_terms( $p->ID, 'product_cat');

    if ( ! empty( $author_categories ) && ! is_wp_error( $author_categories ) ){
        echo '<div class="d-flex flex-wrap">';
            foreach ($author_categories as $author_category) {
                //var_dump($t);
                $author_category_link   = get_term_link( $author_category );
                $author_category_name   = $author_categories[] = $author_category->name;

                echo '<a href="'.esc_url( $author_category_link ).'" class="p-2 p-lg-5 bg-light text-center">';
                echo $author_category_name;
                echo '</a>';
            }
        echo '</div>';
    }
}
wp_reset_postdata();

问题是,产品类别出现多次。我猜是该产品类别中的每个帖子。

有什么方法可以先收集条款,然后按产品类别中的帖子数排序后仅显示一次?

【问题讨论】:

    标签: php wordpress woocommerce categories custom-taxonomy


    【解决方案1】:

    您应该首先将所有类别收集到一个容器数组中。通过在向数组中添加类别时使用类别的 ID 或名称作为数组键,可以消除重复项。然后,您只需遍历结果数组以回显类别。

    // First just collect the distinct categories.
    $posts = get_posts( array('post_type' => 'product', 'posts_per_page' => -1, 'author' => $author_id) );
    $all_author_categories = array();
    foreach ($posts as $p) {
        $author_categories_for_p = wp_get_object_terms( $p->ID, 'product_cat');
        if ( ! empty( $author_categories_for_p ) && ! is_wp_error( $author_categories_for_p ) ){
            foreach ($author_categories_for_p as $author_category) {
                $all_author_categories[$author_category->name] = $author_category;  
            }
        }
    }
    
    // Then just loop over the collected categories and display them.
    echo '<div class="d-flex flex-wrap">';
    foreach($all_author_categories as $author_category) {
        $author_category_link   = get_term_link( $author_category );
        $author_category_name   = $author_categories[] = $author_category->name;
    
        echo '<a href="'.esc_url( $author_category_link ).'" class="p-2 p-lg-5 bg-light text-center">';
        echo $author_category_name;
        echo '</a>';
    }
    echo '</div>';
    

    备注:

    1. 当您有很多帖子时,此代码无法很好地扩展。您应该改用自定义查询。
    2. 我很确定最后不需要 wp_reset_postdata();,因为您没有更改全局 $post 对象。

    【讨论】:

    • 谢谢!问题是,我们有很多帖子,因此我看到了一些性能问题。
    • 我试过你的代码,但没有得到任何结果:-(
    • 它是直接在SO的编辑器窗口中编写的,所以可能并不完美。在第二个循环之前尝试var_dump()-ing $all_author_categories,看看它是否持有(正确的)值。
    • 现在我发现了问题。您需要将if ( ! empty( $author_categories ) &amp;&amp; ! is_wp_error( $author_categories ) ){这一行更改为if ( ! empty( $author_categories_for_p ) &amp;&amp; ! is_wp_error( $author_categories_for_p ) ){之后,代码就可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2015-11-18
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    相关资源
    最近更新 更多