1

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?

KDX
  • 611
  • 2
  • 10
  • 22
  • 1
    `$page` is obviously not an object. And if it was `$page->ID + $page` would be nonsence. There is not enough code to say any more than that – RiggsFolly Jul 23 '16 at 20:09

2 Answers2

0

I think $page is an integer not an object so $page->ID return 0. Just remove $page->ID and use only $page % 3

miepsik
  • 63
  • 8
0

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];
Vyacheslav
  • 157
  • 4
  • 1
    And the notice you get in case of using $page as the object with the property called ID, but $page is not the object and can't have properties. – Vyacheslav Jul 23 '16 at 20:15
  • It was indeed a typo, using `$post->ID` resolve the issue. – KDX Jul 23 '16 at 20:27