1
votes

I want to show two different images between the home page e the other page. To do that I'm using Advanced Custom Fields.

In my header.php i have this:

<?php

$image = get_field('banner');

 if ( is_home() ) { ?>
        <div class="container-fluid container-first-image" style="background-image: url('<?php echo $image['url']; ?>'); background-repeat:no-repeat; background-position:bottom; background-size:cover; height:550px; color:white; font-family:'Alegreya Sans', sans-serif; font-weight:700; text-align:center; margin:0; padding:0; width:100%;"><?php }
    else { ?><div class="container-fluid" style="background-image: url('<?php echo $image['url']; ?>'); background-repeat: no-repeat ;background-position: center; background-size: cover; height: 350px; color: white; font-family: 'Alegreya Sans', sans-serif; font-weight: 700; text-align: center; margin: 0; padding: 0; width: 100%;"><?php } ?>

Using this code my home page shows up the latest image I've uploaded in a post instead of the image i want to show. In other page all works fine.

In Settings --> Reading I've checked "Your Latest Post" in "Front Page Displays" item

How can i fix this?

3

3 Answers

2
votes

In Homepage You set custom field like

name = ishomepage, value = true

now goto your header.php file

$ishomepage = false;
$ishomepage = get_post_meta($post->ID, 'ishomepage', true);

if($ishomepage == true){
   // image for homepage 
}
else{
   // image for other page
}

or you can dirctly set image from homepage

set image path in advance custom field like name = headerimage,value = imagepath

<?php
    $headerimage = get_post_meta($post->ID, 'headerimage', true);

if ( is_home() ) { 
   ?>
   <div style="background-image: url('<?php echo $headerimage; ?>')>
<?php
    }
?>
1
votes

try changing if ( is_home() ) to if( if_front_page() ).

If you have a static page set to your home page you need to use is_front_page() instead.

0
votes

Please replace
$image = get_field('banner');

with

$image = get_field('banner', $pageId);

where $pageId is ID of the page where you upload the image.