2013-03-20 61 views
-1

我是新來的PHP ...這是wordpress。這裏有什麼問題?這是每個迴路簡單...PHP語法問題

<?php 
    foreach($how_posts as $post) : setup_postdata($post); 
    echo '<li><a href="#section_' . get_post_meta($post->ID, 'sub_nav_name', true); . '">' . the_title(); . '</a></li>'; 
    $nav_items[] = html_entity_decode(get_the_title(), ENT_QUOTES, 'UTF-8'); 
    endforeach; 
?> 
+5

我不知道......問題是什麼?如果你告訴我們你遇到了什麼錯誤,這可能會有所幫助。 – 2013-03-20 20:54:46

+1

什麼是foreach(...):? – sircapsalot 2013-03-20 20:54:47

+1

@sircapsalot速記語法 – AlienWebguy 2013-03-20 20:55:12

回答

4

在功能

echo '<li><a href="#section_' . get_post_meta($post->ID, 'sub_nav_name', true) . '">' . the_title() . '</a></li>'; 
4

的末尾刪除;這裏有一對夫婦的事情,你應該注意的。

1)你在你的代碼中分號:

echo '<li><a href="#section_' . get_post_meta($post->ID, 'sub_nav_name', true); . '">' . the_title(); . '</a></li>'; 
// Should be: 
echo '<li><a href="#section_' . get_post_meta($post->ID, 'sub_nav_name', true) . '">' . the_title() . '</a></li>'; 

2)endforeach使用 「備用」 語法:

備用PHP語法應該儘可能避免,爲了標準,可讀性和防止下一個必須閱讀你的代碼的人狩獵你並用一把生鏽的刀刺你。

由於這些語法是「備用」,因此可以安全地假定這些不是推薦的執行操作的方法。雖然他們確實有用,並且目前還沒有任何計劃會反對這種功能,但我不會長期指望他們,因爲PHP開始捲入失控的方法論。

foreach($how_posts as $post) : 
// Should be: 
foreach($how_posts as $post) { 

and 

endforeach; 
// Should be: 
} 

例子:

<?php 
foreach($how_posts as $post) { 
    setup_postdata($post); 
    echo '<li><a href="#section_' . get_post_meta($post->ID, 'sub_nav_name', true) . '">' . the_title() . '</a></li>'; 
    $nav_items[] = html_entity_decode(get_the_title(), ENT_QUOTES, 'UTF-8'); 
} 
+1

不是我使用它,但爲什麼要避免替代語法? – 2013-03-20 21:03:30

+0

我也想知道。你能否提供一個關於替代語法的文檔的鏈接? – 11684 2013-03-20 21:04:39

+2

因爲下一個需要維護你的代碼的人不會恨你。另外請注意,下一個人可能就是你的未來。 – PeeHaa 2013-03-20 21:04:40