Snippets

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

Updated by Willy Bahuaud

File sticky.php Modified

  • Ignore whitespace
  • Hide word diff
 
 add_filter( 'the_posts', 'sticky_count_offset', 10, 2 );
 function sticky_count_offset( $posts, $q ) {
-    if ( $q->get( 'ppp_include_sticky' ) && 0 < $q->get( 'posts_per_page' ) ) {
+    if ( $q->get( 'ppp_include_sticky' )
+      && 0 < $q->get( 'posts_per_page' )
+      && false == $q->get( 'ignore_sticky_posts' ) ) {
         $posts = array_slice( $posts, 0, $q->get( 'posts_per_page' ) );
     }
     return $posts;
Created by Willy Bahuaud

File sticky.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+add_filter( 'query_vars', 'sticky_offset_query_var' );
+function sticky_offset_query_var( $vars ) {
+    $vars[] = 'ppp_include_sticky';
+    return $vars;
+}
+
+add_filter( 'the_posts', 'sticky_count_offset', 10, 2 );
+function sticky_count_offset( $posts, $q ) {
+    if ( $q->get( 'ppp_include_sticky' ) && 0 < $q->get( 'posts_per_page' ) ) {
+        $posts = array_slice( $posts, 0, $q->get( 'posts_per_page' ) );
+    }
+    return $posts;
+}
  1. 1
  2. 2
  3. 3
  4. 4
HTTPS SSH

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