doujia2463 2018-06-30 01:35
浏览 92
已采纳

WordPress自定义帖子类型数组无法按预期工作

I'm using this code...

function include_post_types() {
$post_types = get_post_types( array (
'show_ui'       => true
), 
'objects' );
$return = '';
foreach ( $post_types as $post_type ) {
    $my_sitemap_options = get_option( 'my_sitemap_settings' ); 
    $post_type_name = $post_type->name;
    if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) { 
        $the_excluded = $post_type_name;
        $return .= "'" . $the_excluded . "', ";
    } 
}
return $return;

}

... to return a list of custom post types that I have selected from an options page. This works fine, and if I do this...

echo included_post_types();

...I see this...

'clothes', 'shoes', 'jackets', 

...which is what I excpected.

But the problem is when I try to use included_post_types() in a loop to only show posts with those post types, like this:

$sitemap_post_args = array(
        'post_type'         => array(included_post_types()),
        'posts_per_page'    => -1,
        'orderby'           =>'post_type',
        'order'             =>'asc'
    );

    $loop = new WP_Query($sitemap_post_args);
    global $post_type;
    global $post;

    echo '<ul>';
    $last_post_type = '';
    while($loop->have_posts()): $loop->the_post(); 
        $current_post_type = $post->post_type;
        $current_post_type_object = get_post_type_object( $current_post_type );
        if($current_post_type != $last_post_type) echo "<br /><li><strong>" . $current_post_type_object->labels->name . "</strong></li>";?>
            <li><a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a></li>
        <?php echo "
";
        $last_post_type = $current_post_type;
    endwhile;
    wp_reset_query();
    echo '</ul>';

It simply doesn't display anything on my page, but it doesn't throw an error either.

I'm almost certain the problem is this line:

'post_type' => array(included_post_types()),

I even tried it like this...

'post_type' => included_post_types(),

...but it did not work.

If I try this...

'post_type' => array('clothes', 'shoes', 'jackets', ),

...it works, but I need to be able to use the function name.

Any suggestions would be helpful.

  • 写回答

1条回答 默认 最新

  • dongyi3776 2018-06-30 10:07
    关注

    please replace below code with yours. It should be help.

    function include_post_types() {
    $post_types = get_post_types( array (
    'show_ui'       => true
    ), 
    'objects' );
    $return = array();
    foreach ( $post_types as $post_type ) {
        $my_sitemap_options = get_option( 'my_sitemap_settings' ); 
        $post_type_name = $post_type->name;
        if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) { 
            $the_excluded = $post_type_name;
            $return[] = $the_excluded;
        } 
    }
    return $return;
    }
    

    and also replace below code that show posts

    $post_type_list = include_post_types();
    $sitemap_post_args = array(
        'post_type'         => $post_type_list,
        'posts_per_page'    => -1,
        'orderby'           =>'post_type',
        'order'             =>'asc'
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来