【问题标题】:Wordpress get_pages based on page categoryWordpress get_pages 基于页面类别
【发布时间】:2014-10-27 14:47:32
【问题描述】:

我需要 get_pages 或根据它的类别在 WP 中获取页面数组

我知道 WP 没有页面上的类别,但我在 functions.php 中使用它来获取页面上的类别。

    add_action('admin_init', 'reg_tax');

    function reg_tax() {
        register_taxonomy_for_object_type('category', 'page');
        add_post_type_support('page', 'category');
    }

现在我需要在 get_pages 或 WP_Query 中使用这些类别来进入具有类别的页面。

     <div class="productNav">

                <ul>

                <?php

                $product_page_args = array(

                    'post_type' => 'page',
                    'order' => 'ASC',
                    'orderby' => 'menu_order',
                    'child_of' => $post->ID,
                    'category_name' => 'pillar-product'
                );

                //$product_pages = get_pages($product_page_args);

                $product_pages = new WP_Query($product_page_args);



                foreach ($product_pages as $product_page){  

                ?>

                    <li><?php echo $product_page->post_title; ?></li>    

                <?php
                }
                ?>

                </ul>

            </div>

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    这个答案在2021 中不再适用。这是一个过时的答案,包括所有页面。请使用the other answer

    试试这个:(不会在 wordpress 上工作 5.8

    $product_page_args = array(
    
                    'post_type' => 'page',
                    'order' => 'ASC',
                    'orderby' => 'menu_order',
                    'child_of' => $post->ID,
                    'taxonomy' => 'category',
                            'field' => 'slug',
                            'term' => 'pillar-product'
                );
    

    【讨论】:

      【解决方案2】:

      此问题的已接受答案将不再有效,并包括所有页面,因为 'taxonomy''field''term' 必须位于位于 'tax_query' 数组内的数组内,此查询才能工作.

      有关如何根据特定类别查询wordpress页面的更全面的答案,请参见以下链接:

      How to query pages based on specific categories

      这是此问题的正确答案,已在 wordpress 5.8 上进行了全面测试。

      $product_page_args = array(
        'post_type' => 'page',
        'order' => 'ASC',
        'orderby' => 'menu_order',
        'child_of' => $post->ID,
        'tax_query' => array(
          array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array('pillar-product'),
          )
        )
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-17
        • 1970-01-01
        • 2017-07-07
        • 2011-04-26
        • 1970-01-01
        • 1970-01-01
        • 2013-03-10
        • 1970-01-01
        相关资源
        最近更新 更多