WordPress adding content into different sections

admin2025-06-04  0

What I'm trying to figure out is how I can get the content of a page displayed different div's. That means if I add two paragraphs in the editor I would like to display the first paragraph in div section1 and the second in div section2.

page code:

<?php
if( have_posts()):
    while( have_posts()): the_post();?>
                    <?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>';
                ?>
<?php endwhile;
endif;
?>

and the functions.php:

    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;
}

What I'm trying to figure out is how I can get the content of a page displayed different div's. That means if I add two paragraphs in the editor I would like to display the first paragraph in div section1 and the second in div section2.

page code:

<?php
if( have_posts()):
    while( have_posts()): the_post();?>
                    <?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>';
                ?>
<?php endwhile;
endif;
?>

and the functions.php:

    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;
}
Share Improve this question edited Jan 3, 2019 at 20:20 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Jan 2, 2019 at 18:59 MisterLAMisterLA 257 bronze badges 10
  • please check url : sitepoint/split-wordpress-content-into-multiple-sections – vikrant zilpe Commented Jan 3, 2019 at 8:35
  • first of all thank you for the link. I arleady saw it. If follow the steps like there and try it then i'm getting these errors: Warning: preg_split(): Unknown modifier 'p' in /home/.sites/240/site1857187/web/wordpressversion/wp-content/themes/siberia/functions.php on line 17 Warning: count(): Parameter must be an array or an object that implements Countable in /home/.sites/240/site1857187/web/wordpressversion/wp-content/themes/siberia/functions.php on line 18 – MisterLA Commented Jan 3, 2019 at 12:52
  • please check url and fix php warring : stackoverflow/questions/5237583/… – vikrant zilpe Commented Jan 3, 2019 at 13:00
  • okay fixed. the problem is now the the second content is not showing in the new div. It is displayed in the same div as the first content. I will update the post so you can see what i did now. – MisterLA Commented Jan 3, 2019 at 13:20
  • I'd advice against running matches and splits on the content. Dive into theme development a bit more and learn how to make custom Blocks or use the native column blocks or something. THere are also a lot of themes/plugins available with blocks that will grant you all kinds of layout tooling. – Jebble Commented Jan 3, 2019 at 13:29
 |  Show 5 more comments

1 Answer 1

Reset to default 2

Don't do that! There are many other options to achieve this that are not hacky and messy. Here is what I would suggest.


Overview

First, create a child theme. This will allow you to make edits to your theme without losing them during an update.

Once your child theme is setup, add a custom field to your admin pages. I don't use a ton of plugins but when it comes to adding custom fields I use ACF because its super easy and you can create professional admin layouts in minutes. If you want to add the custom field manually check this out.

Now that you have your custom field added, all that's left is to pull in the data from the custom field into your page template.


Basic Code Example

This will change a bit depending on your theme but you should get the idea. For this example I will use the Twenty Seventeen theme. In this theme, the content for Pages is coming from /template-parts/page/content-page.php, so I duplicate that in my child theme.

With ACF Plugin (recommended)

Lets say I used ACF to create a new editor section called "more_content". I would just place the following code in my child theme's content-page.php and your done.

<div id="more-content">
    <?php
        $value = get_field("more_content");

        if($value) {
            echo $value;
        }
    ?>
</div>

Without ACF Plugin

If you didn't want to use ACF to create the custom field you would need to create the field using code like this...

add_action('add_meta_boxes',  function() { 
    add_meta_box('my_meta_box', 'Meta Box Title', 'my_metabox_function');
});

function my_metabox_function($post) {
    $text= get_post_meta($post->ID, 'my_metabox_name' , true );
    wp_editor(htmlspecialchars_decode($text), 'meta_box_id', $settings = array('textarea_name'=>'my_input_name') );
}

add_action( 'save_post', function($post_id) {
    if (!empty($_POST['my_input_name'])) {
        $data=htmlspecialchars($_POST['my_input_name']); //make sanitization more strict !!
        update_post_meta($post_id, 'my_metabox_name', $data );
    }
}); 

Then display the data in your page template like...

<?php
    $more_content = get_post_meta($post->ID, 'my_metabox_name', true);
    echo $more_content;
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749047739a315908.html

最新回复(0)