Help me get the excerpt!
-
Hi there.. I try to modify a widget to show what i want… But i can’t get the excerpt to show! Can you help me with this ?
function widget( $args, $instance ) { extract( $args ); /* Our variables from the widget settings. */ $title = apply_filters('Recent Posts', $instance['title'] ); $post_cat = $instance['post_cat']; if($post_cat == "All"){ $post_cat = ''; } $show_num = $instance['show_num']; /* Before widget (defined by themes). */ echo $before_widget; if($title) echo $before_title . $title . $after_title; /* Display the widget title if one was input (before and after defined by themes). */ $custom_posts = get_posts('showposts='.$show_num.'&cat='.get_cat_ID($post_cat)); if( !empty($custom_posts) ){ echo "<div class='gdl-recent-conference-widget'>"; foreach($custom_posts as $custom_post) { ?> <div class="recent-conference-widget"> <div class="recent-conference-widget-thumbnail"> <a href="<?php echo get_permalink( $custom_post->ID ); ?>"> <?php $thumbnail_id = get_post_thumbnail_id( $custom_post->ID ); $thumbnail = wp_get_attachment_image_src( $thumbnail_id , '280x111' ); $alt_text = get_post_meta($thumbnail_id , '_wp_attachment_image_alt', true); if( !empty($thumbnail) ){ echo '<img src="' . $thumbnail[0] . '" alt="'. $alt_text .'"/>'; } ?> </a> </div> <div class="recent-conference-widget-context"> <p> <?php echo get_the_excerpt( $custom_post->ID ); ?> </p> <a href="<?php echo get_permalink( $custom_post->ID ); ?>"> Read More </a> </div> <div class="clear"></div> </div> <?php } echo "</div>"; } /* After widget (defined by themes). */ echo $after_widget; }
Thx in advance
-
Instead of
<?php echo get_the_excerpt( $custom_post->ID ); ?>
I would try
<?php echo $custom_post->post_excerpt; ?>
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Template_Tags/get_posts#Examples
http://codex.wordpress.org/Function_Reference/get_the_excerpt
the_excerpt()
does not take the post ID as parameter.be aware that
$post->post_excerpt
only contains the hand-written excerpt – not an automatically generated.in the ‘get_posts()’ foreach loop, don’t forget to use
setup_postdata($post);
–i.e. change here:
foreach($custom_posts as $custom_post) { ?>
to:
foreach($custom_posts as $post) { setup_postdata( $post ); ?>
and use
$post
instead of$custom_post
in the rest of the foreach loop code;
then after the end of the foreach loop, add:wp_reset_postdata();
- The topic ‘Help me get the excerpt!’ is closed to new replies.