while(have_posts()) {}
This in built function of php runs the following program we have posts. But 1 concept in not clear yet. This while loop will run n number of times or if we don't use or fetch the post using while loop then will the loop go infinite times?
To understand this we must be clear of the for loop
If we use the_post(); function of wordpress then only we can make the loop finite.
Make sure to use the_post(); to fetch the post and this is important to finish the loop.
Else the while loop will go infinite.
Example of correct loop :
<?php
while(have_posts()){
the_post();
echo"hello";
}
?>
The above loop will print hello = no. of posts.
Example of incorrect loop :
<?php
while(have_posts()){
echo"hello";
}
?>
The above loop will print hello infinite no. of times.
the_title();
The php function fetches the title of the blogpost. To make this function work you must run the function inside an appropriate while loop.
Example :
<?php
while(have_posts()){
the_post();
the_title();
}
?>
the_content();
This php function fetches the content of the blogpost. To make this function work you must run the function inside an appropriate while loop.
Example :
<?php
while(have_posts()){
the_post();
echo "<br>";
the_title();
echo "<br>";
the_content();
}
?>
the_permalink();
This php function fetches the content of the blogpost. To make this function work you must run the function inside an appropriate while loop.
Further you can wrap this function inside an anchor tag.
Example :
<?php
while(have_posts()){
the_post();
echo "<br>";
the_permalink();
}
?>
get_header();
This php function fetches all the code inside header.php file. This makes it really easy to make a same header for the entire wordpress website. This function also enhances code reusability.
You must name the file as header.php to make get_header() function work.
Example:
<?php
get_header();
?>
Comments
Post a Comment