最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Visualize info in just custom post_type in theme

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论