【问题标题】:Automatically split WordPress post content in to 3 columns?自动将 WordPress 帖子内容分成 3 列?
【发布时间】:2012-10-02 21:50:26
【问题描述】:

我目前正在使用来自this tutorial 的以下代码自动将 WordPress 帖子内容分成 2 列。但是,我将如何更改此代码以输出 3 列而不是仅 2 列?

functions.php 代码:

function content_split($text, $separator = '<hr/>', $start = false ) {

if ( $start === false) {
$start = strlen($text) / 2;
}

$lastSpace = false;
$split = substr($text, 0, $start - 1);

// if the text is split at a good breaking point already.
if (in_array(substr($text, $start - 1, 1), array(' ', '.', '!', '?'))) {

$split .= substr($text, $start, 1);
// Calculate when we should start the split
$trueStart = strlen($split);

// find a good point to break the text.
} else {

$split = substr($split, 0, $start - strlen($separator));
$lastSpace = strrpos($split, ' ');

if ($lastSpace !== false) {
$split = substr($split, 0, $lastSpace);
}
if (in_array(substr($split, -1, 1), array(','))) {
$split = substr($split, 0, -1);
}

// Calculate when we should start the split
$trueStart = strlen($split);
}
//now we know when to split the text
return substr_replace($text, $separator, $trueStart, 0);

}

index.php 代码:

<div class="first-column my-column">
<?php $text = get_the_content(); $separator = '</div><div class="second-column my-column">'; echo apply_filters('the_content', content_split($text,$separator)); ?>
</div>

【问题讨论】:

    标签: php wordpress function wordpress-theming multiple-columns


    【解决方案1】:
    function content_split($text, $separator = '<hr/>') {
    
        $string = '';
        $start = ceil(strlen($text) / 3);
    
        $string.= substr($text,0,$start);
        $string.= $separator;
        $string.= substr($text,$start,$start);
        $string.= $separator;
        $string.= substr($text,($start*2),$start);
    
        return $string;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多