将层次结构功能添加到自定义帖子类型会从管理员中删除缩略图

时间:2013-09-05 22:59:04

标签: wordpress wordpress-theming custom-post-type

我在我的functions.php中设置了一个自定义帖子类型的“投资组合”。我刚刚为它添加了层次结构功能,因此我可以分配子帖等。

这样可以正常工作,但我可以通过添加行...

来分配子/父

'hierarchical' => true,

...到...

$args

...导致帖子的概述列表(其中所有帖子都列在具有已定义的数据列的表中,如标题,标签,缩略图等)由post_title命令ASC并且缩略图消失。删除此行,或将其值设置为false,将所有内容恢复为我希望的方式,由publish_date排序并显示缩略图,但当然没有我需要的层次结构功能。

如何保持分层功能并将帖子列表中的列显示为我希望的?最新订购并显示缩略图?

这是我的代码......

/*
 * Custom Post Type - Portfolio
*/

if ( ! class_exists( 'Portfolio_Post_Type' ) ) :

class Portfolio_Post_Type {

var $version = 0.5;

function __construct() {

    register_activation_hook( __FILE__, array( &$this, 'plugin_activation' ) );

    load_plugin_textdomain( 'portfolioposttype', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

    add_action( 'init', array( &$this, 'portfolio_init' ) );

    add_theme_support( 'post-thumbnails', array( 'portfolio' ) );

    add_filter( 'manage_edit-portfolio_columns', array( &$this, 'add_thumbnail_column'), 10, 1 );
    add_action( 'manage_posts_custom_column', array( &$this, 'display_thumbnail' ), 10, 1 );

    add_action( 'restrict_manage_posts', array( &$this, 'add_taxonomy_filters' ) );

    add_action( 'right_now_content_table_end', array( &$this, 'add_portfolio_counts' ) );

    add_action( 'admin_head', array( &$this, 'portfolio_icon' ) );
}

function plugin_activation() {
    $this->portfolio_init();
    flush_rewrite_rules();
}

function portfolio_init() {

    $labels = array(
        'name' => __( 'Portfolio', 'portfolioposttype' ),
        'singular_name' => __( 'Portfolio Item', 'portfolioposttype' ),
        'add_new' => __( 'Add New Item', 'portfolioposttype' ),
        'add_new_item' => __( 'Add New Portfolio Item', 'portfolioposttype' ),
        'edit_item' => __( 'Edit Portfolio Item', 'portfolioposttype' ),
        'new_item' => __( 'Add New Portfolio Item', 'portfolioposttype' ),
        'view_item' => __( 'View Item', 'portfolioposttype' ),
        'search_items' => __( 'Search Portfolio', 'portfolioposttype' ),
        'not_found' => __( 'No portfolio items found', 'portfolioposttype' ),
        'not_found_in_trash' => __( 'No portfolio items found in trash', 'portfolioposttype' )
    );

    $args = array(
        'labels' => $labels,
        'parent_item_colon' => 'PP',
        'public' => true,
        'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'author', 'custom-fields', 'revisions', 'page-attributes' ),
        'capability_type' => 'page',
        'rewrite' => array("slug" => "portfolio"),
        'menu_position' => 5,
        'hierarchical' => true,
        'has_archive' => true
    );

    $args = apply_filters('portfolioposttype_args', $args);

    register_post_type( 'portfolio', $args );

    $taxonomy_portfolio_tag_labels = array(
        'name' => _x( 'Portfolio Tags', 'portfolioposttype' ),
        'singular_name' => _x( 'Portfolio Tag', 'portfolioposttype' ),
        'search_items' => _x( 'Search Portfolio Tags', 'portfolioposttype' ),
        'popular_items' => _x( 'Popular Portfolio Tags', 'portfolioposttype' ),
        'all_items' => _x( 'All Portfolio Tags', 'portfolioposttype' ),
        'parent_item' => _x( 'Parent Portfolio Tag', 'portfolioposttype' ),
        'parent_item_colon' => _x( 'Parent Portfolio Tag:', 'portfolioposttype' ),
        'edit_item' => _x( 'Edit Portfolio Tag', 'portfolioposttype' ),
        'update_item' => _x( 'Update Portfolio Tag', 'portfolioposttype' ),
        'add_new_item' => _x( 'Add New Portfolio Tag', 'portfolioposttype' ),
        'new_item_name' => _x( 'New Portfolio Tag Name', 'portfolioposttype' ),
        'separate_items_with_commas' => _x( 'Separate portfolio tags with commas', 'portfolioposttype' ),
        'add_or_remove_items' => _x( 'Add or remove portfolio tags', 'portfolioposttype' ),
        'choose_from_most_used' => _x( 'Choose from the most used portfolio tags', 'portfolioposttype' ),
        'menu_name' => _x( 'Portfolio Tags', 'portfolioposttype' )
    );

    $taxonomy_portfolio_tag_args = array(
        'labels' => $taxonomy_portfolio_tag_labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => 'portfolio_tag' ),
        'show_admin_column' => true,
        'query_var' => true
    );

    register_taxonomy( 'portfolio_tag', array( 'portfolio' ), $taxonomy_portfolio_tag_args );

    $taxonomy_portfolio_category_labels = array(
        'name' => _x( 'Portfolio Categories', 'portfolioposttype' ),
        'singular_name' => _x( 'Portfolio Category', 'portfolioposttype' ),
        'search_items' => _x( 'Search Portfolio Categories', 'portfolioposttype' ),
        'popular_items' => _x( 'Popular Portfolio Categories', 'portfolioposttype' ),
        'all_items' => _x( 'All Portfolio Categories', 'portfolioposttype' ),
        'parent_item' => _x( 'Parent Portfolio Category', 'portfolioposttype' ),
        'parent_item_colon' => _x( 'Parent Portfolio Category:', 'portfolioposttype' ),
        'edit_item' => _x( 'Edit Portfolio Category', 'portfolioposttype' ),
        'update_item' => _x( 'Update Portfolio Category', 'portfolioposttype' ),
        'add_new_item' => _x( 'Add New Portfolio Category', 'portfolioposttype' ),
        'new_item_name' => _x( 'New Portfolio Category Name', 'portfolioposttype' ),
        'separate_items_with_commas' => _x( 'Separate portfolio categories with commas', 'portfolioposttype' ),
        'add_or_remove_items' => _x( 'Add or remove portfolio categories', 'portfolioposttype' ),
        'choose_from_most_used' => _x( 'Choose from the most used portfolio categories', 'portfolioposttype' ),
        'menu_name' => _x( 'Portfolio Categories', 'portfolioposttype' ),
    );

    $taxonomy_portfolio_category_args = array(
        'labels' => $taxonomy_portfolio_category_labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'show_tagcloud' => true,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => 'portfolio_category' ),
        'query_var' => true
    );

    register_taxonomy( 'portfolio_category', array( 'portfolio' ), $taxonomy_portfolio_category_args );

}

function add_thumbnail_column( $columns ) {

    $column_thumbnail = array( 'thumbnail' => __('Thumbnail','portfolioposttype' ) );
    $columns = array_slice( $columns, 0, 2, true ) + $column_thumbnail + array_slice( $columns, 1, NULL, true );
    return $columns;
}

function display_thumbnail( $column ) {
    global $post;
    switch ( $column ) {
        case 'thumbnail':
            echo get_the_post_thumbnail( $post->ID, array(35, 35) );
            break;
    }
}

function add_taxonomy_filters() {
    global $typenow;

    $taxonomies = array( 'portfolio_category', 'portfolio_tag' );

    if ( $typenow == 'portfolio' ) {

        foreach ( $taxonomies as $tax_slug ) {
            $current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;
            $tax_obj = get_taxonomy( $tax_slug );
            $tax_name = $tax_obj->labels->name;
            $terms = get_terms($tax_slug);
            if ( count( $terms ) > 0) {
                echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
                echo "<option value=''>$tax_name</option>";
                foreach ( $terms as $term ) {
                    echo '<option value=' . $term->slug, $current_tax_slug == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
                }
                echo "</select>";
            }
        }
    }
}

function add_portfolio_counts() {
        if ( ! post_type_exists( 'portfolio' ) ) {
             return;
        }

        $num_posts = wp_count_posts( 'portfolio' );
        $num = number_format_i18n( $num_posts->publish );
        $text = _n( 'Portfolio Item', 'Portfolio Items', intval($num_posts->publish) );
        if ( current_user_can( 'edit_posts' ) ) {
            $num = "<a href='edit.php?post_type=portfolio'>$num</a>";
            $text = "<a href='edit.php?post_type=portfolio'>$text</a>";
        }
        echo '<td class="first b b-portfolio">' . $num . '</td>';
        echo '<td class="t portfolio">' . $text . '</td>';
        echo '</tr>';

        if ($num_posts->pending > 0) {
            $num = number_format_i18n( $num_posts->pending );
            $text = _n( 'Portfolio Item Pending', 'Portfolio Items Pending', intval($num_posts->pending) );
            if ( current_user_can( 'edit_posts' ) ) {
                $num = "<a href='edit.php?post_status=pending&post_type=portfolio'>$num</a>";
                $text = "<a href='edit.php?post_status=pending&post_type=portfolio'>$text</a>";
            }
            echo '<td class="first b b-portfolio">' . $num . '</td>';
            echo '<td class="t portfolio">' . $text . '</td>';

            echo '</tr>';
        }
}

function portfolio_icon() { ?>
    <style type="text/css" media="screen">
        #menu-posts-portfolio .wp-menu-image {
            background: url(<?php echo bloginfo('stylesheet_directory'); ?>/assets/img/portfolio-icon.png) no-repeat 6px 6px !important;
        }
        #menu-posts-portfolio:hover .wp-menu-image, #menu-posts-portfolio.wp-has-current-submenu .wp-menu-image {
            background-position:6px -16px !important;
        }
        #icon-edit.icon32-posts-portfolio {background: url(<?php echo bloginfo('stylesheet_directory'); ?>/assets/img/portfolio-32x32.png) no-repeat;}
    </style>
<?php }

}

new Portfolio_Post_Type;

endif;

1 个答案:

答案 0 :(得分:0)

这是两个不同的问题:

缩略图无法显示
three types个钩子可显示自定义列。您应该使用 manage_pages_custom_column (分层帖子类型)或 manage_portfolio_posts_custom_column (专门针对您的CPT)。

wp-admin/edit.php 中的默认排序 对于分层帖子类型,它按标题,按日期表示其他类型。您可以使用pre_get_posts挂钩操纵该视图。