简体   繁体   English

the_post_thumbnail(); 根据 WordPress 中的类别调用帖子时不起作用

[英]the_post_thumbnail(); not working when calling post based on category in WordPress

<?php    
                                $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
     'order' => 'DESC' ,
    'cat' => '3',
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :
    while ( $arr_posts->have_posts() ) :
        $arr_posts->the_post();
        ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                               <a class="post-thumbnail" style="width: 250px;height: 147px;">

                             <?php

                    $attimages = get_attached_media('image', $post->ID);
                foreach ($attimages as $image) {
                 ?>
                 <img src="<?php echo wp_get_attachment_url($image->ID);?>"  > <?php
                }
               ?>                           </a> 
                                                <div class="entry-header">
                                                     <h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3></br>
                    <div class="entry-content">
                <?php the_excerpt(); ?>
                <a href="<?php the_permalink(); 

                ?>">Read More</a>
            </div>


                                                    </div>                        
                    </li>        


                            </br></ul>
          </article>
        <?php
    endwhile;

I am trying to display all post from category 3 to a particular page.我正在尝试将第 3 类中的所有帖子显示到特定页面。 I am getting the title and excerpt correctly.But I am not getting the image correctly.我正确地获得了标题和摘录。但我没有正确地获得图像。

I first Used :我第一次使用:

the_post_thumbnail();

But it didnt worked.但它没有奏效。

Then I used然后我用

 $attimages = get_attached_media('image', $post->ID);

Then For I post,I got image so I printed $attimages and found empty array.然后对于我发布,我得到了图像,所以我打印了 $attimages 并找到了空数组。

Any help任何帮助

<?php    
    $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'order' => 'DESC' ,
    'cat' => '3',
);
$arr_posts = new WP_Query( $args );

if ( $arr_posts->have_posts() ) :
    while ( $arr_posts->have_posts() ) :
        $arr_posts->the_post();
        ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a class="post-thumbnail" style="width: 250px;height: 147px;">
                <?php $attimages = get_attached_media('image', $post->ID);
                    foreach ($attimages as $image) { ?>
                    <img src="<?php echo wp_get_attachment_url($image->ID);?>"  > 
                <?php } ?> 
            </a> 
            <div class="entry-header">
                    <h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3>
                    <div class="entry-content">
                        <?php the_excerpt(); ?>
                        <a href="<?php the_permalink();?>">Read More</a>
                    </div>


            </div>                        

          </article>
        <?php
    endwhile;
endif;
?>

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

相关问题 the_post_thumbnail $size 不起作用 - the_post_thumbnail $size not working 在Wordpress中使用the_post_thumbnail()时,特色图片的位置不正确 - Incorrect positioning of the featured image when using the_post_thumbnail() in Wordpress 如何在wordpress上修改the_post_thumbnail函数? - How to modify the the_post_thumbnail function on wordpress? WordPress循环中的the_post_thumbnail无法正常工作 - the_post_thumbnail in WordPress Loop wont work 调整the_post_thumbnail的大小以正确拉伸,wordpress - resize the_post_thumbnail to stretch properly, wordpress 在WordPress中使用the_post_thumbnail的问题 - Issues using the_post_thumbnail in wordpress wordpress中the_post_thumbnail和get_the_post_thumbnail之间的区别 - Difference between the_post_thumbnail and get_the_post_thumbnail in wordpress WordPress-为什么在get_sidebar()之后调用the_post_thumbnail()不会在模板上显示任何内容 - Wordpress - Why calling the_post_thumbnail() AFTER get_sidebar() doesn't show anything on my template 在我自己的 wordpress 主题中添加 the_post_thumbnail 作为 CSS 背景 - adding the_post_thumbnail as CSS Background, in my own wordpress theme 使用the_post_thumbnail()图像引导4张卡片可以通过wordpress拉伸以适合自己? - Bootstrap 4 cards with the_post_thumbnail() images Stretch to fit using wordpress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM