WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] split the content of the_content(); (10 posts)

  1. mscgl
    Member
    Posted 1 year ago #

    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 :)

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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>
  3. mscgl
    Member
    Posted 1 year ago #

    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....

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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)

  5. mscgl
    Member
    Posted 1 year ago #

    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 :)

  6. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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.

  7. mscgl
    Member
    Posted 1 year ago #

    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 :)

  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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.

  9. mscgl
    Member
    Posted 1 year ago #

    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 :)

  10. mscgl
    Member
    Posted 1 year ago #

    Thanks :)

Topic Closed

This topic has been closed to new replies.

About this Topic