2012-06-26 57 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() ;)