【问题标题】:Display post from custom post type显示来自自定义帖子类型的帖子
【发布时间】:2015-02-03 03:29:33
【问题描述】:

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

【问题讨论】:

  • 您是否重新保存了永久链接?
  • @leftside 重新保存的永久链接是什么意思?
  • 有时您需要刷新永久链接。您可以在“设置”>“永久链接”中找到它。将它们保存或重新保存为“帖子名称”
  • @leftside 是的,现在就做,但仍然没有在首页上显示其他地方有问题

标签: php custom-post-type wordpress


【解决方案1】:

您似乎需要将post_type =&gt; "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
);

【讨论】:

  • 还是不行,你能看看主帖上的edit吗?我做错了什么?
  • 显示什么?你所有的帖子?一个错误?你的帖子都没有?有时,自定义帖子类型在主页上显示时会变化无常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
相关资源
最近更新 更多