通过帖子类型查询 - 没有显示

时间:2017-12-28 17:42:25

标签: php wordpress wordpress-theming

我正在尝试显示自定义帖子类型的帖子,但它什么也没显示。

我已经使用CPT UI / ACF设置了所有自定义字段,所有内容似乎都很平滑,直到显示部分。

这是我创建的blog.php模板:

<?php
/**
 * Template Name: Blog template
 *
 * @package Hestia
 * @since Hestia 1.0
 */

get_header();
?>

<?php

$args = array(
    'post_type' => 'blog',
    'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
?>


<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <h1><?php the_field('main_title');?><h1>
    <img src="<?php the_field('header_image');?>" alt="" />
    <p><?php the_field('paragraph_1');?></p>
    <img src="<?php the_field('image_1');?>" alt="" />
    <p><?php the_field('paragraph_2');?><h1>
    <img src="<?php the_field('image_2');?>" alt="" />
    <p><?php the_field('paragraph_3');?><h1>
    <img src="<?php the_field('image_3');?>" alt="" />



<?php endwhile; ?>

    <?php get_footer(); ?>
function cptui_register_my_cpts_blog() {

    /**
     * Post Type: Blog.
     */

    $labels = array(
        "name" => __( "Blog", "hestia-pro" ),
        "singular_name" => __( "blog", "hestia-pro" ),
        "menu_name" => __( "Blog", "hestia-pro" ),
        "all_items" => __( "All posts", "hestia-pro" ),
        "add_new" => __( "Add new", "hestia-pro" ),
        "add_new_item" => __( "Add new post", "hestia-pro" ),
    );

    $args = array(
        "label" => __( "Blog", "hestia-pro" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => false,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "blog", "with_front" => true ),
        "query_var" => true,
        "menu_icon" => "dashicons-welcome-write-blog",
        "supports" => array( "title", "editor", "thumbnail" ),
    );

    register_post_type( "blog", $args );
}

add_action( 'init', 'cptui_register_my_cpts_blog' );

'post_type'是正确的,字段名称也是如此。它们都匹配自定义字段,但由于某种原因,没有显示任何内容。

谢谢!

1 个答案:

答案 0 :(得分:0)

将has_archive设置为true。有同样的问题,这就解决了。