0

I'm sure I'm doing something basic wrong here. Trying to insert a quote block after the second paragraph of a WordPress post if there is content in the field. If not, I want to add the content without the block. The code works outside of the if else statement but not when within it. I've used the Advanced Custom Fields plugin to create the field. Can anyone help? Here's my code:

<?php
$show_after_p = 2;
$content = apply_filters('the_content', $post->post_content);
$contentblock = the_field(post_quote_block);
if (!empty($contentblock)) {
if(substr_count($content, '<p>') > $show_after_p)
{
    $contents = explode("</p>", $content);
    $p_count = 1;
    foreach($contents as $content)
    {
        echo $content;
        if($p_count == $show_after_p)
        {
        ?>
                <div class="blogQuoteBlock"><div class="blogQuoteBlockText"><?php the_field(post_quote_block) ?></div></div>
        <?php
        }
        echo "</p>";
        $p_count++;
    }
}
}
else {
    <?php the_content(); ?>
}
?>

2 Answers 2

1

This is probably your problem:

$contentblock = the_field(post_quote_block);

Unless you have defined a constant post_quote_block you probably want:

$contentblock = the_field('post_quote_block');

Note that you have the same problem later on where you output the html.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Jeroen. Thanks for your response. Unfortunately that's not working (although it's entirely possible I'm missing something else here). When I add quote marks to each 'post_quote_block' I just get a blank page when I visit one of my posts.
0

Replace your 2 line of code by following code, to check whether value is set or not.

$contentblock = the_field('post_quote_block');
if (isset($contentblock) && !empty($contentblock)) {

Comments

Your Answer

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