WordPress Tricks

Show Latest Posts at the end of blog post in Smart Theme v3 by Optimizepress

If you are using Smart Theme V3 by Optimizepress and want to show latest post at the end of each blog post like the screenshot below, then use the code i have shared below.

I have shared the code below. Please use Code Snippets plugins to use the code . You can put the code directly in function.php file ( if you are using child theme )

// Define a function to display latest articles
function display_latest_articles() {
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => 2,
        'orderby'        => 'date',
        'order'          => 'DESC',
    );

    $latest_posts = new WP_Query( $args );

    if ( $latest_posts->have_posts() ) :
        ?>
        <aside class="op-related-articles show">
            <hr>
            <h4 class="op-related-articles-titlea" style="text-align:center;">Latest Articles</h4>
            <div class="op-related-articles-grid-row op-related-articles-grid-row--2-col">
                <?php while ( $latest_posts->have_posts() ) : $latest_posts->the_post(); ?>
                    <div class="box-item">
                        <div class="post-box">
                            <a class="op-related-articles-img-container" href="<?php the_permalink(); ?>">
                                <span class="visuallyhidden"><?php the_title(); ?></span>
                                <?php the_post_thumbnail( 'related-article-size', array( 'class' => 'attachment-related-article-size size-related-article-size wp-post-image', 'loading' => 'lazy' ) ); ?>
                            </a>
                            <div class="post-box-description">
                                <h3 class="op-related-articles-headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>
        </aside>
        <?php
        wp_reset_postdata(); // Reset post data
    endif;
}

// Hook into op_single_after_content to display latest articles
add_action('op_single_after_content', 'display_latest_articles', 50);

Leave a comment