1

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.

5
  • Is this part of an archive page for the custom post type? – Manny Fleurmond Feb 10 '15 at 15:36
  • I want to display all posts of custom post type "songs". Where do you want to display these posts? – Brad Dalton Feb 10 '15 at 17:12
  • @PieterGoosen - Yes I have accepted. – rahul bhatt Feb 11 '15 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 Feb 11 '15 at 13:35
  • @MannyFleurmond - No... – rahul bhatt Feb 11 '15 at 13:38
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()

6
  • Thanks a lot for answer , Using above code I am getting just post name , But I need to display post title with thumbnail and little content, so May I need to call the_content()? – rahul bhatt Feb 11 '15 at 14:14
  • Yes, you should add whatever you need to, this is just the basics. for content, you will need to add the_content();. You still need to add all your HTML mark up as well. This section is personal preference, something I can't help you with :-) – Pieter Goosen Feb 11 '15 at 14:16
  • For basic template outlay, have a look at the bundled themes like twentyfifteen. This should give you a good starting point – Pieter Goosen Feb 11 '15 at 14:17
  • How can I call default post css style for this custom post? I just want that my custom post also should display proper like normal post in terms of style/css. – rahul bhatt Feb 12 '15 at 8:44
  • Just copy your loop between the while part from your index.php and add it in the loop here. As I said, that is a personal preference section which you will need to sort out yourself unfortunately. I can never know your theme styles. That is why this section is off topic here :-) – Pieter Goosen Feb 12 '15 at 8:57
1

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.com/songs and WordPress will display all your CPT pages in a standard archive according to the Template Hierarchy. Swap out example.com 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.

0

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

-1

Or even

query_posts( 'post_type=song');
2
  • 3
    Could you add more explanation on why this would work or why this is an answer to the question? – Howdy_McGee Feb 10 '15 at 20:54
  • I just meant try 'post_type=song' instead of 'post_type=songs' Sorry for confusion – Mike Feb 10 '15 at 21:03
-2

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

Reference: Querying by Post Type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.