Support » Plugins » Hacks » split the content of the_content();

  • Resolved mscgl

    (@mscgl)


    Hi,

    I want the content before <–more–> tag in one column and the rest in another column (single.php) only.

    I don’t want to use plug-ins that you need to edit all the post to get it as I want.

    I’ve done following

    Added following in functions.php

    // split content at the more tag and return an array
    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;
    }

    and added following to single.php

    <?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>';
    ?>

    when viewing a post, it just get the output of this :

    echo ‘<div id=”column1″>’, array_shift($content), ‘</div>’;

    No content from echo ‘<div id=”column2″>’, implode($content)

    I’ve tried plug-ins with similar features and these as the same output as I mention above. (get all the content before <–more–>, but not after the <–more–>).

    So I hope someone has a suggestion to solve my little dilemma or know about a plug-ins that do the same.

    Thanks in advance 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • should be doable just with using the information in
    http://codex.wordpress.org/Function_Reference/the_content
    and
    http://codex.wordpress.org/Customizing_the_Read_More

    use the global $more variable and the $stripteaser parameter of the_content();

    example:

    <div id="column1">
    <?php global $more; $more = 0;
    the_content('');
    $more = 1; ?>
    </div>
    <div id="column2">
    <?php the_content('',true); ?>
    </div>
    Thread Starter mscgl

    (@mscgl)

    Hi alchymyth,

    Thanks for your suggestion, tried it but, still the out put is everything before <–more–>. Nothing after the <–more–>. 🙁

    It seems

    <div id="column2">
    <?php the_content('',true); ?>
    </div>

    is empty some how….

    does the post split normally at the more tag position on an index page?

    what is the html output of the post content in the single post template?

    how are you entering the more tag into the post content?
    are you using the proper <!--more--> tag?

    (this is not quite clear as you are referring to <--more--> in your replies; and also the Codex is a bit inaccurate there)

    Thread Starter mscgl

    (@mscgl)

    Hi

    Sorry, it’s the : <!–more–>, yes it correct.

    I found something strange, after the first column the comment widget loads and then comes the column 2. and then loads the comment widget again.

    That why I didn’t see the column 2, I didn’t scroll below the comments…

    Do you have any idea ho to stop the loading the comments twice ?

    It should only be loaded after the column 2.

    thanks for your suggestions 🙂

    Do you have any idea ho to stop the loading the comments twice ?

    the comment loading might be done by a plugin -‘normally’ comments have nothing to do with the_content()

    temporarily deactivate all plugins to see if that helps; then reactivate one plugin at a time to find the one that is interfering.

    Thread Starter mscgl

    (@mscgl)

    Hi,

    Thanks, tried deactivate the plugins, and activated one and one. still the same.

    It seems that wp see the column 1 contents, is “all the content for the page” then start to load sidebar(), then sees the column 2 in the same wa and loads the facebook comment, linkwithin and shareaholic once again.

    <div id="column1">
    	<p>test content column 1</p>
    
    <div class="fb-social-plugin fb-comments" data-href="http://test.co.uk/testarticle/" data-width="620"></div>
    
    		</div>
    		<div id="column2">
    	<p><span id="more-275"></span>the content of column 2
    
    <div class="fb-social-plugin fb-comments" data-href="http://test.co.uk/testarticle/" data-width="620"></div>

    very strange.

    Thanks once again 🙂

    it looks like the ‘fb-social-plugin’ is hooking itself to the end of the content (even if it split into half);

    you possibly need to go back to using the unfiltered $post->post_content and split that at the <!--more--> location.

    the problem I can see is that when you need to use apply_filters('the_content', ... ) to get the formatting etc for your half contents, that the ‘fb-social-plugin’ will interfere again.

    Thread Starter mscgl

    (@mscgl)

    Hi

    Got help from stackexchange.com So I share the solution for others can use.

    put the code below in function.php

    // split content at the more tag and return an array
    function split_content() {
        global $more;
        $more = true;
        $content = preg_split('/<span id="more-\d+"><\/span>/i', get_the_content('more'));
        // first content section in column1
        $ret = '<div id="column1" class="column1">'. array_shift($content). '</div>';
        // remaining content sections in column2
        if (!empty($content)) $ret .= '<div id="column2" class="column2">'. implode($content). '</div>';
        return apply_filters('the_content', $ret);
    }

    and in single.php

    <? echo split_content(); ?>

    Thanks for your help and suggestions 🙂

    Thread Starter mscgl

    (@mscgl)

    Thanks 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘split the content of the_content();’ is closed to new replies.