B
B
byshabay2021-09-02 16:02:17
WordPress
byshabay, 2021-09-02 16:02:17

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;


It would seem that outside the loop, then get_permalink() should not work without an attribute, but it does. Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
thatguy1, 2021-09-02
@byshabay

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 $postin principle), and by default this argument refers to the global $postone, 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 question

Ask a Question

731 491 924 answers to any question