【问题标题】:Dynamically created sections with unique backgrounds具有独特背景的动态创建部分
【发布时间】:2014-05-23 15:55:40
【问题描述】:

我正在创建一个 Wordpress 主题,它将所有页面显示为一个页面上的部分,并且菜单滚动到每个部分。我希望用户能够为每个页面设置一个独特的背景(这将是页面的特色图像)。但我发现,当用户为一页设置图片时,它会成为所有页面的背景图片。

如何调整我的代码,以便用户可以为每个页面设置唯一的背景图像?下面是我创建所有页面并将它们显示在一个页面上的代码:

<?php $pages = get_pages(array('sort_column' => 'menu_order'));
foreach ($pages as $page_data) {
    $content = apply_filters('the_content', $page_data->post_content);
    $title = $page_data->post_title;
        $slug = $page_data->post_name;
    //get url of featured image
    $thumb_id = get_post_thumbnail_id();
    $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
    $thumb_url = $thumb_url_array[0];
    //check if page has featured image
    if ( has_post_thumbnail() ) {
    $featured_image = $thumb_url;
    }
    else {
$featured_image = '';
    }
    echo "<section id='$slug'  class='main fullscreen'>";
echo '<article class="main parallax" style="background:url(' . $featured_image . ') 50% 0 repeat fixed;">';
echo $content;
    echo '</article>';
echo "</section>";
}
?>

【问题讨论】:

    标签: php arrays wordpress background-image


    【解决方案1】:

    有没有可能

    $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true); 
    

    行是将值添加到数组中的下一个位置而不是替换 [0] 位置?也许将该行更改为

    $thumb_url_array[0] = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
    

    将导致在 if 语句中为 $thumb_url 和稍后的 $featured_image 分配正确的值。

    【讨论】:

    • 恐怕这个解决方案不起作用。它只是从所有部分中删除了特色图片
    • 无赖...我想值得一试。
    【解决方案2】:

    你需要改变

    $thumb_id = get_post_thumbnail_id();
    

    $thumb_id = get_post_thumbnail_id($pages_data->ID);
    

    【讨论】:

    • 您还需要将页面ID传递给has_post_thumbnail()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多