0

I just transformed my query_posts function in a get_posts function.

Here is my old function:

<?php
// The Query
query_posts( 'cat=170&showposts=4' );
remove_filter('the_excerpt', 'wpautop');
// The Loop
while ( have_posts() ) : the_post();
    echo '<h3 style="font-size:14px"><a href="';the_permalink(); echo '" title="';the_title(); echo '"> '; the_title(); echo '</a></h3> <div class="ultimipostexcerpt">'; the_excerpt(); echo '</div>';
endwhile;
// Reset Query
wp_reset_query();
?>

Here's my new one

    <?php
$args = array( 'posts_per_page' => 4, 'category' => 170 );
$tag_posts = get_posts( $args );
    foreach ( $tag_posts as $post ) :
      setup_postdata( $post ); ?>
        <h3 style="font-size:14px"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><div class="ultimipostexcerpt"><?php the_excerpt(); ?></div>
    <?php endforeach;
    wp_reset_postdata(); ?>

The problem is that now the remove_filter('the_excerpt', 'wpautop'); directive doesn't work anymore.. I tried putting it before and after get_posts( $args );, doesn't affect the output.. :( how can I remove the filter for get_posts too?

1 Answer 1

0

You could make your own excerpt function:

    function wpse125086_custom_strip_tags_excerpt() {
        global $post;
        $p_obj = get_post($post->ID);
        $p_exp = $p_obj->post_excerpt;
        $p_exp = apply_filters('the_excerpt', $p_exp);
        $un_p = array("<p>", "</p>");
        $p_exp = str_replace($un_p, "", $p_exp);
        echo $pcont;
    }

But generally I'm thinking instead of transforming to get_posts() it might be better to go with WP_Query. In this case the standard filter you're using should work just fine.


Edit:

If it's about the opening/closing <p></p> tags, you can just go with get_the_excerpt(), because it doesn't add those. you have to echo it to print the excerpt out.

echo get_the_excerpt();

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.