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
|
1 Answer
Reset to default 0The 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.
WP_Query()
? If you set up asingle-cptslug.php
file in the theme, it will already automatically be running a query for you, which you can modify withpre_get_posts
if needed. – WebElaine Commented Feb 11, 2020 at 16:30