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

Not getting posts of custom post types

programmeradmin2浏览0评论

I have created a custom template and trying to get a custom post of type "song", But it is displaying only the header and footer on the page. There is no post content coming. Even if I try to call general posts then also no content coming, Should I register this template anywhere in the theme? OR need to call anything here?

My code :

<?php 
/* Template Name: song Page
 *
 * Selectable from a dropdown menu on the edit page screen.
 */

get_header(); ?>

<div>

<?php 
if ( have_posts() ) {
   while ( have_posts() ) {
      the_post(); 
      query_posts( 'post_type=song'); 
   } // end while
} // end if
?></div>

<?php get_footer(); ?>

Please help to correct my code. I want to display all posts of custom post type "songs". I have already created a custom post type.

I am putting the above code in WordPress editor, I have installed a plugin called "PHP execution" so it executes PHP code.

I have created a custom template and trying to get a custom post of type "song", But it is displaying only the header and footer on the page. There is no post content coming. Even if I try to call general posts then also no content coming, Should I register this template anywhere in the theme? OR need to call anything here?

My code :

<?php 
/* Template Name: song Page
 *
 * Selectable from a dropdown menu on the edit page screen.
 */

get_header(); ?>

<div>

<?php 
if ( have_posts() ) {
   while ( have_posts() ) {
      the_post(); 
      query_posts( 'post_type=song'); 
   } // end while
} // end if
?></div>

<?php get_footer(); ?>

Please help to correct my code. I want to display all posts of custom post type "songs". I have already created a custom post type.

I am putting the above code in WordPress editor, I have installed a plugin called "PHP execution" so it executes PHP code.

Share Improve this question edited Nov 11, 2020 at 11:01 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Feb 10, 2015 at 14:41 rahul bhattrahul bhatt 1176 bronze badges 5
  • Is this part of an archive page for the custom post type? – Manny Fleurmond Commented Feb 10, 2015 at 15:36
  • I want to display all posts of custom post type "songs". Where do you want to display these posts? – Brad Dalton Commented Feb 10, 2015 at 17:12
  • @PieterGoosen - Yes I have accepted. – rahul bhatt Commented Feb 11, 2015 at 13:34
  • @BradDalton - There is custom template I have created. as a code in that template , I have call header , footer and above loop to get posts of custom type , But in page only header and footer comes :( – rahul bhatt Commented Feb 11, 2015 at 13:35
  • @MannyFleurmond - No... – rahul bhatt Commented Feb 11, 2015 at 13:38
Add a comment  | 

5 Answers 5

Reset to default 1

You really have two major flaws in your code:

  • query_posts needs to go before your loop, not inside it

  • Never use query_posts in the first place unless you need to break something on your page

To learn why not to use query_posts and when to use a custom query and how to use it, check this post I have done a while ago

So, in order to correct your code in your template, we are going to make use of WP_Query and not query_posts, and we are going to do the query before the loop

Try something like this:

<?php 
 /* Template Name: song Page
 *
 * Selectable from a dropdown menu on the edit page screen.
 */
?>

<?php get_header(); ?>

<div>

    <?php 
    $args = array(
        'post_type' => 'song'
    );
    $q = new WP_Query($args);
    if ( $q->have_posts() ) {
        while ( $q->have_posts() ) {
            $q->the_post(); 

            //Add your template tags like below
            the_title();

        } // end while
        wp_reset_postdata();
    } // end if
    ?>

</div>

Also, never forget to reset custom queries if you have used the loop or setup_postdata($post) with wp_reset_postdata()

You don't need to create any templates if you simply want to list all pages in an archive for your custom post type:

You simply go to http://example/songs and WordPress will display all your CPT pages in a standard archive according to the Template Hierarchy. Swap out example with your domain.

You may need to re-save your Permalinks to flush your rewrite rules and include the following parameters in your register_post_type function:

'has_archive' => true,

'rewrite' => array( 'slug' => 'songs', 'with_front' => false ),

If you want to create a custom page for your CPT archive, create a new file and name it archive-song.php in the root directory of your theme.

Go to Settings > Permalinks and just click on "Save Changes" button. Now refresh the page.

Or even

query_posts( 'post_type=song');

Have you tried 'post_type'=> 'songs'?

Reference: Querying by Post Type

发布评论

评论列表(0)

  1. 暂无评论