0

I'm using CPT UI to create the post type (books). I am running in debug mode. This is all happening on default, Wordpress 3.8, twenty twelve theme.

I created a field group called Books in ACF and added three fields: book_title, book_author, pub_year. I set the field group to show if post type is books. I then copied page.php, renamed it books.php, and replaced the default header with Template Name: Books. I created a new page called Book Entries and set the template style to Books. I added this page to the menu. I added the necessary php script in books.php to show the post data. I then used Add Books to enter four different sample book titles using the custom fields and saved them. I am able to view them individually on single-books.php, but when I go to Book Entries I see... no post data. I see everything else, like title, header, footer, sidebars, comments, but no post data.

Below is the code for 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(); ?>
1
  • This question is so old and WordPress have received so many updates since it was asked, I'm seriously questioning the eligibility of it. – Athoxx Oct 31 '19 at 12:10
0

Try this below code, also remember you can omit get_template_part( 'content', 'page' ); if you do not want content-page.php template to display your books. You can create your own file like content-book.php and change get_template_part to include your new template.

<?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(); ?>
6
  • This is one of the configurations I had tried earlier and unfortunately it only produces the same result: all page elements appear, but no post data. – MBWD Jan 22 '14 at 12:18
  • @MBWD see my edit... – Rajeev Vyas Jan 22 '14 at 12:29
  • You have to make query to get books as your using "page" to display your books list – Rajeev Vyas Jan 22 '14 at 12:40
  • OK, thank you very much, I will try this. Just for my own information, how would different ways of displaying the books list change the way I called it in the code of books.php? Everything I have seen elsewhere seems to indicate that I can just write the php script to call up a field and it should simply appear. I have not seen anything about the necessity of querying the database, although it makes sense. I am new to this plugin so I appreciate your help. – MBWD Jan 22 '14 at 13:36
  • I ask this because in this video youtube.com/watch?v=6URWfMTsWPo he seems able to just copy page.php, make it a template, write the php, and voila, it appears. I don't understand why it works for him and not for me! – MBWD Jan 22 '14 at 13:43
0

The query for your books.php file contains the page you associated the template to, WordPress has no way of knowing you expect to see your books cpt data within that query.

Rather than create a page and custom template, set your cpt to have an archive page. This is accomplished by setting has_archive to true when you register your post type. You can then create an archive-books.php template and WordPress will automatically use that to display the archive - no need for a separate page.

2
  • OK, this makes sense. I recreated a CPT of 'book' using CPT UI and set has_archive to true. I then recreated some fields in ACF and assigned them to post_type -> 'book'. I then created an archive-book.php, but I do not see it at mydomain/book or at mydomain/archive-book (I'm developing locally). Instead I am getting 404. Also, if I am not creating a page with a template which I can then put in the menu, I do not quite follow how the archive page is linked to. – MBWD Jan 22 '14 at 17:02
  • I'm not familiar with the CPT UI plugin, but you typically have to flush rewrite rules when new post types are added or modified, by visiting the permalinks settings page. your archive should be visible at /book/, or you also have the option of setting has_archive to a string, like books, so your archive will be at /books/. as for the menu item, the ability to add archives is sadly one option still missing, but can be solved by adding an item under links and entering /book/ as the URL. – Milo Jan 22 '14 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.