繁体   English   中英

如何在WordPress中显示每个帖子的功能图片?

[英]How to display the feature image of each post in WordPress?

我已使用以下代码尝试显示每个帖子的特色图片,但未显示任何内容:

<div class="thumbnail-img">

<?php 
$lastBlog = new WP_Query('type=post&posts_per_page=2&offset=1');


if ($lastBlog->has_post_thumbnail()) {
    while($lastBlog->has_post_thumbnail()) {
    $lastBlog->the_post_thumbnail();
                } ?>

<?php get_template_part('content-image', get_the_post_thumbnail()); 

}

?>

</div>
<br>

<?php
if( $lastBlog->have_posts()):
while($lastBlog->have_posts()): $lastBlog->the_post(); ?> 
<?php get_template_part('content-title', get_post_format()); ?>
<?php endwhile;

        endif;

        wp_reset_postdata();
    ?>
    </div>

我想在每个帖子标题的上方添加特色图片。 我该如何解决?

我真的不能具体说明您的模板结构(template-part content-title ??),但使用通用示例,以下将在可用的地方显示特色图片;

functions.php

    if ( ! function_exists( 'mytheme_setup' ) ) :

        function mytheme_setup() {
            /*
             * Enable support for Post Thumbnails on posts and pages.
             *
             * See: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
             */
            add_theme_support( 'post-thumbnails' );
            set_post_thumbnail_size( 825, 510, true );


        }

    endif;
    add_action( 'after_setup_theme', 'mytheme_setup' );

您的内容模板页面(content.php,模板页面等。)

    // WP_Query arguments
    $args = array (
            'nopaging'               => false,
            'posts_per_page'         => '2',
            'offset'                 => '1',
    );

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

    // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
                $the_query->the_post(); ?>
                <article>
                    <?php if ( has_post_thumbnail() ) : ?>
                    <div class="post-thumbnail">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                            <?php the_post_thumbnail(); ?>
                        </a>
                    </div>
                    <?php endif; ?>
                    <div class="post-title">
                        <?php echo '<h2>' . get_the_title() . '</h2>'; ?>
                    </div>
                </article>
        <?php
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
            // no posts found
        echo "NADA";
    }
<?php
$lastBlog = new WP_Query('type=post&posts_per_page=2&offset=1');
if( $lastBlog->have_posts()):
while($lastBlog->have_posts()): $lastBlog->the_post(); ?> 
<div class="title"><?php echo get_the_title(); ?></div>
<br />
<div class="thumbnail-img"><?php echo the_post_thumbnail();?></div>
<br />
<?php 
    endwhile;
    endif;
    wp_reset_postdata();
   ?>

试试这个应该很好

如果您想要图片网址,请使用此

$thumb_image=wp_get_attachment_url( get_post_thumbnail_id() );

而您想获得直接图像,然后在这里获得不同的图像

the_post_thumbnail( 'thumbnail' );     // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' );        // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' );  // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' );         // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' );          // Full resolution (original size uploaded)

//With WooCommerce
the_post_thumbnail( 'shop_thumbnail' ); // Shop thumbnail (180 x 180 hard cropped)
the_post_thumbnail( 'shop_catalog' );   // Shop catalog (300 x 300 hard cropped)
the_post_thumbnail( 'shop_single' );    // Shop single (600 x 600 hard cropped)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM