Snippets

Willy Bahuaud Améliorer la fonctionnalité des « contenus à la une » de WordPress / Improve WordPress “Sticky Posts” feature

Updated by Willy Bahuaud

File sticky-cpt.php Modified

  • Ignore whitespace
  • Hide word diff
     $sticky_box = sprintf( '<span id="sticky-span"><input id="super-sticky" name="sticky" type="checkbox" value="sticky" %1$s />
     <label for="super-sticky" class="selectit">%2$s</label><br/></span>',
     checked( is_sticky( $post->ID ), true, false ),
-    __( 'Stick this to the front page' ) );
+    __( 'Stick this post to the front page' ) );
 
     $re = '/(<input.*id="visibility-radio-password")/U';
     $output = preg_replace( $re, "{$mb}$1", $temp );
Updated by Willy Bahuaud

File sticky-cpt.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+add_action( 'post_submitbox_minor_actions', 'w_post_submitbox_minor_actions' );
+function w_post_submitbox_minor_actions( $post ) {
+    ob_start();
+}
+
+add_action( 'post_submitbox_misc_actions', 'w_post_submitbox_misc_actions' );
+function w_post_submitbox_misc_actions( $post ) {
+    $temp = ob_get_clean();
+    
+    $hidden_sticky = '<input type="checkbox" style="display:none" name="hidden_post_sticky" id="hidden-post-sticky" value="sticky" '
+        . checked( is_sticky( $post->ID ), true, false ) . ' />';
+
+    $sticky_box = sprintf( '<span id="sticky-span"><input id="super-sticky" name="sticky" type="checkbox" value="sticky" %1$s />
+    <label for="super-sticky" class="selectit">%2$s</label><br/></span>',
+    checked( is_sticky( $post->ID ), true, false ),
+    __( 'Stick this to the front page' ) );
+
+    $re = '/(<input.*id="visibility-radio-password")/U';
+    $output = preg_replace( $re, "{$mb}$1", $temp );
+    echo $output;
+}
Updated by Willy Bahuaud

File sticky.php Modified

  • Ignore whitespace
  • Hide word diff
     return $q;
 }
 
-add_filter( 'found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
-function myprefix_adjust_offset_pagination( $found_posts, $q ) {
+add_filter( 'found_posts', 'sticky_adjust_offset_pagination', 1, 2 );
+function sticky_adjust_offset_pagination( $found_posts, $q ) {
     if ( is_paged_home_with_sticky( $q ) ) {
         $offset = count( get_option( 'sticky_posts', array() ) );
         return $found_posts + $offset;
Updated by Willy Bahuaud

File sticky.php Modified

  • Ignore whitespace
  • Hide word diff
     return $vars;
 }
 
+add_filter( 'pre_get_posts', 'w_sticky_posts' );
+function w_sticky_posts($q) {
+    if ( is_home() ) {
+        $q->set('ppp_include_sticky',true);
+    }
+    return $q;
+}
+
 add_filter( 'the_posts', 'sticky_count_offset', 10, 2 );
 function sticky_count_offset( $posts, $q ) {
     if ( $q->get( 'ppp_include_sticky' )
     return $posts;
 }
 
-add_filter( 'pre_get_posts', 'sticky_count_on_paged' );
-function sticky_count_on_paged( $q ) {
-    if ( is_home()
+function is_paged_home_with_sticky( $q ) {
+    return $q->is_home()
       && $q->get( 'ppp_include_sticky' )
       && $q->is_paged()
-      && false == $q->get( 'ignore_sticky_posts' ) ) {
+      && false == $q->get( 'ignore_sticky_posts' );
+}
+
+add_filter( 'pre_get_posts', 'sticky_count_on_paged', 20 );
+function sticky_count_on_paged( $q ) {
+    if ( is_paged_home_with_sticky( $q ) ) {
+        $q->set('post__not_in', get_option( 'sticky_posts', array() ) );
         $offset = count( get_option( 'sticky_posts', array() ) );
-        $page_offset = $offset + ( ( $q->get( 'paged' ) -1 ) * $q->get( 'posts_per_page' );
+        $page_offset = $q->get( 'posts_per_page', get_option( 'posts_per_page' ) ) * ( $q->get( 'paged' ) -1 ) - $offset;
         $q->set( 'offset', $page_offset );
     }
     return $q;
 }
+
+add_filter( 'found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
+function myprefix_adjust_offset_pagination( $found_posts, $q ) {
+    if ( is_paged_home_with_sticky( $q ) ) {
+        $offset = count( get_option( 'sticky_posts', array() ) );
+        return $found_posts + $offset;
+    }
+    return $found_posts;
+}
Updated by Willy Bahuaud

File sticky.php Modified

  • Ignore whitespace
  • Hide word diff
     }
     return $posts;
 }
+
+add_filter( 'pre_get_posts', 'sticky_count_on_paged' );
+function sticky_count_on_paged( $q ) {
+    if ( is_home()
+      && $q->get( 'ppp_include_sticky' )
+      && $q->is_paged()
+      && false == $q->get( 'ignore_sticky_posts' ) ) {
+        $offset = count( get_option( 'sticky_posts', array() ) );
+        $page_offset = $offset + ( ( $q->get( 'paged' ) -1 ) * $q->get( 'posts_per_page' );
+        $q->set( 'offset', $page_offset );
+    }
+    return $q;
+}
  1. 1
  2. 2
  3. 3
  4. 4
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.