2

I currently have a function pulled from WPBeginner which allows me to create a shortcode which pulls the stickied posts on the site wherever I want.

I am in need of this to also show the featured image but not sure how to get this to work currently. The current shortcode is below:

function wpb_latest_sticky() { /* Get all sticky posts */ $sticky = get_option( 'sticky_posts' ); /* Sort the stickies with the newest ones at the top */ rsort( $sticky ); /* Get the 5 newest stickies (change 5 for a different number) */ $sticky = array_slice( $sticky, 0, 1 ); /* Query sticky posts */ $the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) ); // The Loop if ( $the_query->have_posts() ) { $return .= '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); $return .= '<li><a href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>'; } $return .= '</ul>'; } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); return $return; } add_shortcode('latest_stickies', 'wpb_latest_sticky'); 

This is how I pull it on post pages:

 <?php if ( has_post_thumbnail() && !is_search() ) { ?> <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( esc_html__( 'Permalink to ', 'quark' ) . '%s', the_title_attribute( 'echo=0' ) ) ); ?>"> <?php the_post_thumbnail( 'post_feature_full_width' ); ?> </a> 

Any help would be greatly appreciated!

1
  • did you try get_the_post_thumbnail() Commented Feb 16, 2017 at 20:49

1 Answer 1

2
while ( $the_query->have_posts() ) { $the_query->the_post(); $return .= '<li><a href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt() . '<br/>' . get_the_post_thumbnail() . '</li>'; } 
Sign up to request clarification or add additional context in comments.

3 Comments

This worked great, Jim's answer gave me a 500 error but Andy seemed to have fixed it. Is there anyway to add classes to the title, excerpt and featured post for easy css editing? Thank you so much for your help both.
You could do this to add classes: <li><a class="yourTitleClass" href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br /><span="yourExcerptClass">' . get_the_excerpt() . '</span><br/>' . get_the_post_thumbnail('$post->ID', 'full', array( 'class' => 'yourImageClass') . '</li>
0 down vote like this: get_the_post_thumbnail( $_post->ID, 'thumbnail', array( 'class' => 'alignleft' ) );

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.