【问题标题】:Wordpress - post_category shows no resultWordpress - post_category 显示没有结果
【发布时间】:2019-10-08 15:52:04
【问题描述】:

我正在尝试对我页面的最新帖子创建一个小概述。一切都适用于“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;

                        ?>

【问题讨论】:

  • post 对象似乎没有 post_category 属性:codex.wordpress.org/Class_Reference/WP_Post。这个 post_category 到底是什么?什么样的类别?请举一个类别的例子
  • 确实,post_category 属性在 post 中不存在 - 谢谢。有没有办法,我如何访问单个帖子的类别来使这个脚本工作?

标签: php wordpress templates posts


【解决方案1】:

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

您可以使用带有帖子 ID (https://developer.wordpress.org/reference/functions/get_the_category/) 的 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;
                        ?>

【讨论】:

  • 感谢您提供此解决方案!不幸的是,一旦我执行“var_dump($category->name)”,我只收到 NULL NULL NULL NULL NULL。此外,如果我删除“名称”并仅执行“var_dump($category)”,我会收到 5 个空数组。这种方法对你有用吗?或者可能是因为 $post “foreach”中的“foreach”?不幸的是,我无法发布另一个代码 sn-p。
  • 分类循环前 var_dump($categories) 的输出是什么?
  • 对不起,出现错误:您需要像这样获取 id:$post['ID']。我还发布了对我有用的完整代码...
猜你喜欢
  • 1970-01-01
  • 2013-12-18
  • 2012-04-08
  • 2018-07-08
  • 1970-01-01
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多