2

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?

2 Answers 2

2

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
    }
}
?>
1
  • 2
    Could you share the extract_from_string() function also?
    – Eh Jewel
    Mar 19, 2019 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;
        }
     }
?>
1
  • Thanks for your answer @MarketHubb, it made me think and I finally ended up with a solution that works :) Nov 7, 2015 at 14:51

Your Answer

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

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