2

我在我的functions.php 中设置了一个自定义帖子类型'Portfolio'。我刚刚为其添加了层次结构功能,因此我可以分配子帖子等。

这很好用,我可以在其中分配孩子/父母,但是,通过添加行...

'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;
4

1 回答 1

0

这是两个不同的问题:

未显示缩略图显示自定义列的挂钩
分为三种类型。您应该使用manage_pages_custom_column(分层帖子类型)或manage_portfolio_posts_custom_column(专门针对您的 CPT)。

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

于 2013-09-06T12:46:44.113 回答