WordPress开发入门06:条件判断与循环

任何语言都会有条件判断语句,PHP也不例外。条件语句可以让你测试某个条件是否为真,然后执行条件为真的代码。WordPress中最简单,最常见的条件语句是 if 语句,或者 **if else **语句。

WordPress中条件语句的常见用途是:检测是否有可用的发布文章。伪代码就像这样:

IF (There are posts)
    Echo post title
    Echo post content
ELSE
    Echo “Sorry no posts are available”

如果,那么我们可以编写代码来显示这些文章。

如果没有,则执行条件语句的else部分,并输出一条消息,提示没有可用的发布文章。

这个简单的伪代码例子只适用于检查和显示单篇文章。但是大多数时候,在WordPress中,我们会把条件语句循环语句结合起来,因为我们要显示的是文章的列表。

如何将条件语句与简单循环语句相结合

在WordPress中找到的最简单的循环是WHILE循环。伪代码就像这样:

IF (There are posts)
    WHILE(Have post)
        Echo post title
        Echo post content
ELSE
    Echo “Sorry no posts are available”

只要给定的条件为真,while循环就会重复执行相同的代码。 所以,如果想要使用循环,可以像这样使用WHILE循环。 之前的代码只能判断一篇文章。 而现在的代码,只要有文章,我们就会通过代码不断的显示出来。

不过,这些只是伪代码,可能大家还没有完全理解。 所以让我们来结合WordPress主题中的一些实际代码,看看条件语句和WHILE循环是如何结合在一起的。

结合WordPress主题中的条件判断与循环

所以,首先,可以登录WordPress后台,来到主题,我们切换到 Twenty Fifteen 主题。

然后,我们来到对应的代码中,index.php是控制WordPress主页面的模板。

默认情况下,它将控制博客的主页显示的文章数目。

所以打开这个文件,来看看控制它的循环是如何工作的:

可以看到这里以 if 条件语句和 have_posts 模板标签开头

                <?php if ( have_posts() ) : ?> /***if 条件语句和 have_posts 模板标签开头***/

            <?php if ( is_home() && ! is_front_page() ) : ?>
                <header>
                    <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
                </header>
            <?php endif; ?>

            <?php
            // Start the loop.
            while ( have_posts() ) : the_post();

                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( 'content', get_post_format() );

            // End the loop.
            endwhile;

            // Previous/next page navigation.
            the_posts_pagination( array(
                'prev_text'          => __( 'Previous page', 'twentyfifteen' ),
                'next_text'          => __( 'Next page', 'twentyfifteen' ),
                'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
            ) );

        // If no content, include the "No posts found" template.
        else :
            get_template_part( 'content', 'none' );

        endif;  /***endif:表示条件判断语句的结束***/
        ?>

拖到最下面,有一个endif:,表示条件语句的结束。所以这是最外层的if条件语句。

在代码之间我们可以看到也有else语句。

所以这段代码的大致意思是

如果有文章数据

那么,这里会执行到else之前的所有代码。

            <?php if ( is_home() && ! is_front_page() ) : ?>
                <header>
                    <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
                </header>
            <?php endif; ?>

我们可以看到另外一个条件语句是在这里运行的,如果是home主页并且不是front page,那么输出提示信息。

                       // Start the loop.
            while ( have_posts() ) : the_post();

                /*
                 * Include the Post-Format-specific template for the content.
                 * If you want to override this in a child theme, then include a file
                 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                 */
                get_template_part( 'content', get_post_format() );

            // End the loop.
            endwhile;

然后,可以看到启动循环。当有文章时,则执行以下操作。

所以这个while循环语句只是检查是否有文章。只要我们有显示的文章,while语句将继续运行。

然而,可以看到while循环中只有一行代码:

                get_template_part( 'content', get_post_format() );

这行代码实际上会把:将要执行的操作放在不同的模板文件中,该模板文件将被命名为“content”,后面跟上Post格式。如果进入主题文件中,可以看到有content-link。content-none,content-page,content-search,还有常规的content:

You must be logged in to view the hidden contents.

所以这里将根据不同的格式来执行不同类型的文件。

如果没有文章数据

那么就执行这个 get_template_part 找到一个名为(’content’,’none’)的文件,也就是说,它会运行content-none这个模板文件。

while语句详解

为了讲解while语句,我们需要在我们的网站上添加了三篇博客文章。

创建完文章后,进入仪表盘,在“设置”下面的“阅读”下,我们将博客文章至多显示更改为:1

来到博客首页,这里只看到一篇文章,其实,这3篇文章被分开到不同的页面显示

同样地,如果我改回为10,来到博客首页,你会看到博客首页会显示全部的3篇文章。

这就刚好说明了这个while循环是如何工作的。

没有文章数据的情况

如果删除所有的文章,我们可以首页提示:未找到

对应代码中,就是如果失败,它将运行else中的语句。其中包括get_template_part(’content’,’none’)。也就是会执行content-none.php。

如果我们打开了 content-none.php 文件,我们可以看到它显示“Nothing Found”(因为我的是中文版,所以会翻译为“未找到”):

    <header class="page-header">
        <h1 class="page-title"><?php _e( 'Nothing Found', 'twentyfifteen' ); ?></h1>
    </header><!-- .page-header -->

get_template_part()模板标签

如果你要开发自己的主题,你可能不会在这里使用 get_template_part 附加模板,你可能会直接放置循环所需的实际代码。

但是WordPress还是建议大家使用模板文件包含,而不是将所有代码直接包含在主循环中。这样代码的结构将会简单清晰

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚坏叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值