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'
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题