The Loop
in_the_loop() ----------Checks if the WordPress loop is active.
have_posts() ----------Checks if there are posts for the loop.
the_post() ----------Sets the next post in the loop and global $post.
setup_postdata() ----------Sets global $post.
the_ID() ----------Displays the ID of the current post.
the_title() ----------Displays current post title.
the_title_attribute() ----------Displays post title for html tag attribute.
the_content() ----------Displays post content.
the_excerpt() ----------Displays the excerpt (quote) of the post, with [...] at the end.
the_excerpt_rss() ----------Displays the excerpt (quote) (for RSS).
get_permalink() ----------Gets post URL.
the_permalink() ----------Displays post URL.
comments_number() ----------Displays post's number of comments.
edit_post_link() ----------Displays edit post link (A html tag).
the_date() ----------Displays/retrieves post publication date.
get_the_date() ----------Gets the post creation date.
the_time() ----------Displays post publication date.
get_post_time() ----------Gets time (date) of post publication.
the_modified_date() ----------Displays time (date) when post was changed.
the_post_thumbnail() ----------Displays html code of post thumbnail image.
get_post_thumbnail_id() ----------Gets the post thumbnail ID.
has_post_thumbnail() ----------Whether or not the post has a thumbnail. Conditional tag.
the_post_thumbnail_url() ----------Displays the URL of the post thumbnail.
the_attachment_link() ----------Displays the link (A tag) of the attachment or the attachment page.
get_attachment_link() ----------Gets the URL to the attachment page.
wp_get_attachment_link() ----------Gets the link (A tag) of the attachment or the attachment page.
the_tags() ----------Displays links to the post tags.
the_category() ----------Displays post categories as links.
the_taxonomies() ----------Displays links <a> to the post terms.
in_category() ----------Checks if the post belongs to a category.
sticky_class() ----------Displays a "sticky" class if it is a sticky post.
is_sticky() ----------Checks if the post is sticky to the home page.
the_meta() ----------Displays post meta-fields in <li> list.
get_post_format() ----------Gets the post format: quote, status, video, audio.
the_author() ----------Displays post's author name.
get_the_author() ----------Gets post's author name (display_name).
the_author_link() ----------Displays link (A tag) to the site of post's author.
get_the_author_link() ----------Gets link (A tag) to the site of post's author.
the_author_posts() ----------Displays the total number of posts written by the author.
the_author_posts_link() ----------Displays link (A tag) to the post author's archive page .
the_author_meta() ----------Displays specified meta-field of the post author (wp user).
get_the_author_meta() ----------Gets specified meta-field of the post author (wp user).
the_modified_author() ----------Displays the name of the author who last modified the post.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- Post output, loop functions: the_title(), etc. -->
<?php endwhile; else: ?>
No posts.
<?php endif; ?>
<?php if ( have_posts() ){ while ( have_posts() ){ the_post(); ?>
<!-- Post output, loop functions: the_title(), etc. -->
<?php } } else { ?>
No posts.
<?php } ?>
<?php while ( have_posts() ){ the_post(); ?>
<!-- Post output, loop functions: the_title(), etc. -->
<?php } ?>
<?php if ( ! have_posts() ){ ?>
No posts.
<?php } ?>
<?php
global $post;
$myposts = get_posts([
'numberposts' => 5,
'offset' => 1,
'category' => 1
]);
if( $myposts ){
foreach( $myposts as $post ){
setup_postdata( $post );
?>
<!-- Post output, loop functions: the_title(), etc. -->
<?php
}
} else {
// No posts found
}
wp_reset_postdata(); // Reset $post
?>
<?php
global $post;
$query = new WP_Query( [
'posts_per_page' => 5,
'orderby' => 'comment_count',
] );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<!-- Post output, loop functions: the_title(), etc. -->
<?php
}
} else {
// No posts found
}
wp_reset_postdata(); // Reset $post
?>