Wordpress Loop get_the_id() -


i tried following functions in header.php, footer.php, single.php etc.

var_dump(in_the_loop()); var_dump(get_the_id()); 

the first function gives false (meaning not in loop) , second function gives post id every single time.

description of get_the_id() wordpress :

retrieve numeric id of current post. tag must within loop.

i want simple explanation hell going on why post id if call function out of loop !?

must little strong get_the_id() ...delivers evil eye wordpress.

it works in header , non-loop (confirmed).

please note post/page interchangeable in conversation.

think of wp way -> have post id in way, time, every page, unless weird stuff or talk non-page edge cases. when @ install root (such site.com/) there posts being called, has displayed. there other settings impact post/page such static front page settings. on category listing, if there pages, got first id returned before loop.

on post/pages page id (more or less0 set before loop. result of url (pretty or ?p=123 format) dictating content. using pretty names, page @ site.com/foo-bar/ try if there content available via permalink rules "foo-bar". if there content, post id obtained. (simplified)

later in page build loop. however, before loop offered opportunities change, sort, or augment loop - such changing page ids looped or sorting.

regarding in_the_loop(), wp says

"true if caller within loop, false if loop hasn't started or has ended." via http://codex.wordpress.org/function_reference/in_the_loop

in_the_loop() evaluates if loop in action (loop being key wp world). important - when in loop wp can iterate on multiple page/post (ids).

i don't have 100% bulletproof explanation how id shows, when dig api , various methods hooking might result.

i understand confusion , agree you. think wp intended get_the_id() loop based tool, outside loop unpredictable results.

hope helps, enjoy working in wp, , hope to.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -