Wpautop在Get_the_Excerpt之后停止工作

时间:2013-03-24 作者:Tran Cuong

        function pre_process_shortcode($content){
            global $shortcode_tags, $shortcodes;

            // Backup current registered shortcodes and clear them all out
            $orig_shortcode_tags = $shortcode_tags;

        add_shortcode(\'bloglist\',\'bloglist_func\');

             // Do the shortcode (only the one above is registered)
            $content = do_shortcode($content);

            // Put the original shortcodes back
            $shortcode_tags = $orig_shortcode_tags;

            return $content;
        }
        add_filter(\'the_content\', \'pre_process_shortcode\', 7);

        // Allow Shortcodes in Widgets
        add_filter(\'widget_text\', \'pre_process_shortcode\', 7);

    function bloglist_func ($atts,$content=null){   
        extract(shortcode_atts(array(
            \'number\'        =>  2,
        ), $atts));
        ob_start();

        $args = array(
            \'post_type\'     =>  \'post\',
            \'showposts\'     =>  $number,
            \'status\'        =>  \'publish\',
            \'orderby\'       =>  \'date\',
            \'order\'         =>  \'DESC\',
        );

        $latest = new WP_Query($args);

        if ($latest->have_posts()):?>
        <div class="bloglist">\';
        <?php
            while($latest->have_posts()): $latest->the_post();?>

<div <?php post_class(\'post\');?> id="post-<?php the_ID();?>">
    <div class="date" title="<?php the_time(\'g:i a\'); ?>">              
        <span class="month"><?php the_time(\'F\'); ?></span>
        <span class="day"><?php the_time(\'d\'); ?></span>
        <span class="year"><?php the_time(\'Y\'); ?></span>
    </div>
    <div class="text">
        <h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4>
        <?php if($post_type==\'post\'):?>
        <div class="meta">
            <a href="<?php echo get_author_posts_url( get_the_author_meta( \'ID\' ) ); ?>" title="<?php printf(__(\'Posted by %s\',\'wi\') , get_the_author() ); ?>" rel="author" class="author"><?php the_author(); ?></a>
            <span class="slash">/</span>
            <?php comments_popup_link( __(\'Leave a comment\',\'wi\'), __(\'1 comment\',\'wi\'), __(\'% comment\',\'wi\'), \'comment-link\', __(\'Comments are closed\',\'wi\') ); ?>
        </div>
        <?php else: // post type == \'portfolio\'?>
        <div class="categories">
            <?php echo get_the_term_list( get_the_ID(), \'category\', \'\', \'<span class="slash">/</span>\', \'\' );?>
        </div>
        <?php endif; // endif post_type ?>
        <div class="excerpt">
            <p><?php echo substr(get_the_excerpt(),0,120);?> &hellip;</p>
            <div><a href="<?php the_permalink();?>" class="readmore"><?php _e(\'Read more &rarr;\',\'wi\');?></a></div>
        </div>
    </div>
    <div class="clearfix"></div>
</div>

        <?php
            endwhile;
        ?>
        </div>
        <?php
        endif;  // have posts
        wp_reset_query();
        $return = ob_get_clean();   
        return $return;
    }
以上是我的短代码。问题是:当我添加行时get_the_excerpt 在代码块内部,wpautop 之后停止工作。移除后get_the_excerpt, 一切都恢复正常了。

有人知道如何解决这个问题吗?

1 个回复
SO网友:Mike Ebert

wpautop肯定是我最不喜欢使用WordPress的部分。就在其他一切都在发挥作用的时候,它把手指伸进了一切。。。

正如您前面提到的,问题与过滤器优先级有关。对于摘录,我们对过滤器get\\u the\\u摘录感兴趣。我还没有弄清楚它通常有什么优先级,我也试着改变它的优先级。我成功了,但这并没有影响我的wpautop问题。

我没有干扰过滤器优先级,而是在我的短代码中复制了get\\u the\\u摘录的功能。如果我找到一个通用的解决方案会更好,但幸运的是,我不必经常在短代码中使用摘录。

以下是适用于我的代码:

while ($latest->have_posts()): $latest->the_post();
    //$excerpt = get_the_excerpt(); // This doesn\'t work

    // Let\'s do it like the get_the_excerpt filter function in the core does
    // Once we get the excerpt by alternate means, everything else works again
    // See get_the_excerpt in /wp-includes/post-template.php
    $post = get_post();
    $excerpt = $post->post_excerpt;

    // Choose what to do based on whether it\'s empty or not
    if ($excerpt != \'\') echo $excerpt;
    else echo \'some fallback\';
endwhile;

结束