0

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

I don't want to use plug-ins where I would need to edit all the posts to get it as I want.

I've done the following and added it 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 the 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>';?>'

The problem is that first echo '<div id="column1">', array_shift($content), '</div>'; loads and then the bottom sidebar is loaded (Facebook comment links within).

Then echo '<div id="column2">', implode($content), '</div>'; is loaded and then the bottom sidebar is loaded again (Facebook comments link within).

Does anyone have any suggestions so that the sidebar (Facebook comment links within) is only loaded once (after column2)?

1 Answer 1

1

I am guessing, at least partially, but it sounds like the FaceBook content is loaded as a filter on the_content, which you run on both blocks of content.

The quick fix, keeping most of your code intact, would be to remove the FaceBook filter for the first array, then put it back for the second.

$csize = count($content);
remove_filter('the_content','fb-filter-name');
for($c = 0; $c < $csize; $c++) {
    // Note: this conditional may not be quite right
    // I'd have to test it to make sure it fires at the right time
    if ($csize === $c) add_filter('the_content','fb-filter-name');
    $content[$c] = apply_filters('the_content', $content[$c]);
}

However, you are going to have trouble with any filter that inserts content so that is by far not the best way to do this. You will end up having to remove and add back any filters that cause trouble. You'd be better to move most of your code into the function, create a string and run the the_content filter on the whole thing.

// 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">'. array_shift($content). '</div>';
    // remaining content sections in column2
    if (!empty($content)) $ret .= '<div id="column2">'. implode($content). '</div>';
    return apply_filters('the_content', $ret);
}

Completely untested and possibly buggy, but that is the idea.

This won't work well if it is intended to be portable-- that is, used by more than one theme-- since it requires a theme edit. So be aware. But if it is just for you it should be fine.

6
  • Hi, thanks. The first code I got error : Warning: Missing argument 2 for remove_filter(), called in /home/content/m/s/c/mscgluk/html/mscgl-UK/wp-content/themes/pioneer/single.php on line 33 and defined in /home/content/m/s/c/mscgluk/html/mscgl-UK/wp-includes/plugin.php on line 260. The second code no content at all (facebook comment not loaded at all).
    – mscgl
    Jan 6, 2013 at 18:37
  • Lol... of course it did. It should be remove_filter('the_content','fb-function-name'); I'll fix that in the answer. If you are choosing that route, it is high maintenance. Be warned.
    – s_ha_dum
    Jan 6, 2013 at 18:40
  • Hi thanks, Tried the new code. No content at all sadly.
    – mscgl
    Jan 6, 2013 at 18:46
  • I would rather use your second code, it seem as better option as you suggested. but the second code was no content. no error messages or facebook comments loaded. but rest of the page is ok.
    – mscgl
    Jan 6, 2013 at 18:47
  • My (second) function returns data so you will have to echo it. Did you do that?
    – s_ha_dum
    Jan 6, 2013 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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