WordPress自定义帖子类型数组无法正常工作

时间:2018-06-30 01:35:30

标签: php arrays wordpress

我正在使用此代码...

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;

}

...返回我从选项页面选择的自定义帖子类型的列表。效果很好,如果我这样做...

echo included_post_types();

...我看到了...

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

...这是我所期待的。

但是问题是当我尝试在循环中使用included_post_types()仅显示具有这些帖子类型的帖子时,如下所示:

$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 "\n";
        $last_post_type = $current_post_type;
    endwhile;
    wp_reset_query();
    echo '</ul>';

它根本不会在我的页面上显示任何内容,但也不会引发错误。

我几乎可以肯定问题出在这行:

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

我什至尝试过这种方式...

'post_type' => included_post_types(),

...但是没有用。

如果我尝试这个...

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

...可以,但是我需要能够使用函数名。

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

请用以下代码替换您的代码。应该有帮助。

<Script type="text/javascript">
        $(document).on('click', '.edit', function(){
           var id = $(this).attr("id");
            $.ajax({
             type:"post",
             url:"edit.php",    
             data:{id:id},
             success:function(data){
               $('.tampil').html(data);
             }
            });
          });
        </script>

并替换下面显示帖子的代码

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;
}