I am trying to display recent post in static page home-content.php. I’ve added this code in function.php

 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;

    wp_reset_query();

    return $list ;

    }

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

[recent-posts] this is a shortcode for display recent post

and home-content.php for showing post

<?php 
$post_id = 7;
$queried_post = get_post($post_id);
?>
<p><?php  $check=$queried_post->post_content; ?></p>
<?php  echo do_shortcode('["'.$check.'"]');?>

All the recent post display on home page of my custom theme http://templategraphy.com/wp-demo/businessguru/

but the problem theme structure is not properly shown. I want this type of structure http://templategraphy.com/demo/businessguru/

Suggest some solution to where I am doing wrong.

1 Answer
1

You have missed the quotes in the_permalink().
Use following 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() ) {
 $list .= '<a class="single-image link-icon" href="'. get_permalink().'">'.the_post_thumbnail(array(300,200),array('alt' =>$title)).'</a>';
}
$list .= '<h6 class="title"><a href=".the_permalink()."><span>"'.the_title().'"</span></a></h6>';
echo '<div class="entry-body">';
$list .= wpe_excerpt('wpe_excerptlength_index', '').'<a class="button default color" href="'.the_permalink().'">Read More</a>';
echo '</div>';

 echo '</div>';
endwhile;

wp_reset_postdata();

return $list ;

}

Leave a Reply

Your email address will not be published. Required fields are marked *