Wordpress - post_category 没有显示任何结果

Wordpress - post_category shows no result

我正在尝试创建一个关于我页面最新 post 的小概览。 "wp_get_recent_posts" 查询一切正常。现在我试图在标题中添加一些图标,但它总是没有显示任何结果,只要我尝试获取 post 的 post_category .

如果尝试将 $args 'category' => 更改为 '1,2,3,4,...' 但它没有帮助。

非常感谢任何建议。我的代码:

                    <?php

                        $args = array(
                            'numberposts' => 5,
                            'offset' => 0,
                            'category' => '',
                            'orderby' => 'post_date',
                            'order' => 'DESC',
                            'include' => '',
                            'exclude' => '',
                            'meta_key' => '',
                            'meta_value' =>'',
                            'post_type' => 'post',
                            'post_status' => 'draft, publish, future, pending, private',
                            'suppress_filters' => true
                        );

                        $recent_posts = wp_get_recent_posts( $args, ARRAY_A );

                        foreach($recent_posts as $post):
                            if($post['post_category'] == 1):
                                echo "<p><i class=\"fas fa-book\">&nbsp;&nbsp;</i>{$post['post_title']}<br></p>";
                            elseif($post['post_category'] == 2):
                                echo "<p><i class=\"fas fa-desktop\">&nbsp;&nbsp;</i>{$post['post_title']}<br></p>";
                            endif;
                            echo $post['post_category']; //Debug, no output.
                            echo $post['post_title']; //Debug, output: "example post"
                            echo $post['post_date']; //Debug, output: "2019-05-21"
                        endforeach;

                        ?>

post 对象没有 post_category 属性: https://codex.wordpress.org/Class_Reference/WP_Post

您可以将 get_the_category 函数与 post ID (https://developer.wordpress.org/reference/functions/get_the_category/) 一起使用:

<?php    

                        $args = array(
                            'numberposts' => 5,
                            'offset' => 0,
                            'category' => '',
                            'orderby' => 'post_date',
                            'order' => 'DESC',
                            'include' => '',
                            'exclude' => '',
                            'meta_key' => '',
                            'meta_value' =>'',
                            'post_type' => 'post',
                            'post_status' => 'draft, publish, future, pending, private',
                            'suppress_filters' => true
                        );

                        $recent_posts = wp_get_recent_posts( $args, ARRAY_A );

                        foreach($recent_posts as $post):

                            $categories = get_the_category($post['ID']);

                            foreach ($categories as $category):
                                if ($category->cat_name == 'firstcategory'):
                                    //first category found                                  
                                    var_dump($category->cat_name);
                                endif;
                                if ($category->cat_name == 'secondcategory'):
                                    //second category found
                                    var_dump($category->cat_name);
                                endif;
                            endforeach;

                        endforeach;
                        ?>