WP_Query and is_page_template() conditional

I have a custom query that goes looks like this:

        $temp = $wp_query;
        $wp_query = null;
        $args = array('posts_per_page' = 5, 'post_type' = 'gallery', 'post_status' = 'published', 'paged' = $paged);
        $wp_query = new WP_Query($args);
        if($wp_query-have_posts()):
            while($wp_query-have_posts()) : $wp_query-the_post();
                if(get_post_format()) {
                    $post_format = get_post_format();
                } else {
                    $post_format = 'standard';
                }
                get_template_part('/templates/parts/format', $post_format);
            endwhile;
        endif; ?

Inside post format that I call with get_template_part('/templates/parts/format', $post_format); I have conditional is_page_template().

This conditional isn't working. But when I use regular WP_Query without resetting the default one it does work. What can i do to make this work?

Topic wp-reset-query conditional-tags wp-query Wordpress

Category Web


You have a couple of mistakes here.

Firstly, if you need to check if you have a specific post format, use has_post_format( $post_format ). So if you need to something specific for post type video, you will do something like this

if ( has_post_format('video')) {
<--show video maybe-->
}

Secondly, standard is not actually a post format. When no specific format is set, wordpress will assign Standard to that post. This is what you will see in the back end in post editor. If you need to exclude all other post types and just display something specific that has no post format assigned to it you will use false == has_post_format() For example

 if ( false == has_post_format()) {
    <--show post without a post format-->
    }

Thirdly, your is_page_template() baffles me as I don't know where are you using it.

Just to leave this with you, why don't you create specific content-postformat.php templates and just call them normally. get_template_part( 'content', get_post_format() ) to get all formats or get_template_part( 'content', 'video' ) to call a specific format, in this case video format.

This is how twentyfourteen's content.php looks like. All posts without a post format are diplayd using this template.

<?php
/**
 * The default template for displaying content
 *
 * Used for both single and index/archive/search.
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <?php twentyfourteen_post_thumbnail(); ?>

    <header class="entry-header">
        <?php if ( in_array( 'category', get_object_taxonomies( get_post_type() ) ) && twentyfourteen_categorized_blog() ) : ?>
        <div class="entry-meta">
            <span class="cat-links"><?php echo get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfourteen' ) ); ?></span>
        </div>
        <?php
            endif;

            if ( is_single() ) :
                the_title( '<h1 class="entry-title">', '</h1>' );
            else :
                the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
            endif;
        ?>

        <div class="entry-meta">
            <?php
                if ( 'post' == get_post_type() )
                    twentyfourteen_posted_on();

                if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
            ?>
            <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
            <?php
                endif;

                edit_post_link( __( 'Edit', 'twentyfourteen' ), '<span class="edit-link">', '</span>' );
            ?>
        </div><!-- .entry-meta -->
    </header><!-- .entry-header -->

    <?php if ( is_search() ) : ?>
    <div class="entry-summary">
        <?php the_excerpt(); ?>
    </div><!-- .entry-summary -->
    <?php else : ?>
    <div class="entry-content">
        <?php
            the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) );
            wp_link_pages( array(
                'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentyfourteen' ) . '</span>',
                'after'       => '</div>',
                'link_before' => '<span>',
                'link_after'  => '</span>',
            ) );
        ?>
    </div><!-- .entry-content -->
    <?php endif; ?>

    <?php the_tags( '<footer class="entry-meta"><span class="tag-links">', '', '</span></footer>' ); ?>
</article><!-- #post-## -->

That said, and everything put together, your code will look something like this

$temp = $wp_query;
    $wp_query = null;
    $args = array('posts_per_page' => 5, 'post_type' => 'gallery', 'post_status' => 'published', 'paged' => $paged);
    $wp_query = new WP_Query($args);
    if($wp_query->have_posts()):
        while($wp_query->have_posts()) : $wp_query->the_post();
            if ( false == has_post_format()) {
                <--do something for posts without post format-->
            } else {
                <--do something for posts with post format-->
            }
        endwhile;
    endif; ?>

Please also have a look at s-ha-dum's answer


From the Codex (emphasis mine):

This Conditional Tag allows you to determine if you are in any page template. Optionally checks if a specific Page Template is being used in a Page. This is a boolean function, meaning it returns either TRUE or FALSE. This tag must be used BEFORE The Loop and does not work inside The Loop (see Notes below).

http://codex.wordpress.org/Function_Reference/is_page_template

Your get_template_part(), and all the code it loads, is inside the Loop, so even if this works for you sometimes you are doing it wrong. I would suggest the solution offered in the Codex:

The function get_page_template_slug( $post_id ) will return the slug of the currently assigned page template (or an empty string if no template has been assigned - or false if the $post_id does not correspond to an actual page). You can easily use this anywhere (in The Loop, or outside) to determine whether any page has been assigned a page template.

  // in the loop:
  if ( get_page_template_slug( get_the_ID() ) ){
     // Yep, this page has a page template
  }

That is, alter your code inside get_template_part('/templates/parts/format', $post_format); to use get_page_template_slug( get_the_ID() ).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.