Wordpress 使用高级自定义字段显示不同的图像
我想在主页和其他页面之间显示两个不同的图像。为此,我使用高级自定义字段 在my header.php中,我有以下内容:Wordpress 使用高级自定义字段显示不同的图像,wordpress,image,advanced-custom-fields,Wordpress,Image,Advanced Custom Fields,我想在主页和其他页面之间显示两个不同的图像。为此,我使用高级自定义字段 在my header.php中,我有以下内容: <?php $image = get_field('banner'); if ( is_home() ) { ?> <div class="container-fluid container-first-image" style="background-image: url('<?php echo $image['url']; ?&
<?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 } ?>
请更换
$image=get_字段('banner')
与
其中$pageId是上载图像的页面的ID 尝试将if(is_home())更改为if(if_front_page())
如果将静态页面设置为主页,则需要使用is\u front\u page()
。在主页中设置自定义字段,如
name=ishomepage,
值=真
现在转到header.php文件
$ishomepage = false;
$ishomepage = get_post_meta($post->ID, 'ishomepage', true);
if($ishomepage == true){
// image for homepage
}
else{
// image for other page
}
或者您可以直接从主页设置图像
提前设置图像路径自定义字段,如
名称=头部图像,值=图像路径
<?php
$headerimage = get_post_meta($post->ID, 'headerimage', true);
if ( is_home() ) {
?>
<div style="background-image: url('<?php echo $headerimage; ?>')>
<?php
}
?>
<?php
$headerimage = get_post_meta($post->ID, 'headerimage', true);
if ( is_home() ) {
?>
<div style="background-image: url('<?php echo $headerimage; ?>')>
<?php
}
?>