天天看點

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首頁面的模闆。

WordPress開發入門06:條件判斷與循環

​​if else while xuhss.com01 - WordPress開發入門06:條件判斷與循環​​

預設情況下,它将控制部落格的首頁顯示的文章數目。

是以打開這個檔案,來看看控制它的循環是如何工作的:

可以看到這裡以 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這個模闆檔案。

WordPress開發入門06:條件判斷與循環

​​if else while xuhss.com02 - WordPress開發入門06:條件判斷與循環​​

while語句詳解

為了講解while語句,我們需要在我們的網站上添加了三篇部落格文章。

WordPress開發入門06:條件判斷與循環

​​if else while xuhss.com05 - WordPress開發入門06:條件判斷與循環​​

建立完文章後,進入儀表盤,在“設定”下面的“閱讀”下,我們将部落格文章至多顯示更改為:1

WordPress開發入門06:條件判斷與循環

​​if else while xuhss.com06 - WordPress開發入門06:條件判斷與循環​​

來到部落格首頁,這裡隻看到一篇文章,其實,這3篇文章被分開到不同的頁面顯示。

同樣地,如果我改回為10,來到部落格首頁,你會看到部落格首頁會顯示全部的3篇文章。

這就剛好說明了這個while循環是如何工作的。

沒有文章資料的情況

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

WordPress開發入門06:條件判斷與循環

​​if else while xuhss.com07 - WordPress開發入門06:條件判斷與循環​​

對應代碼中,就是如果失敗,它将運作else中的語句。其中包括get_template_part(’content’,’none’)。也就是會執行content-none.php。

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

get_template_part()模闆标簽