Home > CMS Tutorial > WordPress > How to Split WordPress Content Into Two or More Columns

How to Split WordPress Content Into Two or More Columns

Joseph Gordon-Levitt
Release: 2025-02-21 11:07:13
Original
999 people have browsed it

This article demonstrates how to divide WordPress content into multiple sections using the built-in <more></more> tag, offering a user-friendly alternative to HTML or shortcodes. The method avoids requiring coding knowledge from content authors.

How to Split WordPress Content Into Two or More Columns

WordPress typically outputs page/post content using the_content(). However, dividing content into blocks (for multi-column layouts, for example) necessitates a different approach. While get_the_content() provides content as a PHP variable, identifying division points requires careful consideration. Common solutions, such as splitting at HTML tags (like <h2></h2> headings) or using shortcodes, present limitations: HTML tag methods require content author HTML knowledge and limit flexibility, while shortcodes burden the editor with remembering specific codes.

The <more></more> tag offers a superior solution. While often used for pagination, its advantages here include: a dedicated toolbar button in both visual and HTML editors, flexible placement within content, and ease of use for non-technical users.

How to Split WordPress Content Into Two or More Columns

To implement this, modify or create a functions.php file within your theme folder (wp-content/themes). Add the following PHP function:

// split content at the more tag and return an array
function split_content() {
    global $more;
    $more = true;
    $content = preg_split('//i', get_the_content('more'));
    for($c = 0, $csize = count($content); $c < $csize; $c++) {
        $content[$c] = apply_filters('the_content', $content[$c]);
    }
    return $content;
}
Copy after login

Next, locate the_content() calls within your theme's loop (likely in single.php, page.php, and potentially index.php, archive.php, or search.php). Comment out the_content() and replace it with a call to split_content(). This function returns an array where each element represents a content block separated by the <more> tag. You can then output this HTML as needed. For example:

<?php
//Example usage:  (Adapt to your theme's structure)
add_filter( 'the_content', 'my_custom_content' );
function my_custom_content( $content ) {
    return str_replace( 'Read the rest of this page »', '', $content );
}

// split content into array
$content = split_content();

// output first content section in column1
echo '<div>', array_shift($content), '</div>';

// output remaining content sections in column2
echo '<div>', implode($content), '</div>';
?>
Copy after login

Remember to adapt this to your theme's specific structure. This approach provides a clean, user-friendly method for dividing WordPress content without complex coding or plugin dependencies.

Frequently Asked Questions (FAQs) about Splitting WordPress Content into Multiple Sections

(The FAQs section remains unchanged as it offers alternative methods and addresses common concerns related to content splitting, which is valuable supplementary information.)

The above is the detailed content of How to Split WordPress Content Into Two or More Columns. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template