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.

enter image description here

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;
}
  • 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 Jan 3 at 12:52
  • please check url and fix php warring : stackoverflow.com/questions/5237583/… – vikrant zilpe Jan 3 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 Jan 3 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. – Jeffrey von Grumbkow Jan 3 at 13:29
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;
?>
  • I was thinking about ACF but yeah tryed to display at first content without that. Good examples. Thank you. But if i work with ACF that means that i have to make at the beginning a koncept so that i chose where to use extra Fields for Content to make it easy for the blogger or writer to change the content. Anyway it was really helpfull thank you. – MisterLA Jan 3 at 20:25
  • Please accept the answer if it solved your issue to close it out – RiddleMeThis Jan 4 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.