从报价发布格式中筛选第一个

时间:2015-11-05 作者:Marga López

我想以Quote post格式从帖子中获取第一个blockquote(即使用户只写了一个或多个),以便在归档循环中显示它。例如:

$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)
    }
}
有这样的事吗?

2 个回复
最合适的回答,由SO网友:Marga López 整理而成

我终于找到了一个可行的解决方案。如果您需要,请在这里:

<?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
    }
}
?>

SO网友:MarketHubb

尝试以下操作:

<?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;
        }
     }
?>

相关推荐