显示自定义帖子类型的帖子

时间:2015-02-03 03:29:33

标签: php custom-post-type wordpress

我在functions.php定义了自定义帖子类型(CPT):

register_post_type(
    'opinion',
    array(
        'label'              => __( 'Opiniones' ),
        'singular_label'     => __( 'Opinión' ),
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'menu_position'      => 2,
        'hierarchical'       => false,
        'rewrite'            => array( 'slug' => 'opinion', 'with_front' => true ),
        'has_archive'        => true,
        'query_var'          => true,
        'supports'           => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author' ),
        'capability_type'    => 'opiniones',
        'capabilities'       => array(
            'publish_posts'       => 'publish_opiniones',
            'edit_posts'          => 'edit_opiniones',
            'edit_others_posts'   => 'edit_others_opiniones',
            'delete_posts'        => 'delete_opiniones',
            'delete_others_posts' => 'delete_others_opiniones',
            'read_private_posts'  => 'read_private_opiniones',
            'edit_post'           => 'edit_opinion',
            'delete_post'         => 'delete_opinion',
            'read_post'           => 'read_opinion',
        )
    )
);

将此代码放在front-page.php

<?php
$prev_post_ids = array();
$prepost       = $post;
$normal_args   = array(
    'ignore_sticky_posts' => 1,
    'order'               => 'desc',
    'meta_query'          => array(
        array(
            'key'     => '_custom_blog_enhome',
            'value'   => '1',
            'compare' => '='
        )
    ),
    'post_status'         => 'publish',
    'posts_per_page'      => 6
);

$normal_query  = new WP_Query( $normal_args );

if ($normal_query->have_posts()) {
    while ($normal_query->have_posts()) {
        $normal_query->the_post();
        $prev_post_ids[] = $post->ID; ?>
        <?php $field = 'custom_blog_exclusivo';
        if ( ! ($$field = get_post_meta( $post->ID, '_'.$field, true ))) {
            $$field = "";
        } ?>

        // here goes the HTML markup for display the post not relevant here

    <?php }
}
$post = $prepost;
wp_reset_postdata(); ?>

但来自slug=opinion的CPT并未显示。我需要更改什么才能在front-page.php上显示它们?

执行测试

我正在做一些测试,我的代码现在看起来如下:

$prev_post_ids = array();
$prepost       = $post;
$normal_args   = array(
    'ignore_sticky_posts' => 1,
    'order'               => 'desc',
    'meta_query'          => array(
        array(
            'key'     => '_custom_blog_enhome',
            'value'   => '1',
            'compare' => '='
        )
    ),
    'post_status'         => 'publish',
    'posts_per_page'      => 6,
    'post_type'           => array('opinion','especiales','clasificados','portadadeldia','anunciantes','post','pages')
);

$normal_query  = new WP_Query( $normal_args )

但仍未显示opinion的帖子,为什么?有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您似乎需要在查询参数中添加post_type => "opinion"

$normal_args   = array(
'ignore_sticky_posts' => 1,
'order'               => 'desc',
'meta_query'          => array(
    array(
        'key'     => '_custom_blog_enhome',
        'value'   => '1',
        'compare' => '='
    )
),
 'post_type'             => 'opinion'
'post_status'         => 'publish',
'posts_per_page'      => 6
);