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


Display post from custom post type

我在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的帖子,为什么?有什么建议吗?

看起来您需要将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
);