2014-02-11 25 views
0

林被迫與WordPress的工作,如果你使用它,你大概知道我的意思:有人可以解釋wordpress的「The Loop」嗎?

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

它的工作,毫無疑問。但我不明白這實際上意味着什麼。它不是一個三元運營商,也不是我所知道的。我從未在任何php項目中看到類似的聲明。所以我有幾個問題:

  1. 這條線正在做什麼?我知道它得到所有帖子,迭代他們和...這是什麼the_post()在做什麼?這些doubledots在做什麼?
  2. 是這個WordPress的唯一或可以用於其他地方嗎?
  3. 當前帖子存儲在哪裏?

我已經使用了它,但沒有關於我的問題的信息,似乎沒有人對wordpress的工作方式感興趣。我是,但我不明白。如果有人爲我解釋,那會很好。

+0

店除了爲控制結構,你可以檢查HTTP替代語法已經給出了答案://wordpress.stackexchange。 com/questions/117219/why-should-i-put-ifhave-posts-is-whilehave-posts-not-enough它是如何工作的WP –

+0

「雙點」,你的意思是冒號,是三元運算符,參見http ://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary – burzum

+0

啊冒號......不知道他們是怎麼叫的。至少你知道我的意思。但是這些怎麼可能是三元運算符,我以爲你總是需要結構'表達式? if_true_statement:if_false_statement' –

回答

3
<?php define('WP_USE_THEMES', false); get_header(); ?> 

The loop starts here: 

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

and ends here: 

<?php endwhile; else: ?> 
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 

This is using PHP's alternative syntax for control structures, and could also be expressed as: 

<?php 
    if (have_posts()) { 
     while (have_posts()) { 
      the_post(); 
      // 
      // Post Content here 
      // 
     } // end while 
    } // end if 
?> 
+0

好的,這解釋了這是如何寫在「正常」的PHP。但是還有2個問題,如果你知道它們,你是否也可以回答這些問題? –

3

後()

此函數不接受任何參數。

返回值 該函數不返回任何值。

<?php 
while (have_posts()) : the_post(); 
    echo '<h2>'; 
    the_title(); 
    echo '</h2>'; 
    the_content(); 
endwhile; 
?> 

have_posts() 參數 此函數不接受任何參數。

返回值 (布爾值) 成功時爲true,失敗時爲false。 示例 以下示例可用於確定是否存在任何帖子,以及是否存在,通過它們進行循環。

<?php 
if (have_posts()) : 
    while (have_posts()) : the_post(); 
     // Your loop code 
    endwhile; 
else : 
    echo wpautop('Sorry, no posts were found'); 
endif; 
?> 

注 調用循環中此功能會導致一個無限循環。例如,請看下面的代碼:

<?php 
while (have_posts()): the_post(); 
     // Display post 
     if (have_posts()): // If this is the last post, the loop will start over 
       // Do something if this isn't the last post 
     endif; 
endwhile; 
?> 

如果你想檢查是否有電流回路更多帖子沒有這個不幸的副作用,您可以使用此功能。

function more_posts() { 
    global $wp_query; 
    return $wp_query->current_post + 1 < $wp_query->post_count; 
} 
1

這只是一個替代語法:

if (have_posts()) {   //open if 
    while (have_posts()) {  //start while loop 
    the_post();    //call a function 

http://php.net/manual/en/control-structures.alternative-syntax.php

這不是WordPress的具體,可以在任何PHP代碼中使用。

+0

因此,如果您將整個代碼寫入''標籤中,冒號是大括號的某種「替換」? –

+0

這只是一種替代語法。你也可以關閉兩個變種之間的php標籤。但我也喜歡用花括號結束標籤:) –

2

1.什麼是LOOP

環路使用由WordPress顯示帖子的PHP代碼。使用The Loop,WordPress會處理每個帖子以顯示在當前頁面上,並根據它與The Loop標籤中指定條件的匹配格式。

將取回相關特定頁面

數據:(冒號)是用來告訴條件/循環就從這裏開始。你可以用{}(支架引號)

<?php 
    if (have_posts()) { 
     while (have_posts()) { 
      the_post(); 
      // 
      // Post Content here 
      // 
     } // end while 
    } // end if 
?> 

2.更換這是WordPress的,只有或可能是其他somwhere使用嗎?

是的,當然你可以使用它。 您可以通過包含一個名爲「wp-blog-header.php」的核心文件來訪問完整的WordPress功能,該文件位於wordpress目錄的根目錄。

<?php 
/* Short and sweet */ 
define('WP_USE_THEMES', false); 
require('./wp-blog-header.php'); 
?> 

包含該文件在外部文件的頂部,您可以訪問WordPress數據庫,WordPress的功能,WordPress的鉤子了。

3.當前帖子存儲在哪裏?

在wordpress數據庫中存在11個默認表。您可以在數據庫中看到wp_posts表。所有帖子都存儲在這張表中。

假設,如果你在你的帖子創建meta標籤,它會在wp_postmeta