Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like your <code>$post</code> variable isn't being assigned with the current post information through the loop. As far as I can tell, you could try either one of these ways:</p> <h2>1) Global <code>$post</code></h2> <p>The <code>$post</code> variable is within your <code>featured()</code> <a href="http://php.net/manual/en/language.variables.scope.php" rel="nofollow">function scope</a>. Therefore, when you run your loop, it is not being recognized as the <code>global $post</code> variable, which is the one WordPress fills with post information through the loop. Simply declare <code>$post</code> as a <a href="http://www.php.net/manual/en/language.variables.scope.php#language.variables.scope.global" rel="nofollow"><code>global</code></a> variable at the beginning of your function scope and you should be able to gather post information:</p> <pre><code>function featured() { global $post; // ... all your function code, $post-&gt;ID should work now } </code></pre> <h2>or 2) Use <code>get_the_ID()</code></h2> <p>You could replace <code>$post-&gt;ID</code> for WP's native function <code>get_the_ID()</code>. This is just about the same as the previous solution, as this function will retrieve the <code>ID</code> property from the global <code>$post</code> object automatically. I would take this as the best solution, as you don't have to worry about scope whenever using post functions (<code>get_the_ID()</code>, <code>get_the_title()</code>, etc) as long as the <code>$post</code> object is populated (after <code>the_post()</code> is called).</p> <p>So you could replace this line:</p> <pre><code>$do_not_duplicate = $post-&gt;ID; </code></pre> <p>for</p> <pre><code>$do_not_duplicate = get_the_ID(); </code></pre> <p>and</p> <pre><code>if ($post-&gt;ID == $do_not_duplicate) continue; </code></pre> <p>for</p> <pre><code>if (get_the_ID() == $do_not_duplicate) continue; </code></pre> <hr> <p>Try either one of these solutions, I bet both should work for you. Actually, the example you took from codex page works just fine, the problem is that you applied it inside a local <code>function</code>. This way your <code>$post</code> variable is a local (<a href="http://php.net/manual/en/language.variables.scope.php" rel="nofollow">function scope</a>) variable, and not a <a href="http://www.php.net/manual/en/language.variables.scope.php#language.variables.scope.global" rel="nofollow"><code>global</code></a> one.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload