簡體   English   中英

wordpress博客動態背景圖片

[英]wordpress blog dynamic background image

抱歉,如果已經解決了這個問題,但是如果找不到,我很抱歉。

我正在整理一個博客網站,打算將其轉換為wordpress主題。 對於每個博客文章,我在登錄頁面上都有一個全屏圖像鏈接,該鏈接將為您提供特定的博客文章。 每個帖子的代碼是相同的。

的HTML

<a href="#" ><div class="post"></div></a>

的CSS

.post{
    width: 100%;
    height: 75vh; 
    background-image: url(../img/5.jpg); 
    background-position: center 0px; 
    background-repeat: repeat-y; 
    background-attachment: fixed; 
    margin-bottom: 100px; 
    position: relative; 
}

當我希望將其轉換為wordpress主題時,我需要一種方法來復制具有與每個新帖子相關的不同背景圖像的代碼。 他們有辦法使用PHP或javascript嗎?

是的,您可以為此使用“自定義”字段。 https://codex.wordpress.org/Custom_Fields

<?php
$cust_image = get_post_meta($post->ID, 'backgroundimg', true);
echo '.post {background-image:url(';
echo $cust_image ? $cust_image : '../default_background.jpg';
echo ');}';
?>

希望這對您有用。

<a href="#" >
    <div class="post" style="background-image:url('<?php the_post_thumbnail_url( $post->ID ); ?>')">
    </div>
</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM