首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过css添加the_post_thumbnail();

通过css添加the_post_thumbnail();
EN

Stack Overflow用户
提问于 2015-10-13 03:27:28
回答 2查看 1.7K关注 0票数 1

我试图设置我的帖子页面,以便每个帖子项目都加载它的特征图像,因为它的背景。我的代码工作正常,但是如何添加帖子缩略图作为每个帖子的背景?我的代码附在下面

代码语言:javascript
复制
<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package Clean Blog
 */

get_header(); ?>

            <?php
    // Custom loop that adds a clearing class to the first item in each row
    $args = array('numberposts' => -1, 'order' => 'ASC' ); //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="col-md-6 columns-'.$numCol.($counter % $numCol == 0 ? ' first' : '').'">'; // Add class to first column
            $content .= '<h3><a href="'.$post->guid.'">'.$post->post_title.'</a></h3>';
            $content .= $post->post_content;
            $content .= '</div>';
            echo $content;
            $counter ++;
        }
    ?>



    <style type="text/css">
        /* Added styles here for the demo, but ideally you would put this in a stylesheet.*/
        .columns-2 {
            float:left;
        }
        .first {
            clear:both;
            margin-left:0;
        }
    </style>
                <!-- /.col-lg-8.col-lg-offset-2.col-md-10.col-md-offset-1 -->

    <?php get_footer(); ?>

UPDATE我现在尝试将它添加为内联样式,但是原始样式显示在页面上。我觉得我错过了一些轻松的东西

http://imgur.com/kkATCAC

代码语言:javascript
复制
            <?php
    // Custom loop that adds a clearing class to the first item in each row
    $args = array('numberposts' => -1, 'order' => 'ASC' ); //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
    $thumbnail = get_the_post_thumbnail( $page->ID, 'thumbnail' );



    // Start Counter
    $counter = 0;
    foreach ($allposts as $post) {
        $content = '<div class="col-md-6 columns-'.$numCol.($counter % $numCol == 0 ? ' first' : '').'" style="background: url("${thumbnail}") center center/cover no-repeat;">'; // Add class to first column
        $content .= '<h3><a href="'.$post->guid.'">'.$post->post_title.'</a></h3>';
        $content .= $post->post_content;
        $content .= '</div>';
        echo $content;
        $counter ++;
    }


?>

更新2,,我检查了引号,但是现在我得到了一个解析错误,页面根本没有加载。

代码语言:javascript
复制
$content = '<div style="background: url('${thumbnail}') center center/cover no-repeat;" class="col-md-6 columns-'.$numCol.($counter % $numCol == 0 ? ' first' : '').'">'; // Add class to first column
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-13 03:43:22

您可以将其添加为内联样式:

代码语言:javascript
复制
<?php

    $thumbnail = get_the_post_thumbnail( $page->ID, 'thumbnail' );

    $content = "<div class='your-post-thing' style='background: url('${thumbnail}') center center/cover no-repeat;'>...stuff...</div>';

    echo $content;
?>

根据您的浏览器支持,您可以使用对象匹配

代码语言:javascript
复制
<?php
    $content .= "<div class='your-post-thing'>";
        $content .= "<img class='background-image' src='${thumbnail}' />";
    $content .= "</div>";

    echo $content;
?>

CSS:

代码语言:javascript
复制
.your-post-thing {
    position: relative;
}

.background-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}
票数 2
EN

Stack Overflow用户

发布于 2015-10-13 03:38:50

代码语言:javascript
复制
$content = '<div style="background-image:url('.wp_get_attachment_url(get_the_post_thumbnail()).')" class="col-md-6 columns-'.$numCol.($counter % $numCol == 0 ? ' first' : '').'">'; // Add class to first column

您可以尝试这样的方法,在css中您可以添加规则以正确查看背景。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33093608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档