admin管理员组

文章数量:1391991

So in a local wordpress I created a plugin that will make a row with its custom post type called book (it is saving successfully in the db)... and now I want to display a special template for it in the index of my theme, but after I use this code nothing shows:

<?php
    $loop = new WP_Query( array( 'post_type' => 'book', 'category_name' => 'book', 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
////
if($loop->have_posts()):
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
   <div class="ptitle">
                    <h2><?php echo get_the_title(); ?></h2>
                </div>
  <h3> <?php the_title(); ?> </h3>
  <small>Posted on:<?php the_time('F j, Y'); ?>, in
   <?php the_category(); ?> </small>
<p>   <?php the_content(); ?> </p>
<hr>
<?php
endwhile;
endif;
    ?>

So in a local wordpress I created a plugin that will make a row with its custom post type called book (it is saving successfully in the db)... and now I want to display a special template for it in the index of my theme, but after I use this code nothing shows:

<?php
    $loop = new WP_Query( array( 'post_type' => 'book', 'category_name' => 'book', 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
////
if($loop->have_posts()):
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
   <div class="ptitle">
                    <h2><?php echo get_the_title(); ?></h2>
                </div>
  <h3> <?php the_title(); ?> </h3>
  <small>Posted on:<?php the_time('F j, Y'); ?>, in
   <?php the_category(); ?> </small>
<p>   <?php the_content(); ?> </p>
<hr>
<?php
endwhile;
endif;
    ?>
Share Improve this question edited Feb 11, 2020 at 16:12 Cheo Molina asked Feb 11, 2020 at 16:03 Cheo MolinaCheo Molina 155 bronze badges 1
  • Just curious, why are you doing a new WP_Query()? If you set up a single-cptslug.php file in the theme, it will already automatically be running a query for you, which you can modify with pre_get_posts if needed. – WebElaine Commented Feb 11, 2020 at 16:30
Add a comment  | 

1 Answer 1

Reset to default 0

The loops itself is working. I guess you have trouble to output this content on some page template. There are many ways how to show output of this loop on the homepage.

1.You can create home.php file, put there your code and in theme settings select which page should output your custom post type.

2.You can create page template according to the documentation, put there your code, create some page in wordpress admin and on the right side in editor menu choose your newly created template.

本文标签: phpVisualize info in just custom posttype in theme