Comment utiliser efficacement les boucles wordpress et les blocs de boucles de requête pour optimiser la présentation du contenu de votre site web.

什么是 WordPress 循环?

WordPress 循环(The Loop)是 WordPress 中用于显示帖子和页面内容的主要 PHP 代码结构。它是一个 PHP 代码块,用于从数据库中检索帖子、页面和其他类型的内容并将其显示在 WordPress 网站上。

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

WordPress 循环的工作原理

WordPress 循环是一个核心功能,它使得当访问者浏览你的网站时,可以看到文章和页面的内容。这个循环会自动从网站的数据库中提取文章信息,比如标题、发布日期、正文内容和其他相关数据。

下面是一个的 WordPress 循环的例子,帮你更好地理解它是如何工作的:

<?php get_header(); ?>
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title() ;?>
<?php the_author(); ?>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>

当你访问一个WordPress网站时,网站上显示的文章和页面内容都是通过一系列函数工作来展现的。这些函数每一个都有自己的特定任务,看看它们各自负责什么:

  • get_header() répondre en chantant get_footer():这两个函数分别负责在网页上添加页眉和页脚。页眉通常包含了网站的导航菜单和徽标,而页脚则可能包含联系信息、版权声明等。
  • if (have_posts()):这个判断用来检查是否有文章可显示。如果有,它返回真(true),这样循环就可以开始显示文章;如果没有,返回假(false),网页上就不会显示文章内容。
  • while (have_posts()):这是一个循环,它会一直运行,直到所有的文章都被显示完毕。它让网站可以一个接一个地展示每篇文章。
  • the_post():这个函数准备当前要显示的文章数据,使得其他函数如the_title()peut-êtrethe_post_thumbnail()可以正确地取得并展示这些信息。
  • the_title(), the_author(), the_post_thumbnail():这些函数用来显示文章的标题、作者和缩略图。在网页上,这些内容通常被包裹在HTML代码中,以便进行美观的格式化显示。
  • 结束语句 endwhile répondre en chantant endif:这些用于结束前面的循环和判断,确保代码的逻辑正确闭合。

Attention :默认情况下,循环会为每个帖子显示the_title()etthe_time()répondre en chantantthe_category() WordPress 模板标签。
或者,为你的 WordPress 循环使用以下PHP 代码语法。虽然其功能和元素相同,但语法格式不同。下面是使用替代语法编写的WordPress循环的一个示例:

<?php
get_header();
if ( have_posts() ) :
	while ( have_posts() ) : the_post();
	the_title();
	the_author();
	the_post_thumbnail();
	endwhile;
endif;
get_footer();
?>

如何在 WordPress 中使用循环

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

如何使用循环在帖子标题上建立超链接

在WordPress中,如果你想在帖子标题上添加超链接,点击标题可以直接跳转到帖子的详细页面,你可以使用the_permalink()函数。这个函数获取并输出当前帖子的永久链接(URL)。

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

如何使用 WordPress 循环显示内容、作者、日期和类别

如果你希望帖子包含作者、日期、类别和内容,应用以下 WordPress 代码:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
           <h2><?php the_title() ;?></h2> 
           <?php the_author(); ?> <?php the_time('F j, Y'); ?> <?php the_category(); ?> 
           <?php the_post_thumbnail(); ?> 
           <?php the_content(); ?> 
<?php endwhile; ?> 
<?php endif; ?>

条件标签是WordPress中的一系列特殊功能,它们帮助主题开发者根据不同的页面类型或情况决定如何显示网站内容。这些功能可以非常灵活地控制网站的显示逻辑,让不同页面显示不同的内容。

例如,如果你想让某些内容只在首页显示,可以使用 is_home() 函数。这个函数检查当前是否为博客的主页;如果是,就执行相应的代码显示特定内容。如果你想在文章的单独页面上显示某些内容,就可以使用 is_single() 函数。这个函数用来判断当前是否是单个帖子的页面。

以下是 WordPress 中最常见的条件标签:

  • is_front_page() – 用于网站的首页。
  • is_page() – 用于 WordPress 页面。
  • is_category() – 用于类别存档。
  • is_tag() – 用于标签存档。
  • is_archive() – 用于存档页面。
  • is_search() – 用于搜索结果页面。
  • is_author() – 用于作者档案。
  • is_404() – for is 404 ff 页面。

对于你想要在其上使用它们的任何页面,将它们设置为“vrai ".

我们使用带有if语句的is_front_page()标记,看示例:

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

<?php endwhile; ?> 
<?php endif; ?> 
<?php endif; ?>

WordPress 用户可以使用上面的代码在首页显示帖子。

如何使用模板文件自定义 WordPress 循环

要自定义WordPress网站上的帖子显示方式,你可以编辑主题文件夹中的几个关键模板文件。这些文件包括archive.php(用于显示归档页面)、index.php(主页)、catégorie.php(分类页面)和tag.php(标签页面)。

对于非块 WordPress 主题,需要使用 WordPress 循环。

WordPress 循环示例

如何在WordPress的Twenty Twenty主题的index.php文件中添加代码,使得不同类别的帖子有不同的显示样式。这种方式可以帮助读者更容易区分各种类别的帖子。

参考以下的WordPress循环代码,这段代码根据官方文档进行了注释,以帮助你理解每个部分的作用:

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

插入广告

使用 WordPress Loop,你可以将广告插入到帖子中,记住将广告代码部分替换为实际代码。:

<?php if (have_posts()) : ?> 
<?php $count = 0; ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php $count++; ?> 
  <?php if ($count == 2) : ?> 
          //广告代码
          <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
          <?php the_author(); ?> 
   <?php else : ?> 
          <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
          <?php the_author(); ?> 
  <?php endif; ?> 
<?php endwhile; ?> 
<?php endif; ?>

获取两个日期之间的帖子

为了更轻松地管理站点,可以使用 WordPress 循环轻松过滤掉两个日期之间的帖子。

<?php function filter_where($where = ’) {
        $where .= " AND post_date >= '2024-01-01' AND post_date <= '2024-05-01'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
while (have_posts()) :
      the_post();
      the_content();
endwhile;
?>

注意调整调整 2024-01-01 和 2024-05-01.

显示一年前发布的帖子

如果你想显示一年前发布的帖子,使用以下代码:

<?php
$current_day = date('j');
$last_year = date('Y')-1;
query_posts('day='.$current_day.'&year='.$last_year);
if (have_posts()):
    while (have_posts()) : the_post();
       the_title();
       the_excerpt();
    endwhile;
endif;
?>

列出即将发布的帖子

列出即将发布的帖子,以鼓励用户将来访问你的博客。

<?php query_posts('showposts=10&post_status=future'); ?> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <h2><?php the_title(); ?></h2> 
    <span class="datetime"><?php the_time('j.F Y'); ?></span></p> 
<?php endwhile; 
else: ?><p>没有安排未来的帖子。</p> 
<?php endif; ?>

显示最新置顶帖子

如果你想只显示最新的置顶帖,可以参考下面的代码:

<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );

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

制作存档页面

存档页面是你之前发布的帖子的集合。为你的网页访问者轻松显示过去的热门帖子。要创建它,参考下面的代码:

<?php
/*
Template Name: MyArchives
*/
?>
<?php get_header(); ?>
  <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><?php the_time('m/d/y') ?>: <a href="/fr/</?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
  </ul>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

了解 WordPress 查询循环块

WordPress 循环最重要的更新之一是引入了查询循环块。让我们了解它是什么以及如何使用它。

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

什么是查询循环块

WordPress 查询循环块在 WordPress 5.8 中引入,是对基本循环功能的强大补充。古腾堡编辑器使用它来在你的网站上显示帖子。

使用 WordPress 查询循环块,你可以创建复杂且具有视觉吸引力的WordPress 帖子格式,以特定顺序显示内容。可以应用各种过滤器,例如类别、标签或日期。

如何在 WordPress 中使用查询循环块

要添加查询循环块,在块编辑器中打开或添加新的 WordPress 帖子:

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

进入编辑器后,选择块插入器“+”图标:

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

搜索查询query并单击它

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

一个新的查询循环块将添加到你的帖子中。现在,检查以下部分以进行自定义。

自定义查询循环块

每当添加新的查询循环块时,你都会看到两个选项:

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

“选择”选项允许你在各种不同的模式之间进行选择。只需选择一个喜欢的即可

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

每个查询循环块都由各种嵌套块组成,例如帖子标题或摘录。如果你想知道查询循环块中使用了哪些块,请打开文档概述工具。在这里你将看到所有嵌套块并能够在它们之间导航:

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

existent开始空白选项中,您将看到各种模板。选择你喜欢的一个。在此示例中,我们将添加带有帖子标题和摘录的模板:

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

添加查询循环块后,你可以使用块工具栏或块设置部分对其进行自定义。

  • 块工具栏

块工具栏是自定义 WordPress 上任何块(包括查询块)的主要工具。

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

在这里,你可以将查询循环转换为列或组块、在编辑器中移动它、调整对齐方式或更改默认视图。最值得注意的选项是显示设置répondre en chantant替换: :

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

显示设置允许你选择要显示的 WordPress 帖子数量,以及设置在显示首选内容之前应跳过其中的数量。

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

如果你没有看到显示设置,请确保在“块设置”部分中禁用从模板继承查询选项。

另一个有用的 WordPress 功能是Remplacer。有了它,你可以轻松地将当前模式替换为任何其他模式:

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

其他重要的块设置可以通过单击编辑器右上角的"Réglages"按钮找到:

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

查询循环块不仅允许你定制帖子的显示布局、应用过滤器或添加自定义CSS类,还支持分页功能,这在管理内容丰富的网站时尤其有用。

当你的网站包含大量帖子时,通过分页,查询循环块能在多个页面上展示这些帖子。这样的设置帮助访问者通过页码轻松导航你的博客:

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

rendre un verdict

WordPress 循环是一个强大的工具,它允许开发者在网站上有效地展示帖子和页面内容。通过使用基本的循环结构和各种条件标签,开发者可以根据页面类型或特定条件来定制内容的显示。WordPress 循环可以使任何网站受益,因为它安排了显示帖子的方式,提高了访问者的浏览体验。。


Contactez nous
Vous ne pouvez pas lire l'article ? Contactez-nous pour une réponse gratuite ! Aide gratuite pour les sites personnels et les sites de petites entreprises !
Tel : 020-2206-9892
QQ咨询:1025174874
(iii) Courriel : info@361sale.com
Horaires de travail : du lundi au vendredi, de 9h30 à 18h30, jours fériés.
© Déclaration de reproduction
Cet article a été écrit par : xiesong
LA FIN
Si vous l'aimez, soutenez-le.
félicitations0 partager (joies, avantages, privilèges, etc.) avec les autres
avatar de xiesong - Photon Flux | Service professionnel de réparation de WordPress, dans le monde entier, réponse rapide
commentaires achat de canapé

Veuillez vous connecter pour poster un commentaire

    Pas de commentaires