Wordpress - post_category не показывает результат - PullRequest
0 голосов
/ 22 мая 2019

Я пытаюсь создать небольшой обзор последних сообщений на моей странице.Все хорошо работает с запросом "wp_get_recent_posts".Теперь я пытался добавить некоторые значки в заголовок, но он всегда не дает мне результата, как только я пытаюсь получить 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;

                        ?>

1 Ответ

0 голосов
/ 22 мая 2019

Объект post не имеет свойства post_category: https://codex.wordpress.org/Class_Reference/WP_Post

Вы можете использовать функцию get_the_category с идентификатором записи (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;
                        ?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...