1

I want to get the first blockquote from the post (even if the user writes only one, or more than one) in the Quote Post Format, in order to show it in the archive Loop. For example:

$quote = has_post_format( 'quote' );
if ( $quote ) {
    if ( *the post has no quotes* ){
        // Don't show anything
    } else {
        // Show the blockquote from the post (if there's only one),
        // or the first one (if there are more than 1)
    }
}

Is there something like this?

1

I finally found a solution that works. In case you need it, here it is:

<?php
if (has_post_format('quote', $post->ID)) {
    $content = trim(get_the_content());
    // Take the first quote from the content
    $quote_string = extract_from_string('<blockquote>', '</blockquote>', $content);
    // Make sure there's a quote on the content
    if (!$quote_string == "") {
         // Get the first quote and show it on the Loop
         echo $quote_string;
    } else {
         // If not, show nothing
    }
}
?>
| improve this answer | |
  • 2
    Could you share the extract_from_string() function also? – Eh Jewel Mar 19 '19 at 7:02
0

Try something like this:

<?php   
    // Check is post has quote format       
    if (has_post_format('quote', $post->ID)) {
       $content = trim(get_the_content());
       // Make sure content isn't empty
       if (!$content == "") {
           // Take each new line and put into an array (for multiple quotes)
           $quote_array = explode( "\n", $content);
           // Get the first quote and do something with it
           $first_quote = array_shift( $quote_array );
           echo $first_quote;
        }
     }
?>
| improve this answer | |
  • Thanks for your answer @MarketHubb, it made me think and I finally ended up with a solution that works :) – Marga López Nov 7 '15 at 14:51

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.