首页 视频课程 主题开发课程第05章、文章输出 WordPress循环中的常用函数介绍

WordPress循环中的常用函数介绍

2023-06-11 / 250阅

WordPress循环中常用函数

在WordPress中,循环是一个非常重要的概念,它用于遍历并展示文章/页面/自定义类型等的内容。在循环中,开发人员可以使用一系列的函数来展示文章的各种元素,例如标题、摘要、作者名、时间戳、分类、标签等等。下面是一些在WordPress循环中经常使用的函数。

1. the_title()

用于展示文章的标题。

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

2. the_excerpt()

用于展示文章的摘要。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p><?php the_excerpt(); ?></p>
<?php endwhile; endif; ?> 

3. the_content()

用于展示文章的正文内容。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php the_content(); ?>
<?php endwhile; endif; ?> 

4. the_author()

用于展示文章的作者。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p>Written by: <?php the_author(); ?></p>
<?php endwhile; endif; ?> 

5. the_time()

用于展示文章的发布时间。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p>Published on: <?php the_time('F j, Y'); ?></p>
<?php endwhile; endif; ?> 

6. the_category()

用于展示文章的分类(多个分类之间用逗号隔开)。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p>Category: <?php the_category( ', ' ); ?></p>
<?php endwhile; endif; ?> 

7. the_tags()

用于展示文章的标签(多个标签之间用逗号隔开)。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <p>Tags: <?php the_tags( '', ', ' ); ?></p>
<?php endwhile; endif; ?> 

8. get_the_post_thumbnail()

用于展示文章的特色图片。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php if ( has_post_thumbnail() ) { echo get_the_post_thumbnail( $post->ID, 'thumbnail' ); } ?>
<?php endwhile; endif; ?> 

以上是一些在WordPress循环中常用的函数,开发人员可以根据需要灵活使用。

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录