I create a copy of the page:

<?php /** * Template Name: Gallery Page - Default * */ get_header(); ?> <main class="main__content"> <?php if ( has_post_format( 'image' ) ) : while ( have_posts() ) : the_post(); ?> <figure class="gallery-image post__prew effect"> <a href="<?php echo wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ) ?>" class="post__prew-link fancy" title=""> <?php mytheme_post_thumbnail(); ?> </a> </figure> <?php endwhile; else : get_template_part( 'template-parts/content', 'none' ); endif; ?> </main> <?php get_footer(); ?> 

I am trying to display on this page only articles with image type:

 ... <?php if ( has_post_format( 'image' ) ) : ... 

But it displays the none page, i.e. condition does not work:

 ... get_template_part( 'template-parts/content', 'none' ); ... 

In a post (article) I set the type of the image .

How can I fix what I'm doing wrong?

    2 answers 2

    has_post_format without the second parameter does not work OUTSIDE a WordPress loop. This check should be moved inside the loop and check each post for the presence of the image format.

    Here is absolutely working image output code.

     <?php /** * Template Name: Gallery Page - Default * */ get_header(); ?> <?php include(TEMPLATEPATH.'/logo-menu.php');?> <main class="main__content"> <?php global $post; if ( have_posts() ) : // если имеются записи в блоге. while ( have_posts() ) : the_post(); if ( has_post_format( 'image' ) ) : ?> <figure class="gallery-image post__prew effect"> <a href="<?php echo wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ) ?>" class="post__prew-link fancy" title=""> <?php the_post_thumbnail(); //mytheme_post_thumbnail(); ?> </a> </figure> <?php else : get_template_part( 'template-parts/content', 'none' ); endif; endwhile; endif; ?> </main> <?php get_footer(); ?> 

    The result can be found here: output page

    This code works under the following conditions:

    1. Image type posts are created and they have a thumbnail (I replaced the my_theme_thumbnail function with the standard the_post_thumbnail ();)
    2. A rubric created with gallery gallery and image type entries are included in this rubric.
    3. The file with the code is called category-gallery.php and placed in the theme folder

    If you need to display image type records from the page, the code will be somewhat different. I can also make such a fully working code, if it is necessary to output from the page.

    • The question was - what am I doing wrong)) I answered. Ok, now I'll fix the code - KAGG Design
    • I said correct your code. Why minus? Added work code to the response, with reference to the work page. - KAGG Design
    • I apologize!!! - HamSter
    • For some reason, it still does not work for me = (((, I already tried it the way. And I fulfilled the conditions clauses. - HamSter
    • and what is displayed when referring to site.com/gallery? - KAGG Design 2:55 pm

    Such construction with new WP_Query( $args ) helped:

     <?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'post', 'paged' => $paged, 'tax_query' => array( array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-image', ))); // the query $mytheme_the_query = new WP_Query( $args ); if ( $mytheme_the_query->have_posts() ) : while ( $mytheme_the_query->have_posts() ) : $mytheme_the_query->the_post(); ?> <?php if ( has_post_format( 'image' )) : ?> <figure class="gallery-image"> <a href="<?php echo wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) ) ?>" class="fancy" > <?php the_post_thumbnail(); ?> </a> </figure> <?php else : get_template_part( 'template-parts/content', 'none' ); endif; ?> <?php endwhile; endif; ?>