wordpress更改帖子列表中的自定义帖子类型标题

时间:2014-10-30 05:46:02

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

我搜索了很多如何自定义自定义帖子列表, 我设法使用 manage_commercial_cpi_posts_columns 添加新列, 但是,我需要从列表中删除“标题”,仍然有编辑,删除选项。 我该怎么办?

1 个答案:

答案 0 :(得分:2)

您可以使用CSS或JS隐藏标题链接,但这是一种PHP方法。首先删除标题列,然后添加自定义列:

add_filter( 'manage_commercial_cpi_posts_columns', 'manage_commercial_cpi_posts_columns', 25, 1 );
function manage_commercial_cpi_posts_columns( $cols )
{
    // remove title column
    unset( $cols['title'] );

    // add custom column
    $cols['ccpi_links'] = __( 'Edit', 'textdomain' );

    // return columns
    return $cols;
}

然后,使用WP_List_Table类的row_actions方法生成链接。代码是从WP_Posts_List_Table类复制的。一个重要的细节是添加true作为row_actions方法的第二个参数,确保链接始终可见。

add_action( 'manage_commercial_cpi_posts_custom_column', 'manage_commercial_cpi_custom_column', 10, 2 );
function manage_commercial_cpi_custom_column( $col, $post_id )
{
    switch( $col ) :

    case 'ccpi_links' :

        // variables
        $post               = get_post( $post_id );
        $title              = _draft_or_post_title();
        $post_type_object   = get_post_type_object( $post->post_type );
        $can_edit_post      = current_user_can( 'edit_post', $post->ID );

        // set up row actions
        $actions = array();
        if ( $can_edit_post && 'trash' != $post->post_status ) {
            $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '" title="' . esc_attr( __( 'Edit this item' ) ) . '">' . __( 'Edit' ) . '</a>';
            $actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="' . esc_attr( __( 'Edit this item inline' ) ) . '">' . __( 'Quick&nbsp;Edit' ) . '</a>';
        }
        if ( current_user_can( 'delete_post', $post->ID ) ) {
            if ( 'trash' == $post->post_status )
                $actions['untrash'] = "<a title='" . esc_attr( __( 'Restore this item from the Trash' ) ) . "' href='" . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&amp;action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
            elseif ( EMPTY_TRASH_DAYS )
                $actions['trash'] = "<a class='submitdelete' title='" . esc_attr( __( 'Move this item to the Trash' ) ) . "' href='" . get_delete_post_link( $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
            if ( 'trash' == $post->post_status || !EMPTY_TRASH_DAYS )
                $actions['delete'] = "<a class='submitdelete' title='" . esc_attr( __( 'Delete this item permanently' ) ) . "' href='" . get_delete_post_link( $post->ID, '', true ) . "'>" . __( 'Delete Permanently' ) . "</a>";
        }
        if ( $post_type_object->public ) {
            if ( in_array( $post->post_status, array( 'pending', 'draft', 'future' ) ) ) {
                if ( $can_edit_post ) {
                    $preview_link = set_url_scheme( get_permalink( $post->ID ) );
                    /** This filter is documented in wp-admin/includes/meta-boxes.php */
                    $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
                    $actions['view'] = '<a href="' . esc_url( $preview_link ) . '" title="' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'Preview' ) . '</a>';
                }
            } elseif ( 'trash' != $post->post_status ) {
                $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
            }
        }

        // invoke row actions
        $table = new WP_Posts_List_Table;
        echo $table->row_actions( $actions, true );

        break;

    endswitch;
}

<强>结果

enter image description here

相关问题