Monday, August 29, 2022
HomeWordPress Developmentshortcode - get_queried_object_id / quick code returning clean

shortcode – get_queried_object_id / quick code returning clean


From this reply, I am utilizing the next in a small customized plugin:

perform mm_related_posts() {

    if (is_singular()) {
        $tags = wp_get_post_terms( get_queried_object_id(), 'post_tag', ['fields' => 'ids'] );
        $args = [
            'post__not_in'        => array( get_queried_object_id() ),
            'posts_per_page'      => 5,
            'ignore_sticky_posts' => 1,
            'tax_query' => [
                [
                    'taxonomy' => 'post_tag',
                    'terms'    => $tags
                ]
            ]
        ];
        $my_query = new wp_query( $args );
        if( $my_query->have_posts() ) {
            echo '<div id="associated"><h4>Associated Posts</h4>';
                whereas( $my_query->have_posts() ) {
                    $my_query->the_post(); ?>
                    <div class="ncc">

                        <h5><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>" rel="nofollow"><?php the_title(); ?></a></h5>
                        <?php the_excerpt(); ?>

                    </div><!--ncc-->
                <?php }
                wp_reset_postdata();
            echo '</div><!--related-->';
        }
    }
}

// register shortcode
add_shortcode('mm_related_posts', 'mm_related_posts');

Nevertheless, the shortcode [mm_related_posts] is just not outputting any content material.

Are you able to see what’s flawed with this code, please?

Assist appreciated.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments