php - 随机 WordPress 未定义变量
问题描述
我最近突然遇到了一个 WordPress 网站的未定义变量。我看到它是用 if 语句定义的(我认为这是无效的)。我在哪里可以放置它以消除错误。该站点呈现正常,但显示错误消息。消息是“注意:未定义的变量:第 29 行 /nas/content/live/qsrautomations/wp-content/themes/qsrautomations/lib/page-header.php 中的 background_image”
function qsrautomations_opening_page_header() {
if ( ( is_front_page() && is_active_sidebar( 'front-page-hero' ) ) || ( is_page() && !is_page_template( 'page_landing.php' ) ) || is_single() || is_archive() || is_home() ) {
if ( is_front_page() && is_active_sidebar( 'front-page-hero' ) ) {
$hero_image = get_option( 'qsrautomations-hero-image', sprintf( '%s/images/hero-image-1.jpg', get_stylesheet_directory_uri() ) );
$background_image = 'style="background-image: url(' . $hero_image . ')"';
}
if ( ( is_page() || ( is_home() && get_option('page_for_posts') ) ) && has_post_thumbnail() ) {
if ( is_home() && get_option('page_for_posts') ) {
$posts_page_id = get_option('page_for_posts');
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $posts_page_id ), 'full' );
} else {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
}
$background_image = 'style="background-image: url(' . $image[0] . ')"';
}
if ( $background_image ) {
$background_image_class = 'with-background-image';
}
?>
<div class="header-wrap bg-primary <?php echo $background_image_class; ?>" <?php echo $background_image; ?>>
<?php
}
}
解决方案
我没有经常使用 wordpress,但您可能需要分配变量 $background_image = ''; 如下所示,希望这对你有用。
<?php
function qsrautomations_opening_page_header() {
if ( ( is_front_page() && is_active_sidebar( 'front-page-hero' ) ) || ( is_page() && !is_page_template( 'page_landing.php' ) ) || is_single() || is_archive() || is_home() ) {
$background_image = '';
if ( is_front_page() && is_active_sidebar( 'front-page-hero' ) ) {
$hero_image = get_option( 'qsrautomations-hero-image', sprintf( '%s/images/hero-image-1.jpg', get_stylesheet_directory_uri() ) );
$background_image = 'style="background-image: url(' . $hero_image . ')"';
}
if ( ( is_page() || ( is_home() && get_option('page_for_posts') ) ) && has_post_thumbnail() ) {
if ( is_home() && get_option('page_for_posts') ) {
$posts_page_id = get_option('page_for_posts');
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $posts_page_id ), 'full' );
} else {
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
}
$background_image = 'style="background-image: url(' . $image[0] . ')"';
}
// if ( $background_image ) {
// $background_image_class = 'with-background-image';
// }
?>
<div class="header-wrap bg-primary <?php echo $background_image_class; ?>" <?php echo $background_image; ?>>
<?php
}
推荐阅读
- oracle-adf - 在 adf 中,当 autosubmit ="true" 时,实体验证将被跳过
- ubuntu - Octave 在 octave 包中找不到函数(流线型)
- c# - 需要将数据从excel导入C#中的数组
- docker - .Net Core Windows 服务未安装在 docker 容器中
- rest - 如何将 API amember pro 集成到 ionic 5 应用程序中
- python - Python 错误 1064:您的 SQL 语法有错误 | Arduino 串行
- android - MotionLayout 折叠 RecyclerView 标头但不展开
- powerbi - 如何使用 SUMX 和 VALUES 逐行计算 - ALLSELECTED 似乎不适用于 SUMX/VALUES?
- flask - 将变量从浏览器发送到 dash plotly 仪表板
- php - 如何使用 PHP 从 Excel 转换负数