WordPress主循环The Loop详解
文 / @WordPress主题
WordPress的主循环The Loop用来输出内容,例如:文章内容、页面内容、文章列表。一些WordPress函数规定必须在Loop循环中使用,这是因为它们需要获取文章的ID
例如这些函数:
- the_title():输出标题;
- the_time():输出文章发表时间;
- the_category():输出文章的分类;
- the_permalink():输出文章的链接;
WordPress函数的命名很科学,从字面上基本就能猜到函数作用,通常带有the的,都需要获取当前文章的ID,需要在主循环中使用。
下面来看看WordPress主循环The Loop相关代码:
<?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); ?> 这里是要输出的具体内容... <?php endwhile; ?> <?php endif; ?>
以文章和页面为例,输出文章/页面的标题和内容:
<?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <h1 class="title"><?php the_title(); ?></h1> <div class="content"> <?php the_content(); ?> </div> </article> <?php endwhile; ?> <?php endif; ?>
同样的,以分类或首页的文章列表输出为例:
<?php if( have_posts() ) : ?> <?php while( have_posts() ) : the_post(); ?> <article class="entry"> <h1><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title_attribute(); ?></a></h1> <footer class="post-meta"> <time class="dashicons-before dashicons-clock" datetime="<?php the_time( 'c' ); ?>" pubdate><?php the_time( 'Y-m-d' ); ?></time> <?php if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : ?> <span class="comments-links dashicons-before dashicons-admin-comments">评论(<?php comments_popup_link( '0', '1', '%' ); ?>)</span> <?php endif; ?> </footer> <p class="excerpt"><?php echo mb_strimwidth(wp_strip_all_tags($post->post_content, true), 0, 200, '...' ); ?></p> </article> <?php endwhile; ?> <?php endif; ?>

相关文章
-
搭建一个WordPress网站需要多少成本 2023-11-06 00:09:51
-
Symlink介绍(附:如何使用Symlink进行WordPress开发) 2023-11-05 23:38:32
-
让WordPress实现数据库同步的插件:HyperDB 2023-10-24 23:40:49
-
allegro电商平台值得做吗(附:2023年Allegro注册流程指南) 2023-10-08 21:53:39
-
印度跨境电商平台有哪些(附:印度跨境电商做什么产品好) 2023-10-08 21:34:23
-
跨境电商必看的几大海外二手电商平台 2023-10-08 18:04:42
-
WordPress同城互联网产品解决方案:UBASE 2023-10-03 16:40:39
-
WordPress网站的安全插件:wordfence 2023-09-14 09:25:18
-
WordPress 6.3 支持在手动更新插件和主题失败后回滚旧版本 2023-08-28 16:58:45
-
WordPress 6.3版本(2023年8月)性能提升了哪些? 2023-08-28 16:56:02