2012-10-12 59 views
5

Tôi hiện đang sử dụng mã sau đây từ this tutorial để tự động chia nội dung bài đăng WordPress thành 2 cột. Tuy nhiên, làm cách nào để thay đổi mã này thành 3 cột thay vì chỉ 2? đangTự động chia nội dung bài đăng WordPress thành 3 cột?

functions.php: Mã

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> 

Trả lời

3
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; 
} 
+1

làm việc như một nét duyên dáng, cảm ơn! –

Các vấn đề liên quan