0

I have this code for showing recent posts on my home page custom theme and it works well.

functions.php code:

function my_recent_posts_shortcode($atts){
    $q = new WP_Query(
        array( 'orderby' => 'date','posts_per_page' => '4')
    );
    $list = "";

    while($q->have_posts()) : $q->the_post();    
        echo '<div class="item">';

        $title=get_the_title();

        if ( has_post_thumbnail() ) {    
            echo '<a class="single-image link-icon" href="'.get_permalink().'">';

            $list .=the_post_thumbnail(array(300,200),array('alt' =>$title));   

            echo '</a>';   
        }

        echo '<h6 class="title"><a href="'.get_permalink().'"><span>'.$title.'</span></a></h6>';  
        echo '<div class="entry-body">';

        $list .= wpe_excerpt('wpe_excerptlength_index', '');

        echo '<a class="button default color" href="'.get_permalink().'">Read More</a>';
        echo '</div>';
        echo '</div>';

        endwhile;

        return $list;
}

add_shortcode('recent-posts', 'my_recent_posts_shortcode');

This is the shortcode I use on my pages->Home page

[recent-posts]

For displaying the shortcode I have to use:

<?php
$post_id = 1746;
$queried_post = get_post($post_id);
$check=$queried_post->post_content;
echo do_shortcode($check);
?>

For displaying the shortcode, I used $post_id of my home page. I want to display the shortcode content without using post_id. When I use the shorcode on my homepage, it automatically calls that shorcode and displays the result. My theme URL is http://templategraphy.com/wp-demo/businessguru/

Please suggest some solutions.

0

You can use get_the_ID(); function in place of $post_id= 'your post id' so that you don't have to place the post id manually every time. The Function automatically gets the id of the post/page

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.