gpt4 book ai didi

jquery - 为 CPT UI 添加 Sticky post 属性

转载 作者:行者123 更新时间:2023-11-28 17:35:53 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何将帖子定义为精选帖子或置顶帖子,以便在我的 WordPress 构建中以不同的方式显示。

CPT UI 给了我:

add_action('init', 'cptui_register_my_cpt_projects');
function cptui_register_my_cpt_projects() {
register_post_type('projects', array(
'label' => 'Projects',
'description' => 'Add individual projects',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'projects', 'with_front' => true),
'query_var' => true,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
'labels' => array (
'name' => 'Projects',
'singular_name' => 'Project',
'menu_name' => 'Projects',
'add_new' => 'Add Project',
'add_new_item' => 'Add New Project',
'edit' => 'Edit',
'edit_item' => 'Edit Project',
'new_item' => 'New Project',
'view' => 'View Project',
'view_item' => 'View Project',
'search_items' => 'Search Projects',
'not_found' => 'No Projects Found',
'not_found_in_trash' => 'No Projects Found in Trash',
'parent' => 'Parent Project',
)
) ); }

我尝试添加 'meta_key' => 'Sticky', & 'meta_key' => 'on', 但是都没有成功。

我要如何在每个帖子的前端添加一个选项以使其成为特色或粘性?

最佳答案

据我所知,置顶帖子不支持自定义帖子类型。但是我设法用这个函数绕过了它。

<?php 
function wpb_cpt_sticky_at_top( $posts ) {

// apply it on the archives only
if ( is_main_query() && is_post_type_archive() ) {
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, 0, array( $sticky_post ) );
$sticky_offset++;
}
}

}

return $posts;
}

add_filter( 'the_posts', 'wpb_cpt_sticky_at_top' );

// Add sticky class in article title to style sticky posts differently

function cpt_sticky_class($classes) {
if ( is_sticky() ) :
$classes[] = 'sticky';
return $classes;
endif;
return $classes;
}
add_filter('post_class', 'cpt_sticky_class');

这将在所有 CPT 帖子中添加一个元复选框,该复选框将显示“置顶”并 float 到该 CPT 存档的顶部

关于jquery - 为 CPT UI 添加 Sticky post 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com