admin管理员组文章数量:1391974
// 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:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.php on line 4
Notice: Undefined index: subtitle in C:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.php on line 7
Notice: Undefined index: photo in C:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.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
// 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:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.php on line 4
Notice: Undefined index: subtitle in C:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.php on line 7
Notice: Undefined index: photo in C:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.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
Share
Improve this question
edited Feb 17, 2020 at 12:31
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Feb 17, 2020 at 11:32
Rajat SharmaRajat Sharma
1
0
1 Answer
Reset to default 1function site_banner_section($args = null) {
if (!$args['title']) {
You're getting warnings from these lines
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.php on line 4
Notice: Undefined index: subtitle in C:\xampp\htdocs\wp-vs-5.3.2\wp-content\themes\personal\functions.php on line 7
because $args might be null and you're still trying to treat it as an array, and because you're trying to access properties that may or may not exist to check their existence, which may work in other languages but isn't how you're meant to do this in PHP. You probably want something like:
function site_banner_section( $args = null ) {
if ( ! is_array( $args ) ) {
$args = array();
}
if ( ! ( array_key_exists( 'title', $args ) && $args['title'] ) ) {
$args['title'] = get_the_title();
to both make sure that $args is an array before we start testing properties on it and setting properties in it, and to check whether 'title' exists before we try and check if it's non-empty. Or if you're unlikely to be passed null as an argument, only the default, then you could use
function site_banner_section( $args = array() ) {
if ( ! ( array_key_exists( 'title', $args ) && $args['title'] ) ) {
And I'm not sure what you meant by duplicated code. If you meant avoid having to repeatedly check whether properties exist then you could use wp_parse_args to fill in the defaults, e.g.
function site_banner_section( $args = array() ) {
$defaults = array(
'title' => get_the_title(),
// etc.
);
$args = wp_parse_args( $args, $defaults );
but then you would need to call get_the_title and get_field to populate $defaults even when you don't actually use the values.
本文标签: theme developmentreduce duplicate code in wordpress
版权声明:本文标题:theme development - reduce duplicate code in wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744741305a2622623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论