dtrt2368 2018-08-10 08:36
浏览 32
已采纳

wp_dropdown_pages(仅显示带有自定义分类的页面)?

I want to echo the pages only with the custom taxonomy, but now i have drop down all pages what i have in custom post type.

<?php                                                       
 $post_type_object = get_post_type_object('property');                     
     if ($post) {
$parent_properties_dropdown_args =  array(
    'post_type'        => 'property',
    'property_status'  => 'condominium-villas-project', // <<-- Here is the (property_status - is the custom taxonomy and condominium-villas-project is the custom taxonomy tag)
    'selected'         => $prop_data->post_parent,
    'name'             => 'parent_id',
    'show_option_none'  => __('Not selected'),
    'echo'             => 0,);                    

                    $parent_properties_dropdown = wp_dropdown_pages($parent_properties_dropdown_args);

if (! empty($parent_properties_dropdown)) {
    ?>
    <div class="form-option parent-field-wrapper">
        <label for=""></label>
        <?php echo $parent_properties_dropdown; ?>
    </div>
    <?php

} }

But im anyway get all pages in custom post type 'property'. i need only 'property' with the taxonomy to show.

example: now -> - Property 1 - 'property_status' 'rent' - Property 2 - 'property_status' 'sale' - property 3 - 'property_status' 'condominium-villas-project'

i want to get only - property 3 - 'property_status' 'condominium-villas-project'

  • 写回答

2条回答 默认 最新

  • douhoulei4706 2018-08-10 10:40
    关注

    Using what I think is your exact code (I am still confused by your use of $prop_data) you could use the following code to get what (I think) you are looking for. Add the code to your theme's function.php file, a .php file it requires, or one of the .php files of a plugin you might implement:

    <?php
    
    class wpse_51782342 {
        static function on_load() {
            add_filter( 'get_pages',  [ __CLASS__, '_get_pages' ], 10, 2 );
        }
    
        static function _get_pages( $pages, $args ) {
            if ( isset( $args[ 'property_status' ] ) ) {
                $pages = get_posts(array(
                    'post_type'       => 'property',
                    'posts_per_page'  => -1,
                    'property_status' => $args[ 'property_status' ],
                    'include'         => wp_list_pluck( $pages, 'ID' ),
                ));
            }
            return $pages;
        }
    }
    wpse_51782342::on_load();
    

    The 'get_pages' filter hook runs at the end of get_pages() which is called by wp_dropdown_pages(). In that hook the code looks for the argument you passed named 'property_status' to decide if it should modify behavior. This is an important technique because it ensures that the same args will always return the same results and are not dependent on something like the current post ID or URL. Following this principle will usually reduce the number of bugs you have to fix in your project.

    If the argument 'property_status' is found the $args array the code uses its value to call get_posts() to return a list of posts that have been assigned the value of property_status that you passed to wp_dropdown_pages().

    Finally the code limits the get_posts() query to the $post->IDs from $pages found by the query already run by wp_dropdown_pages(). This should result in a dropdown showing just the pages you prefer.

    And for reference, here is the code in single.php to test out the above code, after I entered examples for property and property status, of course.

    wp_dropdown_pages(array(
        'post_type'        => 'property',
        'property_status'  => 'condominium-villas-project',
        'selected'         => $post->ID,
        'name'             => 'ID',
        'show_option_none'  => __('Not selected'),
        'echo'             => 1,
    ));
    

    Hope this helps?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?