我使用CPT创建post类型(图书)。我在调试模式下运行。这一切都发生在默认情况下,Wordpress 3.8,二十十二个主题。
我在ACF中创建了一个名为Books的字段组,并添加了三个字段: book_title、book_author、pub_year。我将字段组设置为显示post类型是否为图书。然后,我复制了page.php,将其重命名为books.php,并将默认的标题替换为模板名:Books。我创建了一个名为Books的新页面,并将模板样式设置为Books。我把这个页面添加到菜单中。我在books.php中添加了必要的php脚本来显示post数据。然后,我使用Add使用自定义字段输入四个不同的示例图书标题,并保存它们。我可以在sing-books.php上单独查看它们,但是当我进入图书条目时,我看到了.没有发布数据。我看到其他一切,如标题、页眉、页脚、侧边栏、注释,但没有post数据。
下面是books.php的代码:
<?php
/**
* Template Name: Books
*
*
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php if ( have_posts() ) : ?>
<h1><?php the_field('book_title'); ?></h1>
<h2><?php the_field('book_author'); ?></h2>
<p><?php the_field('pub_year'); ?></p>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; wp_reset_postdata(); ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>发布于 2014-01-22 12:13:26
尝试下面的代码,还请记住,如果您不希望内容-page.php模板显示您的书籍,也可以省略get_template_part( 'content', 'page' );。您可以创建自己的文件,比如内容-book.php,并将get_template_part更改为包含新模板。
<?php
/**
* Template Name: Books
*
*
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php get_posts(array('post_type'=>'books')); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_field('book_title'); ?></h1>
<h2><?php the_field('book_author'); ?></h2>
<p><?php the_field('pub_year'); ?></p>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
<?php endif; wp_reset_postdata(); ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>https://wordpress.stackexchange.com/questions/130770
复制相似问题