Advertisement
Guest User

latest_stickies

a guest
Jul 13th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. //последние закрепленные посты
  2. function wpb_latest_sticky() {
  3. //https://www.wpbeginner.com/wp-tutorials/how-to-display-the-latest-sticky-posts-in-wordpress/
  4.     /* Get all sticky posts */
  5.     $sticky = get_option( 'sticky_posts' );
  6.  
  7.     /* Sort the stickies with the newest ones at the top */
  8.     rsort( $sticky );
  9.  
  10.     /* Get the 5 newest stickies (change 5 for a different number) */
  11.     $sticky = array_slice( $sticky, 0, 5 );
  12.  
  13.     /* Query sticky posts */
  14.     $the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
  15. // The Loop
  16.     $return = "";
  17.     if ( $the_query->have_posts() ) {
  18.         $return .= '<ul>';
  19.         while ( $the_query->have_posts() ) {
  20.             $the_query->the_post();
  21.             $return .= '<li><a href="' .get_permalink(). '" title="'  . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>';
  22.  
  23.         }
  24.         $return .= '</ul>';
  25.  
  26.     } else {
  27.         // no posts found
  28.     }
  29.     /* Restore original Post Data */
  30.     wp_reset_postdata();
  31.  
  32.     return $return;
  33.  
  34. }
  35. add_shortcode('latest_stickies', 'wpb_latest_sticky');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement