!
也想出现在这里? 联系我们
广告位

WordPress主循环The Loop

WordPress的主循环The Loop用来输出内容,例如:文章内容、页面内容、文章列表。一些WordPress函数规定必须在Loop循环中使用,这是因为它们需要获取文章的ID

例如这些函数:

  • the_title():输出标题;
  • the_time():输出文章发表时间;
  • the_category():输出文章的分类;
  • the_permalink():输出文章的链接;

WordPress函数的命名很科学,从字面上基本就能猜到函数作用,通常带有the的,都需要获取当前文章的ID,需要在主循环中使用。

下面来看看WordPress主循环The Loop相关tag/23/" target="_blank">代码:

<?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(); ?&gt;&gt;
<h1 class="title"><?php the_title(); ?></h1>
<div class="content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?&gt;

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

<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<article class="entry">
<h1><a href="https://www.ztjun.com/<?php the_permalink(); ?&gt;" rel="bookmark"><?php the_title_attribute(); ?></a></h1>
<footer class="post-meta">
<time class="dashicons-before dashicons-clock" datetime="<?php the_time( 'c' ); ?&gt;" 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; ?>

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress加载侧边栏模板标签:get_sidebar

2021-4-16 5:30:03

WordPress教程

WordPress自定义Page页面模板

2021-4-16 17:30:03

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索