为什么无法使用query_posts检索自定义post_type?

时间:2012-09-09 14:04:06

标签: wordpress wordpress-theming

我在function.php中声明了一个自定义帖子类型,如下所示:

register_post_type('movies', array(
    'label' => __("Movies", TEMPLATENAME),
    'singular_label' => __("Movie", TEMPLATENAME),
    'public' => true,
    'show_ui' => true,
    'exclude_from_search' => true,
    'publicly_queryable' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'permalink_epmask' => EP_PERMALINK,
    'rewrite' => array('slug' => 'cine', 'with_front'=> false),
    'query_var' => 'movies',
    'show_in_nav_menus' => true,
    'menu_position' => 20,
    'description' => __("These Movies will be automatically displayed on the « Movies » page.", TEMPLATENAME),
    'labels' => array('add_new_item' => __( "Ajouter une movie", TEMPLATENAME), 'add_new' => __( "Ajouter une movie", TEMPLATENAME), 'edit_item' => __( "Modifier la movie", TEMPLATENAME), 'new_item' => __( "Nouvelle movie", TEMPLATENAME), 'view_item' => __( "Voir la movie", TEMPLATENAME), 'search_items' => __( "Chercher dans toutes les movies", TEMPLATENAME), 'not_found' => __( "Not Found", TEMPLATENAME), 'not_found_in_trash' => __( "No movie found in trash", TEMPLATENAME)),
    'supports' => array('title', 'editor', 'thumbnail',  'excerpt','custom-fields')
    //, 'register_meta_box_cb' => 'movies_box_fields'
));

然后我尝试在这样的模板文件中检索它们:

<?php query_posts(array ( 'post_type' => 'movies' )); ?>

它不起作用,它检索正常的帖子。如果我使用post_type'event'它可以工作(它从事件管理器插件中检索事件)。

有什么问题?

2 个答案:

答案 0 :(得分:0)

尝试:

<?php $loop = new WP_Query( array( 'post_type' => 'movies', 'posts_per_page' => 10 ) ); ?>

还有:

http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

答案 1 :(得分:0)

我终于设法使用get_posts()

来获取帖子
$args=array(
    'post_type'=> 'movies',
    'numberposts'=> -1
    );

$myposts = get_posts( $args );

foreach( $myposts as $post ) :
    setup_postdata($post);
    ...
endforeach;