2011-05-17 29 views
0

我在WordPress的WordPress的PHP函數

function includePosts ($content = '') { 
    preg_match_all('/(?<=\\[\\[)\\d+?(?=\\]\\])/', $content, $matches, PREG_PATTERN_ORDER); 
    $numMatches = count($matches[0]); 
    for ($i = 0; $i < $numMatches; $i++) { 
     $postId = $matches[0][$i]; 
     $post= get_post($postId); 
     $linkToPost = '<a href="'.get_permalink($postId).'">'; 
     $postTitle = $post->post_title; 
     $postTitleText = "<li> $linkToPost$postTitle</a></li>"; 
     $content = str_replace("[[$postId]]", $postTitleText, $content); 
    } 
    return $content; 
} 
// add_action('admin_menu', 'addAdminPage'); 
add_filter('the_content', 'includePosts', 1); 

這個PHP函數這樣做是什麼把從WordPress頁面短碼,至極將是帖子的ID,並顯示<li>..<li>一切好之間的文章的標題..不完全。 我希望所有的<li> ... </li>在一個<ul>...</ul>之內。這個功能有可能嗎?


jeremysawesome,有一件事我忘了提return $content回報不僅<li>又哪裏短碼的,但感謝你的時間在網頁的內容。

三星網頁設計,你的解決方案的行爲奇怪,我得到的是這樣的:

<ul> 

    <li>...</li> 

    <li>...</li> 

     <ul> 

     <li>...</li> 

     <li>...</li> 

    </ul> 
</ul> 

我真的不明白這一點

回答

1

只是在黑暗中拍攝。如果你用下面的語句替換你的return語句,它是否達到你想要的?

return "<ul>$content</ul>"; 
+0

這不起作用,這將包裹整個內容和列表中的元素在一個UL – 2011-05-17 21:15:44

0

像這樣的東西可能會奏效:

function includePosts ($content = '') { 
    preg_match_all('/(?<=\\[\\[)\\d+?(?=\\]\\])/', $content, $matches, PREG_PATTERN_ORDER); 
    $numMatches = count($matches[0]); 
    for ($i = 0; $i < $numMatches; $i++) { 
     $postId = $matches[0][$i]; 
     $post= get_post($postId); 
     $linkToPost = '<a href="'.get_permalink($postId).'">'; 
     $postTitle = $post->post_title; 
     $ul = ($i == 0) ? '<ul>' : ''; 
     $endul = ($i == $numMatches) ? '</ul>' : ''; 
     $postTitleText = "$ul<li> $linkToPost$postTitle</a></li>$endul"; 
     $content = str_replace("[[$postId]]", $postTitleText, $content); 
    } 
    return $content; 
} 
// add_action('admin_menu', 'addAdminPage'); 
add_filter('the_content', 'includePosts', 1); 

不是100%肯定這會工作,但不妨一試!