首页 WordPress函数 WordPress主循环The Loop
正文 评论

WordPress主循环The Loop

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 ><?php the_title(); ?></h1><div ><?php the_content(); ?></div></article><?php endwhile; ?><?php endif; ?>

同样的,以分类或首页的文章列表输出为例:

<?php if( have_posts() ) : ?><?php while( have_posts() ) : the_post(); ?><article ><h1><a href=\"<?php the_permalink(); ?>\" rel=\"bookmark\"><?php the_title_attribute(); ?></a></h1><footer ><time  datetime=\"<?php the_time( \'c\' ); ?>\" pubdate><?php the_time( \'Y-m-d\' ); ?></time><?php if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : ?><span >评论(<?php comments_popup_link( \'0\', \'1\', \'{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}\' ); ?>)</span><?php endif; ?></footer><p ><?php echo mb_strimwidth(wp_strip_all_tags($post->post_content, true), 0, 200, \'...\' ); ?></p></article><?php endwhile; ?><?php endif; ?>
-=||=-收藏赞 (0)
更多主题
单栏极简文艺WordPress博客主题:Diaspora主题
¥ 0 关注:21,681
详情
v1.0.0
小程序收录免费WordPress主题:XCX主题
¥ 0 关注:21,681
详情
简约黑白WordPress个人博客主题:Personal主题
¥ 0 关注:21,314
详情
v1.0.0
一款简约单栏的免费WordPress博客主题:itheme主题
¥ 0 关注:19,263
详情
功能丰富切轻量的WordPress虚拟资源主题:Rizhuti主题
¥ 599 关注:19,238
详情
v3.4.0
适用自媒体资讯的免费WordPress主题:Autumn主题
¥ 0 关注:16,484
详情
回复
暂无评论

不要再留垃圾评论了,主题君整理资源不容易,留几句鼓励的话吧。