我刚刚在网上找到了一段聪明的代码,它允许Wordpress帖子并排显示在列中,并且可以很好地处理我想要显示的标题和帖子摘录。
然而,我也想显示我在每篇文章的函数文件中定义的特定特征图像。除了缺少特征图像的输出外,以下代码工作正常。我猜我的语法在那一点上是不正确的。
                <?php
                    // Custom loop that adds a clearing class to the first item in each row
                    $args = array(\'numberposts\' => -1, \'order\' => \'ASC\', \'post_type\' => \'treatments\' ); //For this example I am setting the arguments to return all posts in reverse chronological order. Odds are this will be a different query for your project
                    $allposts = get_posts($args);
                    $numCol = 2; // Set number of columns
                    // Start Counter
                    $counter = 0;
                    foreach ($allposts as $post) {
                        $content = \'<div class="six columns\'.\' \'.($counter % $numCol == 0 ? \' alpha\' : \'omega\').\'">\'; // Add class to the columns depending on if it is odd or even
                        $content .= \'<section class="treatments lightgrey-background">\';
                        $content .= \'<figure>\';
                        $content .= \'<img src="\'.($post->the_post_thumbnail->small).\'" />\';
                        $content .= \'</figure>\';
                        $content .= \'<h4>\'.$post->post_title.\'</h4>\';
                        $content .= $post->post_excerpt;
                        $content .= \'</section>\';
                        $content .= \'</div>\';
                        echo $content;
                        $counter ++;
                    }
                ?>
 如果用户还没有指定特征图像,我希望有一个后备图像。我想一个类似于向列中添加类的缩写的if-else语句是合适的?以下是功能文件中的特色图像代码。
<?php
// add featured image
add_theme_support( \'post-thumbnails\' );
set_post_thumbnail_size( 120, 120, true );
add_image_size( \'small\', 120, 120, true );
add_image_size( \'medium\', 330, 330, true );
add_image_size( \'front_page\', 460, 350, true);
add_image_size( \'header\', 660, 200, true);
add_image_size( \'large\', 600, 390, true );
?>
 
                SO网友:Chip Bennett
                这就是语法问题:
$content .= \'<img src="\'.($post->the_post_thumbnail->small).\'" />\';
 The
the_post_thumbnail() 函数不是
$post 对象,并将打印而不是返回后期缩略图,即使该方法可行。
尝试使用get_the_post_thumbnail() 而是:
$thumbnail = get_the_post_thumbnail( $post->ID, \'small\' );
 请注意,此函数返回完整格式的
<img> 标记标记,因此您需要相应地调整代码:
$content .= get_the_post_thumbnail( $post->ID, \'small\' );
 编辑请注意
\'small\' 是WordPress已定义的图像大小,因此您需要使用其他名称。我会使用一些位置描述,例如
\'post-two-column\':
add_image_size( \'post-two-column\', 120, 120, true );
 然后,在循环中相应地引用:
$content .= get_the_post_thumbnail( $post->ID, \'post-two-column\' );