简体   繁体   中英

How to fix PHP Notice: Trying to get property of non-object in Wordpress template?

I'm working on a Wordpress template content-article.php

Here are the segment of my code:

$article_field[];
$article_field[] = 'test1';
$article_field[] = 'test2';
$article_field[] = 'test3';

echo $article_field[($page->ID + $page) % 3];

The PHP Notice: Trying to get property of non-object, is taking place at the line of echo

Even there's a warning, the script still execute fine and giving me correct result.

I'm rotating the $article_field[] element display based on pagination variable $page , where array index is calculated by current page id + page number % 3

How do I fix this PHP Notice?

我认为$ page是一个整数而不是对象,因此$ page-> ID返回0。只需删除$ page-> ID并仅使用$ page%3

I may be wrong, but if I've understood you right you want to get current post id? If yes,

$post_id = get_the_id()

and your output will be something like this:

echo $article_field[($post_id + $page) % 3];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM