How to efficiently use wordpress loops and query loop blocks to optimize website content presentation

What is a WordPress Loop?

The WordPress Loop is the main PHP code structure used in WordPress to display post and page content. It is a block of PHP code used to retrieve posts, pages, and other types of content from a database and display them on a WordPress website.

图片[1]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

How WordPress loops work

WordPress loops are a core feature that makes it possible to see the content of posts and pages when visitors browse your website. This loop automatically pulls post information from your website's database, such as title, publish date, body content and other relevant data.

Here is an example of a WordPress loop to help you better understand how it works:

When you visit a WordPress website, the content of the posts and pages displayed on the site are presented through a series of functions working. Each of these functions has its own specific task, so take a look at what each of them is responsible for:

  • get_header() respond in singing get_footer(): These two functions are responsible for adding headers and footers to web pages, respectively. The header usually contains the site's navigation menu and logo, while the footer may contain contact information, copyright notices, etc.
  • if (have_posts()): This judgment is used to check if there is an article to display. If there is, it returns true so that the loop can start displaying the article; if there is not, it returns false so that the content of the article will not be displayed on the web page.
  • while (have_posts()): This is a loop that will run until all the articles have been displayed. It allows the site to display each article one after the other.
  • the_post(): This function prepares the current article data to be displayed, allowing other functions such asthe_title()maybethe_post_thumbnail()This information can be obtained and presented correctly.
  • the_title(), the_author(), the_post_thumbnail(): These functions are used to display the title, author and thumbnail of an article. On web pages, these are usually wrapped in HTML code for aesthetically pleasing formatted display.
  • closing statement endwhile respond in singing endif: These are used to close the preceding loops and judgments, ensuring that the code's logic is closed correctly.

Attention:By default, the loop displays for each post thethe_title(),the_time()respond in singingthe_category() WordPress template tags.
Alternatively, use the following PHP code syntax for your WordPress loops. While the functions and elements are the same, the syntax is formatted differently. Below is an example of a WordPress loop written using the alternative syntax:

endwhile; endif; get_footer(); ?

How to use loops in WordPress

图片[2]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

How to use loops to create hyperlinks on post titles

In WordPress, if you want to add a hyperlink to the title of a post, so that clicking on the title can directly jump to the post's detail page, you can use thethe_permalink()function. This function gets and outputs the current post's permalink (URL).

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
           <h2><a href="/en/</?php the_permalink(); ?>"><?php the_title() ;?></a></h2>

How to use WordPress to cycle through content, authors, dates and categories

Apply the following WordPress code if you want the post to contain author, date, category and content:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
           <h2><?php the_title() ;?></h2>

Conditional tags are a set of special features in WordPress that help theme developers decide how to display website content based on different page types or situations. These features allow for very flexible control over the display logic of a website, allowing different pages to display different content.

For example, if you want certain content to be displayed only on the front page, you can use the is_home() function. This function checks to see if the current page is the home page of the blog; if so, it executes the appropriate code to display specific content. If you want to display something on a separate page of a post, you can use the is_single() function. This function is used to determine if the current page is a single post.

Here are the most common conditional tags in WordPress:

  • is_front_page() - Used for the home page of the site.
  • is_page() - for WordPress pages.
  • is_category() - For category archives.
  • is_tag() - For label archiving.
  • is_archive() - Used to archive pages.
  • is_search() - Used on the search results page.
  • is_author() - For author files.
  • is_404() - for is 404 ff page.

For any pages on which you want to use them, set them to "true "The

We use the function with theif statement is_front_page()Marker, see example:

<h2><?php the_title() ;?></h2>

WordPress users can use the code above to display posts on the front page.

How to Customize WordPress Loops Using Template Files

To customize the way posts are displayed on your WordPress site, you can edit several key template files in the theme folder. These files includearchive.php(for displaying the archive page),index.php(home page),category.php(category page) andtag.php(tabbed page).

For non-block WordPress themes, WordPress loops are required.

WordPress Loop Example

How to make the Twenty Twenty theme in WordPressindex.phpCode is added to the file to make different categories of posts have different display styles. This approach helps readers to distinguish between various categories of posts more easily.

Refer to the following WordPress loop code which is commented according to the official documentation to help you understand what each part does:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div no numeric noise key 1011>
        <h2>
            <a href="/en/</?php the_permalink(); ?>"><?php the_title(); ?></a>
        </h2>
        <p class="post-category">Category:</p>
        <?php if ( in_category(3) ) : ?>
            <div style="color: red;"> <!-- 这里设置特别样式 -->
                <?php the_content(); ?>
            </div>
        
    </div>
<?php endwhile; else : ?>
    <p>No posts found.</p>
<?php endif; ?>

Insertion of advertisements

Using WordPress Loop, you can insert ads into posts.Remember to put the ad codePartially replace with actual code. :

<?phpAdvertising Code

<a href="">

<a href="">

Get posts between two dates

For easier site management, posts between two dates can be easily filtered out using a WordPress loop.

= '2024-01-01' AND post_date endwhile; ?

Note the adjustments. 2024-01-01 and 2024-05-01The

Showing posts from a year ago

If you want to display posts that were made a year ago, use the following code:

endif; ?

List upcoming posts

List upcoming posts to encourage users to visit your blog in the future.

<h2><?php the_title(); ?></h2> 
    <span class="datetime"><?php the_time('j.F Y'); ?></span></p> 
<?php endwhile; 
else: ?><p>There are no scheduled future posts.</p> 
<?php endif; ?>

Show latest top posts

If you want to show only the latest top posts, you can refer to the code below:

$sticky, 'caller_get_posts' => 1 ) );

if (have_posts()) :
    while (have_posts()) : the_post().
        the_title().
        the_excerpt();
    endwhile; endif; endif
endif.
? >endif; ?

Creating an archive page

An archive page is a collection of your previously published posts. Easily display past popular posts for your web visitors. To create it, refer to the code below:

<h2><?php $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts); ?>
<h2><?php echo $numposts.' recipes published since January 01, 2024'; ?>
  </h2>
  <ul id="archive-list">
    <?php
    $myposts = get_posts('numberposts=-1&');
    foreach($myposts as $post) : ?>
      <li>. <a href="/en/</?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
  </ul>

Understanding WordPress Query Loop Blocks

One of the most important updates to WordPress loops is the introduction of the query loop block. Let's learn what it is and how to use it.

图片[3]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

What is a query loop block

The WordPress Query Loop block was introduced in WordPress 5.8 and is a powerful addition to the basic loop functionality. The Gutenberg editor uses it to display posts on your site.

Using WordPress Query Loop Blocks, you can create complex and visually appealing WordPress post formats that display content in a specific order. Various filters can be applied, such as categories, tags or dates.

How to Use Query Loop Blocks in WordPress

To add a query loop block, open or add a new WordPress post in the block editor:

图片[4]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Once in the editor, select the blockInserter "+"Icons:

图片[5]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Search Queriesqueryand click on it

图片[6]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

A new query loop block will be added to your post. Now, check the following sections for customization.

Custom Query Loop Blocks

Whenever you add a new query loop block, you will see two options:

图片[7]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

"Choose."Options allow you to choose between a variety of different modes. Simply choose a favorite

图片[8]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Each query loop block consists of various nested blocks, such as post titles or excerpts. If you want to know which blocks are used in a query loop block, open the Document Overview tool. Here you will see all nested blocks and be able to navigate between them:

图片[9]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应
  • Start blank

existStart blankIn the options, you will see various templates. Select the one you like. In this example, we will add the template with post title and excerpt:

图片[10]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

After adding a query loop block, you can customize it using the Block Toolbar or the Block Settings section.

  • Block Toolbar

The Block Toolbar is the main tool for customizing any block on WordPress, including query blocks.

图片[11]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Here you can convert a query loop into a column or group block, move it around in the editor, adjust the alignment, or change the default view. The most notable options areDisplay Settingsrespond in singinginterchangeability::

图片[12]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

The display settings allow you to select the number of WordPress posts to display, as well as set the number of them that should be skipped before the preferred content is displayed.

图片[13]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

If you don't see the display settings, make sure they are disabled in the Block Settings section.Inherit query options from the template.

Another useful WordPress feature isReplace. With it, you can easily replace the current mode with any other mode:

图片[14]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应
  • Block setup

Other important block settings can be accessed by clicking on the editor's top-right corner of the"Settings"button is found:

图片[15]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

Not only does the Query Loop block allow you to customize the display layout of your posts, apply filters, or add custom CSS classes, it also supports pagination, which is especially useful when managing content-rich websites.

When your site contains a large number of posts, a query loop block can display these posts on multiple pages through pagination. Such a setup helps visitors to easily navigate your blog through page numbers:

图片[16]-如何高效使用wordpress循环与查询循环块优化网站内容展示-光子波动网 | 专业WordPress修复服务,全球范围,快速响应

reach a verdict

WordPress loops are a powerful tool that allows developers to effectively display posts and page content on a website. By using a basic loop structure and various conditional tags, developers can customize the display of content based on page types or specific conditions.WordPress loops can benefit any website by arranging the way posts are displayed, improving the visitor's browsing experience.


Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
Author: xiesong
THE END
If you like it, support it.
kudos0 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments