1

I'm trying to display different templates in archive.php page. I need to load different article display depending on custom post type. Here is my code:

<?php if(is_singular('libri')) :?>

<?php 
// WP_Query arguments
$args = array (
    'post_type'              => 'libri',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        the_title();
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata(); ?>

<?php else : ?>


<?php endif; ?>


<?php get_footer();?>

Anyway the page doesn't display anything. How can I get rid of this problem?

Thanks!

  • When you say the page doesn't display anything, do you mean it shows no posts, or do you mean you get a blank white screen with nothing at all? Have you checked your PHP error log and done the standard debugging checks? – Tom J Nowell Nov 7 '16 at 20:27
  • It doesn't display anything but header and archive title. Can I debug with a plugin? – Stefano Nov 7 '16 at 20:28
  • 3
    To start debugging I would put something in each "else" clause. Something like 'echo "No posts found in the loop."'; and then right before endif 'echo "Not singular libri."'; Then you will be able to tell which conditions are or aren't being met. – WebElaine Nov 7 '16 at 20:56
  • is_singular will return false on an archive page. If you wanted to check if you are on an archive for a post type, you would use is_post_type_archive( 'libri' ) , or you can create an archive-{post_type}.php file, which on this case would be archive-libri.php – czerspalace Nov 7 '16 at 21:38
2

is_singular will return false on an archive page. If you wanted to check if you are on an archive for a post type, you would use is_post_type_archive( 'libri' ) , or you can create an archive-{post_type}.php file, which on this case would be archive-libri.php

References: https://codex.wordpress.org/Post_Type_Templates https://codex.wordpress.org/Conditional_Tags#A_Single_Post_Page

0

You use is_singular() which is never going to return true on archive page, as @Tom J Nowell pointed out in the comment. So what you need to do for query to execute is to remove that condition.

  • I tried also without array but it doesn't work... – Stefano Nov 7 '16 at 20:52
  • I've just edited as noticed sth intereseting. – wscourge Nov 7 '16 at 20:57
  • is_singular ( aka is this page showing just a single post? ) refers to the current page, not the current post, so on an archive it will always be false, even inside a query/post loop. Unless you're doing queries with query_posts, which is bad practice – Tom J Nowell Nov 7 '16 at 21:43

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.