the_post_thumbnail (); не работает при вызове сообщения на основе категории в WordPress - PullRequest
0 голосов
/ 25 марта 2020
<?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;

Я пытаюсь отобразить все сообщения из категории 3 на определенной странице. Я получаю заголовок и отрывок правильно. Но я не получаю изображение правильно.

Я впервые использовал:

the_post_thumbnail();

Но это не сработало.

Затем я used

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

Затем для публикации я получил изображение, поэтому напечатал $ attimages и нашел пустой массив.

Любая помощь

1 Ответ

0 голосов
/ 25 марта 2020
<?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;
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...