I created a custom post type in the mu-plugins folder.
function wpdocs_codex_book_init() {
$labels = array(
'name' => _x( 'Books', 'Post type general name', 'textdomain' ),
'singular_name' => _x( 'book', 'Post type singular name', 'textdomain' ),
);
$args = array(
'labels' => $labels,
'public' => true,
);
register_post_type( 'book', $args );
}
add_action( 'init', 'wpdocs_codex_book_init' );
When it comes to render the content of it in the single-book.php page I create the custom query and I use this code:
<?php
$args = array(
'post_type' => 'book',
);
$query = new WP_Query( $args );
?>
<h1><?php the_title(); ?></h1>
<h1><?php the_content(); ?></h1>
the title is rendered correctly but when it comes to the content it throws this error:
Warning: count(): Parameter must be an array or an object that implements Countable in /app/public/wp-includes/post-template.php on line 284
The default posts and the pages are, instead, rendered correctly. Why is this happening? Thanks in advance
I created a custom post type in the mu-plugins folder.
function wpdocs_codex_book_init() {
$labels = array(
'name' => _x( 'Books', 'Post type general name', 'textdomain' ),
'singular_name' => _x( 'book', 'Post type singular name', 'textdomain' ),
);
$args = array(
'labels' => $labels,
'public' => true,
);
register_post_type( 'book', $args );
}
add_action( 'init', 'wpdocs_codex_book_init' );
When it comes to render the content of it in the single-book.php page I create the custom query and I use this code:
<?php
$args = array(
'post_type' => 'book',
);
$query = new WP_Query( $args );
?>
<h1><?php the_title(); ?></h1>
<h1><?php the_content(); ?></h1>
the title is rendered correctly but when it comes to the content it throws this error:
Warning: count(): Parameter must be an array or an object that implements Countable in /app/public/wp-includes/post-template.php on line 284
The default posts and the pages are, instead, rendered correctly. Why is this happening? Thanks in advance
Share Improve this question asked Jan 14, 2019 at 13:51 EnricoEnrico 1096 bronze badges 2- Why are you creating a new query in the template? Does it work correctly if you remove your query code? – Milo Commented Jan 14, 2019 at 13:58
- totally my bad. Wordpress has already identified the correct query since i created the single-book itself. – Enrico Commented Jan 14, 2019 at 14:09
1 Answer
Reset to default 2single-book.php
should not have a custom query in it. WordPress has already queried the correct post, so new WP_Query( $args )
is completely unnecessary.
Your problem is that you're missing the loop.
At the bare minimum single-book.php
should have:
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>