KDX KDX - 1 year ago 141
PHP Question

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

I'm working on a Wordpress template


Here are the segment of my code:

$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

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

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

How do I fix this PHP Notice?

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download