2

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

0

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 ;

}
4
  • Don't use wp_reset_query, this is ment to be used for query_posts. You should be using wp_reset_postdata Commented Jun 30, 2014 at 8:25
  • Thanks @ jansha please see my code again something i am updated now links are working properly but now the problem is div are not closed properly, item class div closed before <h6> i dont where i am wrong please suggest me...
    – Payal
    Commented Jun 30, 2014 at 8:35
  • thanks @Pieter for editing my code and suggest me a solutions but my problem remain the same...
    – Payal
    Commented Jun 30, 2014 at 8:38
  • my issue is resolved and i am updated my code too... thanks everyone without your suggestion it can never be possible.
    – Payal
    Commented Jun 30, 2014 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.