最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

pages - the_content(); of custom post type treated as an array or object

matteradmin10PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

single-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; ?>
Post a comment

comment list (0)

  1. No comments so far