reduce duplicate code in wordpress
// I have this code in my single.php file and i am trying to reduce duplication
// code is under below, this code works for me but this is displaying multiple //messages to me such as
/*
Notice: Trying to access array offset on value of type null in C:xampphtdocswp-vs-5.3.2wp-contentthemespersonalfunctions.php on line 4
Notice: Undefined index: subtitle in C:xampphtdocswp-vs-5.3.2wp-contentthemespersonalfunctions.php on line 7
Notice: Undefined index: photo in C:xampphtdocswp-vs-5.3.2wp-contentthemespersonalfunctions.php on line 10
*/
// SINGLE.PHP File
while (have_posts() ) {
the_post();
site_banner_section();
?>
// FUNCTIONS.PHP File
<?php
function site_banner_section($args = null) {
if (!$args['title']) {
$args['title'] = get_the_title();
}
if (!$args['subtitle']) {
$args['subtitle'] = get_field('page_banner_subtitle');
}
if (!$args['photo']) {
if (get_field('page_banner_background_image')) {
$args['photo']= get_field('page_banner_background_image')['sizes']['bannerImage'];
}else{
$args['photo']=get_theme_file_uri('/images/ocean.jpg');
}
}
?>
<div class="page-banner">
<div class="page-banner__bg-image" style="background-image: url(<?php echo $args['photo']; ?>);"></div>
<div class="page-banner__content container container--narrow">
<h1 class="page-banner__title"><?php echo $args['title']; ?></h1>
<div class="page-banner__intro">
<p><?php echo $args['subtitle']; ?></p>
</div>
</div>
</div>
<?php
Leave an answer