Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
There are no paragraphs in the loop, so use a shortcode. –  Wyck May 2 '12 at 1:52
    
Well the loop output contains paragraphs. I could have asked that question more clearly. –  jw60660 May 2 '12 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? –  m0r7if3r May 2 '12 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 '12 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 '12 at 2:12
show 1 more comment

1 Answer

up vote 0 down vote accepted

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>";
} ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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