I have made a custom archive template which I want to display all the latest posts. I have managed to get this to work when searching for categories using archive.php fine.

However now I have created a page template for displaying the archive with the same code, get_excerpt() Is not working. Here is the function for excerpt in my function.php file

function add_excerpt_class( $excerpt ) {

if ( is_front_page() ){
    $excerpt = str_replace( "<p", "<p class=\"book-description hidden-md-down brown\"", $excerpt );
    return $excerpt;
} elseif ( is_archive() || is_page( 'blog' ) ){
    $excerpt = str_replace( "<p", "<p class=blog-extract", $excerpt );
    return $excerpt;
} else {
    $excerpt = str_replace( "<p", "<p class=blog-extract", $excerpt );
    return $excerpt;


This is my query (only in page template)

<?php $args = array(
'posts_per_page'   => 4,
'orderby'          => 'post_date',
'order'            => 'DESC',
'post_type'        => 'post',
'post_status'      => 'publish',
'suppress_filters' => false );

$wp_query = new WP_Query( $args );

        <?php if ( have_posts() ) : ?>

            <?php while ( have_posts() ) : the_post(); ?>


                    include( locate_template( 'template-parts/content-archive.php' ) );


            <?php endwhile; ?>

            <script type="text/javascript">
            blogImages(<?php echo $post_number; ?>, <?php echo json_encode($archiveImgArray); ?>);

        <?php the_posts_navigation(); ?>

        <?php else : ?>

            <?php get_template_part( 'template-parts/content', 'none' ); ?>

        <?php endif; ?>

I've used include local template instead of get_template_part for a script and this works find in archive.php. In archive-content.php it's simply

<?php the_excerpt(); ?>

the title(); works fine so what has gone wrong here?


I've found a workaround.

$content = get_post_field( 'post_content', $latest_post["ID"] );
$content = strip_tags($content);
$content = wp_trim_words($content);

and then just echo $content instead of the excerpt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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