Php WordPress自定义post类型数组未按Expected格式工作
我正在使用这个代码Php WordPress自定义post类型数组未按Expected格式工作,php,arrays,wordpress,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' ); $pos
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',
…这就是我所期待的
但问题是,当我尝试在循环中使用包含的帖子类型()
时,仅显示具有这些帖子类型的帖子,如下所示:
$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' => included_post_types(),
…但它不起作用
如果我试试这个
'post_type' => array('clothes', 'shoes', 'jackets', ),
…它可以工作,但我需要能够使用函数名
任何建议都会有帮助。请用您的代码替换以下代码。这应该是有帮助的
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;
}
并替换下面显示帖子的代码
$post_type_list = include_post_types();
$sitemap_post_args = array(
'post_type' => $post_type_list,
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);
太好了,谢谢!这是有道理的。我需要升级我的PHP游戏。
$post_type_list = include_post_types();
$sitemap_post_args = array(
'post_type' => $post_type_list,
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);