I have made a custom post type called news that has meta boxes. I would like to print the data from the metabox as a excerpt. How can I do that?

This is the code that I use that prints the data :

<?php $loop = new WP_Query( array( 'post_type' => 'news', 'posts_per_page' => 10, 'post_parent' => 0 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_excerpt(); ?> 
    <p><?php echo get_post_meta( $post->ID, 'twpb_news_textdate', true ); ?></p>
    <p><?php echo get_post_meta( $post->ID, 'twpb_news_rub', true ); ?></p>
    <p><?php echo get_post_meta( $post->ID, 'twpb_news_textnews', true ); ?></p>

<?php endwhile; ?>

1 Answer
1

You could make use of wp_trim_words:

<p><?php
    echo wp_trim_words(
        get_post_meta( $post->ID, 'twpb_news_textnews', true ),
        55,
        '[&hellip;]'
    );
?></p>

Or, if you want the filters applicable to the regular excerpts to be used as well, write your own wrapper for it:

function wpse115106_news_excerpt( $text="" ) {
    $excerpt_length = apply_filters( 'excerpt_length', 55 );
    $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );

    return wp_trim_words( $text, $excerpt_length, $excerpt_more );
}

and then use

<p><?php
    echo wpse115106_news_excerpt( get_post_meta( $post->ID, 'twpb_news_textnews', true ) );
?></p>

in your loop.

Leave a Reply

Your email address will not be published. Required fields are marked *