Answer the question
In order to leave comments, you need to log in
Why are Loop dependent functions used outside of Loop?
I'm new to Wordpress, getting familiar with Loop. I got to the variables / functions that work inside the loop. For example, get_permalink() without an ID attribute should be used inside a loop. In the code of the theme, formed using underscores, there is a content.php template, where there is such a piece
if ( is_singular() ) :
the_title( '<h1 class="entry-title">', '</h1>' );
else :
the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
endif;
Answer the question
In order to leave comments, you need to log in
Because WordPress really likes to use global variables. The function get_permalink()
takes an object or post_id as an argument $post
(i.e., you can pass the id of any post or any object $post
in principle), and by default this argument refers to the global $post
one, which is why it works outside the loop and without an argument.
These functions are meant to be used outside of the loop because that's often what's needed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question