Classipress make sticky ads on the top for each category

Posted: December 11, 2014 in web

on functions.php

<?php

...
function wpb_cpt_sticky_at_top($posts) {

// apply it on the archives only
    
    if (is_main_query() && !is_single() && !is_admin() && !is_search()) {
        global $wp_query;

        $sticky_posts = get_option('sticky_posts');
        $num_posts = count($posts);
        $sticky_offset = 0;

        // Find the sticky posts
        for ($i = 0; $i < $num_posts; $i++) {

            // Put sticky posts at the top of the posts array
            if (in_array($posts[$i]->ID, $sticky_posts)) {
                $sticky_post = $posts[$i];

                // Remove sticky from current position
                array_splice($posts, $i, 1);

                // Move to front, after other stickies
                array_splice($posts, $sticky_offset, 0, array($sticky_post));
                $sticky_offset++;

                // Remove post from sticky posts array
                $offset = array_search($sticky_post->ID, $sticky_posts);
                unset($sticky_posts[$offset]);
            }
        }

        // Look for more sticky posts if needed
        if (!empty($sticky_posts)) {

            $stickies = get_posts(array(
                'post__in' => $sticky_posts,
                'post_type' => $wp_query->query_vars['post_type'],
                'post_status' => 'publish',
                'nopaging' => true
                    ));

            foreach ($stickies as $sticky_post) {
                array_splice($posts, $sticky_offset, 1, array($sticky_post));
                $sticky_offset++;
            }
        }
    }

    return $posts;
}

add_filter('the_posts', 'wpb_cpt_sticky_at_top');

Advertisements
Comments
  1. Brian says:

    Hi,

    Do you know how I can put the classipress ad categories widget into the header next to my logo?

    http://www.worldclassads.com/classifieds/

    I have followed tutorial after tutorial, still can’t get it

    Thank you
    Brian

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s