has_post_thumbnail()不在页面上工作

时间:2016-03-16 12:54:07

标签: wordpress

我正在重复使用我在wordpress主题中的index.php上使用的一些代码。一切都有效,除了缩略图(适用于index.php)。 我可以展示它们。 但是,如果我尝试检查via(has_post_thumbnail()),它总是返回false。 哪里可能是问题?

问题出现在以下部分:

if ( has_post_thumbnail() ) {
                        $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
                        echo  '<div class="section1-2singlephoto">' . $photo . '</div>';
                    }

                    else {
                        echo 'no Thumbnail';
                    }

只使用以下代码才能起作用:

                    $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
                    echo  '<div class="section1-2singlephoto">' . 

任何人都可以给我一个暗示,为什么这可能不起作用。 我将页面设为页面模板并将代码添加到其中。 这是完整的代码:

 <?php
            $args = array( 'numberposts' => '20',
                'offset'    => '1'
            );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ){




                echo '<div class="section1-2single large-12 medium-12 small-12 column">';

                echo '<a class="" href="' . get_permalink($recent["ID"]) . '"><h4>' . $recent["post_title"] .'</h4></a>';


                echo '<div class="large-12 medium-12 small-12 row">';

                echo '<div class="large-2 medium-2 small-2 columns">';

                // GET COMMENTS NUMBER
                $comments_count = wp_count_comments( $recent["ID"] );
                echo '<div class=metaDataContainer>';
                if ($comments_count->total_comments === 0) {
                    echo '<i class="fa fa-comment-o commentsNumbre"> 0</i>';
                };
                if ($comments_count->total_comments === 1) {
                    echo '<i class="fa fa-comment-o commentsNumbre"> '. $comments_count->total_comments .'</i>';
                };
                if ($comments_count->total_comments > 1) {
                    echo '<i class="fa fa-comment-o commentsNumbre"> '. $comments_count->total_comments .'</i>';
                };

                echo do_shortcode('[mashshare text="'. $recent["post_title"] .'" url="'. get_permalink($recent["ID"]) .' shares="false" align="center" "]');

                echo '</div>';
                echo '</div>';


                echo '<div class="large-10 medium-10 small-10 columns">';
// GET THE THUMBNAIL
                if ( has_post_thumbnail() ) {
                    $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
                    echo  '<div class="section1-2singlephoto">' . $photo . '</div>';
                }

                else {
                    echo 'no Thumbnail';
                }

                $content = wpautop($recent['post_content']);
                $trimmed_content = wp_trim_words( $content, 60, '...' );
                ;
                echo '<div class="trimmedContent">' . $content .'</div>';
                echo '<a class="button section1-2single-button" href="'. get_permalink($recent["ID"]) .'">weiter</a>';

                echo '</div>';

                echo '</div>';

                echo '</div>';



            }

            ?>

3 个答案:

答案 0 :(得分:3)

has_post_thumbnail()采用$post的可选参数,可以是帖子ID或WP_Post对象。

默认为全局$post对象。您需要传递帖子ID以告诉WordPress检查循环中的最近帖子而不是全局$post对象。像这样:

if ( has_post_thumbnail( $recent['ID'] ) ) {
    $photo = get_the_post_thumbnail( $recent['ID'], 'large' );
    echo  '<div class="section1-2singlephoto">' . $photo . '</div>';
}

else {
    echo 'no Thumbnail';
}

答案 1 :(得分:2)

在外循环中,请使用

has_post_thumbnail( $post_id )

答案 2 :(得分:1)

您正在将ID传递给// row dimension of R must be equal to row dimension of H if (measNoise.getRowDimension() != measurementMatrix.getRowDimension() || measNoise.getColumnDimension() != 1) { throw new MatrixDimensionMismatchException(measNoise.getRowDimension(), measNoise.getColumnDimension(), measurementMatrix.getRowDimension(), 1); } 函数以获取特定帖子的thumbanil,因此您应该检查该帖子上的thumbanil,并将ID传递给get_post_thumbnail()

has_post_thumbnail()