• Resolved yosmc

    (@yosmc)


    Hi all,

    I’m having some fairly basic formatting needs and I’m slightly frustrated that it seems so hard to get it done in WordPress. Maybe someone can point me in the right direction.

    Basically I have a lot of texts I need to transfer to WordPress. Each paragraph has the first line indented, and there’s a couple of extra pixels of space between each paragraph.

    Now when I copy and paste the whole thing, WordPress doesn’t recognize the paragraphs as paragraphs – it seems WordPress will only do that if you have two linebreaks (i.e. an empty line between two paragraphs).

    That puts me in a difficult position: I know there are various approaches I can take to make things look right in the end – e.g. I can add paragraph tags manually or I can use DIV-tags. But all of these approaches require a lot of work for each text I am transferring.

    I am looking for an easier approach that I can apply once, and then I don’t have to keep making manual corrections every time I add a new text. So this could be teaching WordPress to add the paragraph tags for single line breaks as well, or finding a way to turn single line breaks into the requested formatting.

    Any ideas for this? Possibly there’s a plugin I can use? Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • yosmc

    Try out this code and let me know if it works. you can put it in functions.php of the theme or some custom plugin you might be using on your site.

    add_filter('posts_results', 'the_para_fixer');
    function the_para_fixer($posts) {
        if (is_array($posts)) {
            foreach ($posts as $post) {
                if ($post->post_type == 'post') {
                    $post->post_content = str_replace("\n", "\n\n", $post->post_content);
                    $post->post_content = str_replace("\n\n\n", "\n\n", $post->post_content);
                }
            }
        }
        return $posts;
    }
    Thread Starter yosmc

    (@yosmc)

    Works great – thanks a ton!

    Tiny thing: Now, all paragraphs are the same. How can I preserve the original paragraphs that were created because of two line breaks? I.e. what string would I have to look for?

    yosmc

    Welcome ??

    Just remove the second string replace function, here’s the code

    add_filter('posts_results', 'the_para_fixer');
    function the_para_fixer($posts) {
        if (is_array($posts)) {
            foreach ($posts as $post) {
                if ($post->post_type == 'post') {
                    $post->post_content = str_replace("\n", "\n\n", $post->post_content);
                }
            }
        }
        return $posts;
    }

    Also, don’t forget to mark the ticket as resolved.

    Thread Starter yosmc

    (@yosmc)

    Actually I had tried that, but didn’t work this time. This here does the trick, though:

    add_filter('posts_results', 'the_para_fixer');
    function the_para_fixer($posts) {
        if (is_array($posts)) {
            foreach ($posts as $post) {
                if ($post->post_type == 'post') {
                    $post->post_content = str_replace("\n", "\n\n", $post->post_content);
                    $post->post_content = str_replace("\n\n\r", " ", $post->post_content);
                }
            }
        }
        return $posts;
    }

    yosmc

    Cool ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Paragraph styling question’ is closed to new replies.