Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
        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;
    }

Above is my shortcode. The problem is: when i add the line get_the_excerpt inside the code block, wpautop stops working after that. After removing get_the_excerpt, everything is back to normal.

Anyone knows how to solve this problem?

share|improve this question
    
What do you mean it "stops working"? You haven't tried to use wpautop after that line. Please elaborate. –  s_ha_dum Mar 24 '13 at 13:43
    
As normal, WP automatically trigger function wpautop for post content. But if I use get_the_excerpt in the shortcode, all content after that shortcode is not effectless under wpautop. If i remove that shortcode, everything is back to normal. Note that, I triggered do_shortcode function in priority 7, before wpautop. –  Tran Cuong Mar 24 '13 at 14:24
    
Please post all of you code-- the complete shortcode callback and your Loop. –  s_ha_dum Mar 24 '13 at 14:31
    
Thanks for interesting. I've posted all my codes. Grateful if you help me to solve this. –  Tran Cuong Mar 25 '13 at 3:27
    
I am not sure why you think you are "clearing" all the shortcodes, or why you think you need to add the shortcode in that roundabout way... can you explain in human terms what result you are looking for? –  s_ha_dum Mar 25 '13 at 4:10

1 Answer 1

wpautop has got to be my least favorite part of working with WordPress. Just when everything else is working, it sticks its fingers into everything...

The problem has to do with filter priority, as you noted earlier. In the case of the excerpt, we're interested in the filter get_the_excerpt. I haven't been able to figure out what priority it has normally, and I also tried changing its priority. I succeeded, but it didn't affect my wpautop problem.

Instead of messing with filter priorities, I duplicated the functionality of get_the_excerpt in my shortcode. It would be better if I found a general solution, but luckily I don't have to use the excerpt in shortcodes very often.

Here is the code that worked for me:

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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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