如何在custon帖子列表中显示标题的自定义元字段值? [英] How to Display Custom meta field value insted of title on custon post list table?

查看:156
本文介绍了如何在custon帖子列表中显示标题的自定义元字段值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义帖子类型 cinfo,并从编辑页面中删除了标题和编辑器。借助此代码。还显示了一些与我的插件相关的自定义元字段。

I have created a custom post type "cinfo" and removed title and editor form the edit page. With the help of this code. Also displayed some custom meta fields which are relevant to my plugin.

function remove_box(){
    remove_post_type_support('cinfo', 'title');
    remove_post_type_support('cinfo', 'editor');
}
add_action("admin_init", "remove_box");

看起来像这样。

现在当我看到在列表页面上,我仍然看到标题下方带有编辑,查看和删除按钮的标题。这是我不想要的,因为标题字段在编辑页面中不存在,因此它在列表页面中显得无关紧要。取而代之的是,我尝试显示自定义元字段 email,但我只能更改列的标题。

Now when i see the list page I still see the title with edit, view and delete button beneath it. which I don't want because the title field doesn't exist in the edit page So it looks a bit irrelevant in the listing page. Instead of that I tried to display the custom meta field "email" but I was only able to change the heading of the column. which looks something like this.

我只是做了一些研究,发现了一个动作和过滤器,但它们似乎仍然对我没有多大帮助。仍然是为了更好地了解问题。我也尝试使用插件发布列表视图计数,但它也没有达不到我的目的希望您能理解我的基本想法。感谢您抽出宝贵时间阅读我的问题。

I just did some research and found one action and filter but they still didn't seems to be much of a help to me. Still for the better view of the problem. Also I tried to use a plugin Post List View Count but it also didn't accomplish my purpose. I hope You understand what I basically want to do. Thanks for your time to read my question.

add_filter('manage_cinfo_posts_columns', 'bs_cinfo_table_head');
function bs_cinfo_table_head( $defaults ) {
    $defaults['title']  = 'Email';
    return $defaults;
}


add_action( 'manage_cinfo_posts_custom_column', 'card_cinfo_table_content', 10, 2 );
function card_cinfo_table_content( $column_name, $post_id ) {
    if ($column_name == 'title') {
        echo "its working";
    }
}


推荐答案

操作 manage_cinfo_posts_custom_column 永远不会成立。最好删除 manage_cinfo_posts_columns 的默认值,并在其他过滤器中进行常规的自定义设置。

The action manage_cinfo_posts_custom_column will never be true. It's better to remove the defaults on manage_cinfo_posts_columns and do the regular custom stuff in the other filter.

一个快速的类来在我的设置中一起测试所有内容:

I tried this with a fast class to test all together inside my setup:

class SO23467344
{
    private $cpt = 'portfolio';
    private $custom_field = 'video';

    public function __construct()
    {
        add_filter( "manage_edit-{$this->cpt}_columns", array( $this, 'column_register' ), 20, 1 );
        add_action( "manage_{$this->cpt}_posts_custom_column", array( $this, 'column_display' ), 20, 2 );
        add_action( "admin_init", array( $this, "remove_box" ) );
    }
    function column_register( $columns ) 
    {
        $columns['my-col'] = 'My column';
        # Remove default columns
        unset( $columns['title'], $columns['categories'], $columns['comments'], $columns['date'] );         
        return $columns;
    }

    function column_display( $column_name, $post_id ) 
    {
        if ( 'my-col' != $column_name )
            return;

        if ( $field = get_post_meta( $post_id, $this->custom_field, true ) )
            echo '<br/><strong style="color:#0f0;font-size:4em"> • </strong>';
    }

    function remove_box(){
        remove_post_type_support( $this->cpt, 'title' );
        remove_post_type_support( $this->cpt, 'editor' );
    }
}
new SO23467344;

这篇关于如何在custon帖子列表中显示标题的自定义元字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆