I want to make Shortcode where if client pass the name of tag(available or added in blog posts) as attribute then it will show 2 blog post from that mentioned tag.

as i cannot ask him to do all html static code and his placement of posting the blog in case study is not fixed so i am trying to make a shortcode.

tried many ways but can't figure out how to wrap the Tag + HTML Code & ShortCode in functions.php.

This is my shortcode code

// Custom ShortCode for Displaying Related Blog Posts 
add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
function rmcc_post_listing_parameters_shortcode( $atts ) {
    // define attributes and their defaults
    extract( shortcode_atts( array (
        'type' => 'post',
        'order' => 'date',
        'posts' => -1,
        'tag' => 'catname',
    ), $atts ) );   
    // define query parameters based on attributes
    $options = array(
        'post_type' => $type,
        'order' => $order,
        'posts_per_page' => $posts,
        'cat' => $catname,
    $query = new WP_Query( $options );
    // run the loop based on the query
    if ( $query->have_posts() ) { ?>
        <ul class="clothes-listing ">
            <ul class="clothes-listing ">
                <li id="post-<?php the_ID(); ?>"></li>
        $myvariable = ob_get_clean();
        return $myvariable;

// Add a shortcode

and this is my actual logic of printing the HTML data

<?php $blogposts = get_post_meta( get_the_ID(), 'blogpostscat', true ); ?>
        <?php $the_query = new WP_Query(array( 'post_type' => 'post', 'orderby' => 'rand', 'posts_per_page' => 2, 'tag' => $csdivision[0]->slug )); ?>
            // Loop through the posts
            while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
                <div class="one-half fl cs-blog-post">
                    <?php if (has_post_thumbnail( $post->ID ) ): ?>
                    <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'cspostimg' ); ?>
                        <a href="<?php the_permalink() ?>"><img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>"></a>
                    <?php endif; ?>
                    <div class="blog-post-content clearfix">                    
                        <a href="<?php the_permalink() ?>" class="cs-blog-post-head"><?php echo the_title(); ?></a>
                        <div class="position-relative fl"><a href="<?php the_permalink() ?>" class="cs-blog-readmore d-block">Read more &nbsp;&rarr;</a></div>
            <?php endwhile; ?>
            <?php wp_reset_postdata(); ?>        


and this would be specified in wordpress page

[list-posts catname='helloworld']

any help is most appriciated.

Thank you.

Your Answer

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

Browse other questions tagged or ask your own question.