Family Encyclopedia >> Electronics

How to Add Sticky Posts in WordPress Custom Post Type Files

Recently, one of our users asked us if it was possible to add sticky posts to custom post type files. By default, WordPress has sticky functionality available for posts, but not for other post types. In this article, we will show you how to add sticky posts in WordPress custom post type files. Before moving on, you'll probably want to learn how to create custom post types in WordPress.

Add sticky posts in custom post types

The first thing you need to do is install and activate the Sticky Custom Post Types plugin. After activating the plugin, go to Settings »Reading and scroll down to the Sticky Custom Post Types section. Next, you need to choose the custom post types you want this option to stick to.

How to Add Sticky Posts in WordPress Custom Post Type Files

Now what we've done here is we've added the sticky posts feature to our custom post types. Sticky posts in custom post types will display on the front page just like regular sticky posts.

The problem is that by default, WordPress only shows sticky posts on the home page. Does not show sticky messages on archive pages.

Displaying sticky posts in custom post type files

Let's say you have a custom post type for Movie Reviews with sticky posts enabled using the plugin we mentioned earlier. Now you want your sticky posts on movie review post types to display differently and on regular non-sticky movie reviews. I like this:

How to Add Sticky Posts in WordPress Custom Post Type Files

To achieve this goal, the first thing you need is a file template for your custom post type like this:archive-post-type.php . Learn how to create a custom post type archive page. For example, if you have a custom post type críticas de cine then your archive page template should be archivo-película-reviews.php . If you don't have a template, create one. Simply copy the contents of archive.php to your theme directory and paste them into a new archive-your-post-type.php file .

The next step is to add this code in your theme funciones.php file:

 función wpb_cpt_sticky_at_top ($ posts) // aplíquela en los archivos solo si (is_main_query () && is_post_type_archive ()) global $ wp_query; $ sticky_posts = get_option ('sticky_posts'); $ num_posts = count ($ posts); $ sticky_offset = 0; // Encuentra las publicaciones adhesivas para ($ i = 0; $ i ID, $ sticky_posts)) $ sticky_post = $ posts [$ i]; // Eliminar la posición actual array_splice ($ posts, $ i, 1); // Mover al frente, después de otros stickies array_splice ($ posts, $ sticky_offset, 0, array ($ sticky_post)); $ sticky_offset ++; // Eliminar la publicación de la matriz de publicaciones adhesivas $ offset = array_search ($ sticky_post-> ID, $ sticky_posts); unset ($ sticky_posts [$ offset]); // Busque más publicaciones adhesivas si es necesario si (!! ($ 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 como $ sticky_post) array_splice ($ posts, $ sticky_offset, 0, array ($ sticky_post)); $ sticky_offset ++; devolver $ posts; add_filter ('the_posts', 'wpb_cpt_sticky_at_top'); // Agregue una clase pegajosa en el título del artículo para dar estilo a las publicaciones pegajosas función diferente cpt_sticky_class ($ classes) if (is_sticky ()): $ classes [] = 'sticky'; devuelve $ clases; terminara si; devuelve $ clases; add_filter ('post_class', 'cpt_sticky_class'); 

The above code would move your sticky posts to the top, and if your theme is using post_class () function, then I would add sticky in the post class.

You can style your sticky posts using .pegajoso class in your style sheet. Example:

 .pegajoso color de fondo: # editado; imagen de fondo: url ('http://example.com/wp-content/uploads/featured.png'); repetición de fondo: no repetición; posición de fondo: arriba a la derecha; 

How to Add Sticky Posts in WordPress Custom Post Type Files

We hope this article helped you add sticky posts in custom post type files. For questions and comments please leave a comment below.

Source:Tareq Hasan