代码之家  ›  专栏  ›  技术社区  ›  Raj

如何在wordpress中显示帖子中的每个特色图片

  •  0
  • Raj  · 技术社区  · 7 年前

    2 回复  |  直到 7 年前
        1
  •  1
  •   Sheedo    7 年前

    是的,你可以使用 loop 这样做。类似于帖子在博客页面中的显示方式。仅排除 the_content() the_post_thumbnail() ):

    <?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
            the_post_thumbnail();
        }  //end while
    } //end if
    ?>
    
        2
  •  1
  •   Raghav Gubrani    7 年前

    可能您正在使用循环(while循环)显示页面模板下的所有帖子,您可以在循环内添加

    the_post_thumbnail();
    

    到代码以在循环中显示特征图像。

    您可以在模板页面上尝试此代码,以显示此文件中每个帖子的特色图像。

    <?php
       $type = 'post';
       $paged = (get_query_var('paged')) ? absint( get_query_var('paged') ) : 1;
       $args = array(
            'post_type' => $type,
            'post_status' => 'publish',
            // 'posts_per_page' => 3, // To display the number of post per page 
            'paged' => $paged,
       );
        $query = new WP_Query( $args );
    
        if (have_posts()) :
    ?>
    
        <?php
            // Start the loop.
            while ( $query->have_posts() ) : $query->the_post();
        ?>
    
                <?php
                    if ( has_post_thumbnail() ) {
                        //the_post_thumbnail();
                            the_post_thumbnail( 'post-thumbnail', array( 'alt' => the_title_attribute( 'echo=0' ), 'class'  => "img-responsive" ) );
    
                            /* You can try other resolution also
    
                                the_post_thumbnail();                  // without parameter => Thumbnail
                                the_post_thumbnail('thumbnail');       // Thumbnail
                                the_post_thumbnail('medium');          // Medium resolution
                                the_post_thumbnail('large');           // Large resolution
                                the_post_thumbnail( array(100,100) );  // Other resolutions 100px X 100px 
                            */
    
                    }
                ?>
                <?php the_permalink(); // Link of the post ?>
    
             <?php the_title( sprintf( '<h3 class="title"><a href="%s">', esc_url( get_permalink() ) ), '</a></h3>' ); ?>
    
       <?php
            // End the loop.
            endwhile;
        ?>
    
    <?php endif; ?>