O
O
Outoverlay2015-12-30 01:25:15
PHP
Outoverlay, 2015-12-30 01:25:15

How does wordpress know what a function is passing when it doesn't have a parameter?

The code shows that there is a foreach loop, inside it there is the_title () function, and yet, they have no parameters. How does he understand that for example it is necessary to display three records?

$args = array( 'posts_per_page' => 3 );
$lastposts = get_posts( $args );
foreach( $lastposts as $post ){ setup_postdata($post);
  ?>
  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <?php 
  the_content(); 
}
wp_reset_postdata();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri, 2015-12-30
@riky

this part of the code knows about 3 records

$args = array( 'posts_per_page' => 3 );
$lastposts = get_posts( $args );

a long time ago I looked, inside WordPress there is such a thing as the current post.
looks like setup_postdata($post);
and changes this global variable to the passed post
the_title(); / the_content();
just display the data of the current

N
nozzy, 2015-12-30
@nozzy

the_title() internally calls get_the_title()
https://developer.wordpress.org/reference/function...
and get_the_title() has one global parameter by default - global $post

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question