0

I'd like to place the contents of a custom field or variable in between the second and third paragraphs in the loop on a page template.

Is there a way to do this?

So for example, on a client's website I would like them to use the post editor to write out their page content, and keep things simple for them by giving them the ability to write out a call to action in a custom field that diplays between two paragraphs of

the_content();

using either a plugin or a PHP snippet or filter in functions.php.

Thanks very much.

6
  • There are no paragraphs in the loop, so use a shortcode.
    – Wyck
    May 2, 2012 at 1:52
  • Well the loop output contains paragraphs. I could have asked that question more clearly.
    – jw60660
    May 2, 2012 at 1:54
  • post_content does not inherently contain paragraphs, it only contains paragraphs if wp_auto_p is enabled, and even then it is not a sure thing. Is there a reason that the element cannot be placed with CSS alone?
    – mor7ifer
    May 2, 2012 at 2:07
  • Thanks for the reply and the additional info. What I'm trying to do is simplify things for a client. I have a custom field that the client is using to create content for a blockquote. I would like to have the blockquote appear after the second paragraph and I didn't want to have to use two editors to create the separate paragraphs. So I'm looking to filter the the_content() output and place the contents (and otherwise styled div) of the custom field in between the second and third paragraphs.
    – jw60660
    May 2, 2012 at 2:10
  • I did find something, but I'm not sure where to place the code? Do I place it here, or do I answer my own question, or edit the original question.
    – jw60660
    May 2, 2012 at 2:12

1 Answer 1

0

After researching this, here is what works for me:

<?php
$paragraphAfter= 1; //show box after selected paragraph
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++) {
if ($i == $paragraphAfter) { ?>
<!-- START OF BLOCKQUOTE -->
stuff
<!-- END OF BLOCKQUOTE -->
<?php
}
echo $content[$i] . "</p>";
} ?>

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.