jquery - 为 CPT UI 添加粘性帖子属性

标签 jquery html css wordpress

我正在尝试弄清楚如何将帖子定义为特色帖子或置顶帖子,以便在我的 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 添加粘性帖子属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25082474/

相关文章:

jquery - 如何获得元素的正确偏移量? -jQuery

jquery - 如何使用 jquery 为小型设备添加 css 属性

css - 带有左右元素的 Bootstrap 表单样式

javascript - 从选择中隐藏先前选择的选项

html - 修复页面顶部的菜单栏

linux - 使用 shell 脚本从出现在网页顶部的弹出窗口中提取 urls 链接?

jquery - 如何切换并记住 localStorage 中的 div

javascript - 当我们滚动到 div 来到窗口中间位置时为我的 div 制作动画

javascript - 页面重新加载后重置导航栏状态

php - ajax验证不起作用