0

SCENARIO: I inherited a WordPress site that uses the Thesis theme. I have Content Blocks installed, which I use to populate the right sidebar. I have a function that randomly picks up three post excerpts in the Books category. The excerpts contain iframes to book listings from Amazon.com. View on http://thebakingwizard.com/

PROBLEM: The posts are picking up randomly, as desired, but the iframes are outside of the layer structure in the function.

WHAT I HAVE DONE SO FAR:

  1. I understand that iframes should not be inside paragraph tags, so I used remove_filter( 'the_excerpt', 'wpautop' ); to remove the paragraph tags that Content Blocks create automatically. This did not change the situation.
  2. I changed the unordered list HTML to divs in case they were causing the problem. This did not work either.

I need the iframes to be inside the divs so I can line them up in one row.

THE FUNCTION

function RandomBook() 
{ 
$args = array(
'post_type' => 'post',
'orderby'   => 'rand',
'posts_per_page' => 3,
'category_name' => 'Books',
);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {

$string .= '<div id="AmazonListings">';
while ( $the_query->have_posts() ) 
{
    $the_query->the_post();
    $BookExcerpt = the_excerpt();
    $string .= '<div class="BookExcerpt">'.$BookExcerpt.'</div>';
} // end while
$string .= '</div><!-- end Amazon book listings -->';
/* Restore original Post Data */
wp_reset_postdata();
} else {

$string .= 'no posts found';
} // 5/23/17 Nora added */

return $string; 
} /* End Random post code from http://www.wpbeginner.com/wp-tutorials/how-to-display-random-posts-in-wordpress/<br />
 5/11/17 Nora added  */

add_shortcode('RandomBook','RandomBook');
add_filter('widget_text', 'do_shortcode'); 
3

The problem is that you are using the_excerpt(). This function prints out the excerpt immediately, it doesn't return a string.

From the core:

/**
 * Display the post excerpt.
 *
 * @since 0.71
 */
function the_excerpt() {

    /**
     * Filters the displayed post excerpt.
     *
     * @since 0.71
     *
     * @see get_the_excerpt()
     *
     * @param string $post_excerpt The post excerpt.
     */
    echo apply_filters( 'the_excerpt', get_the_excerpt() );
}

So you need to circumvent the echo by using apply_filters( 'the_excerpt', get_the_excerpt() ) instead of the_excerpt(), then you get the string you need, and the iframes should be where they belong.

| improve this answer | |
  • In CS we learned that if you are going to call a function, you should be sure you know what that function does. This is a perfect example. This answer set me in the right direction with a minor change. I switched functions in one line of code. I switched $BookExcerpt = get_the_excerpt(); for $BookExcerpt = the_excerpt(); – Nora McDougall-Collins Jul 12 '17 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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