2012-06-26 106 views
1

我有一個名爲'list-two'的自定義元框(使用Advanced Custom Fields),我想在編輯窗格中使用'more'標籤將文本/內容分成兩部分列。這是到目前爲止我的代碼,但只做什麼,我需要它的一半做:將內容拆分爲兩列 - Wordpress

在我functions.php文件:

function split_morecontent() { 
global $morecontent; 
$morecontent = true; 
$copy = preg_split('/<span id="more-\d+"><\/span>/i', get_field('list_two')); 
for($c = 0, $csize = count($copy); $c < $csize; $c++) { 
    $copy[$c] = apply_filters('list_two', $copy[$c]); 
} 
return $copy; 
} 

在我的帖子模板文件:

<?php 
// split text into array 
    $copy = split_morecontent(); 
// output first content section in column1 
echo '<section class="split">', array_shift($copy), '</section>'; 
// output remaining content sections in column2 
echo '<section class="split">', implode($copy), '</section>'; 
?> 

輸出運行時注意以下HTML(注意其實際上並未將內容拆分爲兩個標籤):

<section class="split"> 
    <ul class="footnote"> 
    <li>Christopher Gray</li> 
    <li>Jean Jullien<!--more--></li> 
    <li>Nous Vous</li> 
    <li>Liv Bargman</li> 
    <li>Luke Drozd</li> 
    </ul> 
</section> 

<section class="split"> 
    <!-- the last 3 <li> tags from the list above should be displayed within here.--> 
</section> 

回答

1

有一個ar在SitePoint上看起來好像可能會幫助你。 Check out How to Split WordPress Content Into Two or More Columns。基本上,它會告訴你要做到以下幾點:

添加到您的functions.php

// split content at the more tag and return an array 
function split_content() { 
    global $more; 
    $more = true; 
    $content = preg_split('/<span id="more-\d+"><\/span>/i', get_the_content('more')); 
    for($c = 0, $csize = count($content); $c < $csize; $c++) { 
     $content[$c] = apply_filters('the_content', $content[$c]); 
    } 
    return $content; 
} 

不管你想要發生(我默認情況下,大多數主題假設single.php),你需要更換這個師the_content()呼叫與新創建split_content(),就像這樣:

<?php 
    // original content display 
     // the_content(); 
    // split content into array 
     $content = split_content(); 
    // output first content section in column1 
     echo '<div id="column1">', array_shift($content), '</div>'; 
    // output remaining content sections in column2 
     echo '<div id="column2">', implode($content), '</div>'; 
?> 

您現在有內容拆分成兩個單獨的div。只要給它們各自的寬度並對它們應用一個浮點數,然後使用<!--more-->標籤將您的內容分成兩列。

+0

是的,這就是我試圖編輯的代碼。基本上來自Sitepoint的代碼只會分割the_content。我試圖調整它以拆分另一個所見即所得的內容框(我稱之爲list_two),但無法使其正常工作。所以我需要SitePoint的代碼在後期編輯頁面上使用額外的所見即所得編輯器。任何想法我可能會這樣做? – egr103

1

另一個簡單的方法是通過jQuery。這很簡單。 使用此:Columnizer jQuery Plugin

它的工作原理是這樣的:

$('.post-content').columnize({ columns: 2 }); 

而且在循環只需將DIV後內容的

<div class="post-content"> 
    <?php the_content() ?> 
</div> 

希望這有助於別人內the_content() ;)