Can anybody explain "The Loop" of wordpress? - php

Im forced to work with wordpress, and if you work with it, you probably know what i mean:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Its working, no question. But i do not understand what this actually means. Its not a ternary operator nor anything else i know. Ive never seen a statement like this in any php-projects ive worked on. So i got several questions:
What is this line exactly doing? I know that it gets all posts, iterates over them and ... what is this the_post() doing? And what are these doubledots doing?
Is this Wordpress-Only or could it be used somwhere else too?
Where is the current post stored?
Ive already googled it, but there are no information regarding my problem, noone seems to be interested in how wordpress works. I am, but i do not get it. If somebody got an explanation for me, it would be great.

<?php define('WP_USE_THEMES', false); get_header(); ?>
The loop starts here:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
and ends here:
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
This is using PHP's alternative syntax for control structures, and could also be expressed as:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>

the post()
This function does not accept any parameters.
Return Values
This function does not return any values.
<?php
while ( have_posts() ) : the_post();
echo '<h2>';
the_title();
echo '</h2>';
the_content();
endwhile;
?>
have_posts()
Parameters
This function does not accept any parameters.
Return Values
(boolean)
True on success, false on failure.
Examples
The following example can be used to determine if any posts exist, and if they do, loop through them.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>
Note
Calling this function within the loop will cause an infinite loop. For example, see the following code:
<?php
while ( have_posts() ): the_post();
// Display post
if ( have_posts() ): // If this is the last post, the loop will start over
// Do something if this isn't the last post
endif;
endwhile;
?>
If you want to check if there are more posts in the current loop without this unfortunate side effect, you can use this function.
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}

1. What is LOOP
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags.
It will fetch data related to specific page
:(colon) is used to tell condition/loop starts from here. You can replace it with { }(bracket quotes)
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
2. Is this Wordpress-Only or could it be used somwhere else too?
yes of course you can use it.
You can access full wordpress functionality by including one core file with name of "wp-blog-header.php" that is located on root of wordpress directory.
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
Include this file in the top of your external file, you can access wordpress database, wordpress function , wordpress hooks too.
3. Where is the current post stored?
11 default tables are existed in wordpress database. You can see wp_posts table in database. all posts are store in this table.
suppose, if your are creating meta tag in your post, it will store in wp_postmeta

It's just an alternative syntax for:
if ( have_posts() ) { //open if
while ( have_posts() ) { //start while loop
the_post(); //call a function
See http://php.net/manual/en/control-structures.alternative-syntax.php
It's not wordpress specific and can be used in any php code.

To whom it may concern.
What is WordPress Loop?
When we save data inside WordPress(posts, pages, almost everything) data gets saved as a row inside our database fx. MySQL.
WordPress dynamically query the database and finds which row corresponds to the page you are on, and pulls that data then displays it in that section.
As this is a dynamic query and executed in a repeated manner, it is called a WordPress Loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
What are these doubledots doing? condition/loop starts from here :(colon) and is a ternary operators.
I think rest of the questions has been answered by others.

Related

Getting Post Thumbnail to show for Single Post Obect (WP + ACF)

I am having a hard time getting the Wordpress thumbnail to show up when I am using Advanced Custom Fields Pro Post Object.
My goal is to have the user select a single featured post to show up after the 6th post on the blog page.
This is my code (which is pretty much directly from ACF documentation):
<?php
$featured_post = get_field('featured_post', 'option');
if( $featured_post ): ?>
<?php echo esc_html( $featured_post->post_title ); ?></h3>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
This returns the correct title but the wrong featured image (from the post above).
The code above is called inside the loop on index.php inside the featured template part.
$counter = 1;
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', 'get_post_type()' );
//check the counter and display your content
if( $counter === 3 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'subscribe'); ?>
<?php } elseif ( $counter === 6 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'featured'); ?>
<?php }
//update the counter on every loop
$counter++;
endwhile;
I have tried the other code examples on the documentation page but those return all of my posts (again with only one thumbnail, not the correct thumbnail per post).
I am not sure where to go from here.
Anyone who can help would be greatly appreciated!
Since this is not in the loop context, the function does not find the correct post object set up to take the ID from. Use get_the_post_thumbnail instead, that takes the post id as parameter.
<?php echo get_the_post_thumbnail( $featured_post->ID ); ?>

Should I place wp_reset_postdata(); after endwhile; or endif; ?

I have spent some time reading through the WordPress Codex, as well as various themes, and can see that some developers insert <?php wp_reset_postdata(); ?> after the endif; in a Blog Loop whilst others insert the code between the endwhile; and endif; of a Blog Loop. I have tried both locations but have yet to see a difference. Is there a correct location?
This function supposed to reset the secondery query that you run.. the function the_postis letting you to use all the functions that you can run in the loop like the_title() the_content and so on..
So you reset the the_post function and after the endwhile; you can reset it already. and use your main query inside the if statment too if you like.
<?php
// this is the main query check if there is posts
if ( have_posts() ) :
// loop the main query post and run the_post() function on each post that you can use the function the_title() and so on..
while ( have_posts() ) : the_post();
the_title(); // title of the main query
// the is second query
$args = array( 'posts_per_page' => 3 );
$the_query = new WP_Query( $args );
// check if there is a posts in the second query
if ( $the_query->have_posts() ) :
// run the_post on the second query now you can use the functions..
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_excerpt();
endwhile;
// reset the second query
wp_reset_postdata();
/* here we can use the main query function already if we want..
its in case that you want to do something only if the second query have posts.
You can run here third query too and so on...
*/
the_title(); // title of the main query for example
endif;
endwhile;
endif;
?>
wp_reset_postdata() is only required if you have a secondary loop (you're running additional queries on a page). The purpose of the function is to restore the global post variable back to the current post in the main query.
Example:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
OUTPUT MAIN POSTS HERE
<?php endwhile; endif; ?>
In the code above I'm simply using the main loop to display posts. There's no reason to include wp_reset_postdata(). The global post variable is exactly what it's supposed to be.
If somewhere else on the page I decide to add a secondary loop, then I'll need wp_reset_postdata().
// This query is just an example. It works but isn't particularly useful.
$secondary_query = new WP_Query( array(
'post_type' => 'page'
) );
if ( $secondary_query->have_posts() ) :
// Separating if and while to make answer clearer.
while ( $secondary_query->have_posts() ) : $secondary_query->the_post();
// OUTPUT MORE STUFF HERE.
endwhile;
wp_reset_postdata();
endif;
To answer your original question: it usually comes after endwhile and before endif. It's the call to the_post() which actually changes the global post variable. If there are no posts then the post variable remain the same and there's no reason to use the reset function.

Post in wrong category

I've a weird problem, some posts appears in categories where they are not in.
When I look in my backoffice and filter by categories, some post appears there but they are not checked in.
The resultat is that in the front office they appear too.
This is my category.php (but I don't think it's the matter)
<?php
get_header();
?>
<section id="wrapper" class="page <?php echo get_query_var('cat'); ?>">
<div id="container">
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
query_posts('showposts=1&cat='.$cat_id);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
</div>
</section>
<?php
get_footer();
?>
I looked in the table "_term_relationships" and everything is right, they're not in the wrong categories.
So maybe someone have a clue to find out ?
PS : I'm using WPML, but if I desactive it, it's the same problem
You should not use query_posts(),
see (https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts)
try this:
<?php
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
$args = array( 'category' => $cat_id );
$query2 = new WP_Query($args);
if ( $query2->have_posts() ) :
while ( $query2->have_posts() ) :
$query2->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
?>
First of all, never use query_posts to construct any type of query
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Secondly, never change the the main query for a custom query on any type of archive page or home page. The correct way is to use pre_get_posts to alter the query variables before the main query executes. Check out this post I've done a while ago
Thirdly, category pages in Wordpress does work in a strange way. When a category page is visited, it will display posts from the selected category and posts from the selected category's child categories. I bet this is what you are seeing. This is pretty normal behavior. If you need to change this, have a look at this answer on WPSE by #ialocin. For the benefit of this answer, here is the solution
add_filter(
'parse_tax_query',
'wpse163572_do_not_include_children_in_category_archive_parse_tax_query'
);
function wpse163572_do_not_include_children_in_category_archive_parse_tax_query( $query ) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_category()
) {
// as seen here: https://wordpress.stackexchange.com/a/140952/22534
$query->tax_query->queries[0]['include_children'] = 0;
}
}

How to get wordpress main query posts as an array

I need to get the main query posts as an array. Example, in common tag page(tag.php), I need to get all the posts as an array ( like get_posts() do) and display it using some multiple loops instead of using default wordpress loop as shown under
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
$posts is the variable you're looking for it. It is the equivalent of get_posts results for the main query. It's in the global namespace, so in order to access it somewhere else you'll need to use the keyword global.
global $posts;
foreach( $posts as $a_post ) {
echo $a_post->post_title;
}

Wordpress list all posts (excerpt) php loop

I am creating a wordpress template and I now need some code that creates a loop to show all posts but not full posts, just excerpts.
Can anyone help please?
Use this code to generate the excerpt into the loop:
<?php
if(have_posts())
{
while(have_posts())
{
the_post();
the_excerpt();
}
}
?>
The above will generate only the excerpt of the posts. If you need extra options like post title, date, author and more you have to read the WordPress codex. http://codex.wordpress.org/Main_Page
You can read more by following the link given by markratledge below.
Everything you need to know - with examples - is here: http://codex.wordpress.org/The_Loop
The most basic loop is
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>
and you want to use the_excerpt() instead of the_content() See http://codex.wordpress.org/Function_Reference/the_excerpt

Categories