我已将debug设置为true,并使用条件标记显示不同的横幅,例如:
<?php
if ( is_page( \'about\' ) || \'2\' == $post->post_parent ) {
// the page is "About", or the parent of the page is "About"
$bannerimg = \'about.jpg\';
} elseif ( is_page( \'learning\' ) || \'56\' == $post->post_parent ) {
$bannerimg = \'teaching.jpg\';
} elseif ( is_page( \'admissions\' ) || \'15\' == $post->post_parent ) {
$bannerimg = \'admissions.jpg\';
} else {
$bannerimg = \'home.jpg\'; // just in case we are at an unclassified page, perhaps the home page
}
?>
这会导致如下错误:
[24-Sep-2013 00:03:32] PHP Notice: Trying to get property of non-object in D:\\Clients\\project1\\www.project1.dev\\wp-content\\themes\\custom_v1\\header.php on line 100
删除$post->post\\u parent修复了PHP通知。如果我做错了什么,有人能告诉我吗?
Location of this code:
我在标题中使用了类似于上面的代码结构。php文件。这不在任何post循环内。我在上面粘贴的成本来自
http://codex.wordpress.org/Conditional_TagsPurpose of my code:根据用户所在的页面,我需要显示不同的横幅。因此,每个页面及其子页面都有相同的横幅。
if ( is_page(\'114\') || $post->post_parent == \'114\' ) { }
甚至上面的代码也会生成错误。不确定为什么$post->post\\u Parents不被视为对象。
最合适的回答,由SO网友:gmazzap 整理而成
@kaiser 答案为您提供了解决方案。我从你的评论中看出你不明白,所以我对他的答案投了赞成票,并将其翻译成代码:
<?php
$bannerimg = \'home.jpg\';
$post = is_singular() ? get_queried_object() : false;
if ( ! empty($post) && is_a($post, \'WP_Post\') ) {
if ( \'about\' == $post->post_name || \'2\' == $post->post_parent ) {
$bannerimg = \'about.jpg\';
} elseif ( \'learning\' == $post->post_name || \'56\' == $post->post_parent) ) {
$bannerimg = \'teaching.jpg\';
} elseif ( \'admissions\' == $post->post_name || \'15\' == $post->post_parent ) {
$bannerimg = \'admissions.jpg\';
}
}