【问题标题】:Why can't I loop through a custom post type and category in WordPress为什么我不能遍历 WordPress 中的自定义帖子类型和类别
【发布时间】:2016-11-01 10:48:10
【问题描述】:

所以我正在尝试输出属于自定义帖子类型服务和特定类别的帖子名称。

但是我使用的代码是输出所有帖子类型的类别

    $args = array(
                    'post_type' => 'service',
                    'cat' => $cat
                );

                $query = new WP_Query( $args );

                echo '<pre>';
                print_r($query);
                echo '</pre>';

            ?>

            <div id="subCatBox">
                <ul id="subCatlist" class="list">

                    <?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

                        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

                    <?php endwhile; else : ?>
                    <?php endif; ?>

                </ul>
            </div>

这里是查询对象的输出

WP_Query Object
(
[query] => Array
    (
        [post_type] => service
        [cat] => 4
    )

[query_vars] => Array
    (
        [post_type] => any
        [cat] => 4
        [error] => 

我不明白为什么在查询中 post_type 是服务,但在 query_vars 中却是任何。任何帮助将不胜感激

【问题讨论】:

  • 尝试 get_the_category( int $id = false ) 获取帖子类别。
  • 我已经得到了这样的类别 $cat = get_the_category()[0]->cat_ID;

标签: php wordpress


【解决方案1】:

我已经解决了。以防万一这有助于我使用 category__in 而不是 cat 的其他人

 $query = new WP_Query(array(
    'post_type' => 'post',
    'category__in' => $cat
 ));

【讨论】:

    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多