我在函数中创建了一个自定义的post类型。php:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type(
'specialities',
array(
'labels' => array(
'name' => __( 'Besonderheiten' ),
'singular_name' => __( 'Besonderheit' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail')
)
);
}
然后我在wp管理部分添加了一些帖子。
我查询了所有帖子:
$args = query_posts( array(
'post_type' => 'specialities',
'posts_per_page' => -1
));
$query = new WP_Query($args);
为了确保$查询不是空的,我var_dumped整件事-
所以我尝试循环这个查询:
<?php if(!empty($query)){ ?>
<div class="slideshow clearfix">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="specialities clearfix"><?php echo the_title(); ?></div>
<?php endwhile; ?>
</div>
<?php } ?>
在web inspector中,我可以看到div(幻灯片clearfix),但没有子div(specialities clearfix)。。为什么?
因此,如果是var_转储:
对象(WP_Query)#6956(48){["查询"]=
query_posts()创建全局$wp_query。$查询是一个不同的WP_Query对象。另外,构造函数参数在我看来是错误的。
我想我知道您有一个格式错误的$query对象。$query的var_dump中的post不是查询的结果,而是您在构造函数中传递的错误参数。您不需要query\u posts(),只需将参数直接输入到新的WP\u查询中即可。
$args = array(
'post_type' => 'specialities',
'posts_per_page' => -1
);
$query = new WP_Query($args);
在查询中使用两个函数query_posts()和WP_Query()。
$args = query_posts( array(
'post_type' => 'specialities',
'posts_per_page' => -1
));
$query = new WP_Query($args);
query_posts()和WP_query()是两个不同的函数。
您可以使用以下代码
$args = array(
'post_type' => 'specialities',
'posts_per_page' => -1
);
$query = new WP_Query($args);
参考-https://codex.wordpress.org/User:JamesVL/query_posts
-https://codex.wordpress.org/Class_Reference/WP_Query