【发布时间】:2019-07-14 18:32:08
【问题描述】:
我有一个(可能)复杂的问题,与 WordPress 自定义帖子类型有关,以及如何在网站管理部分的列中显示这些类型的自定义元数据。我的网站上有两种自定义帖子类型:“联赛”和“团队”。每个团队都有一个自定义用户元数据,将其连接到联赛。该球队的元数据设置为“team_league”,并具有一个数值,该数值等于相应联赛 CPT 的帖子 ID。
我添加了一些代码以使“联赛”元数据出现在“团队”自定义帖子类型下的列中。但是,我想更进一步,因为它只显示相应联赛的 Post ID。是否可以在列中显示帖子标题,通过查找帖子 ID 和打印帖子标题来引用。
示例:帖子 ID 98215 = 帖子标题“春季 - 女子联赛 - 周四”。
这是我将自定义元添加到 CPT 列的代码:
// Add the custom columns to the Teams post type:
add_filter( 'manage_team_posts_columns', 'set_custom_edit_team_columns' );
function set_custom_edit_team_columns($columns) {
$columns['sport_name'] = __( 'Sport', 'your_text_domain' );
$columns['team_league'] = __( 'League', 'your_text_domain' );
$columns['current_paid_amount'] = __( 'Amount Paid', 'your_text_domain' );
return $columns;
}
// Add the data to the custom columns for the Teams post type:
add_action( 'manage_team_posts_custom_column' , 'custom_team_column', 10, 2 );
function custom_team_column( $column, $post_id ) {
switch ( $column ) {
case 'sport_name' :
echo get_post_meta( $post_id , 'sport_name' , true );
break;
case 'team_league' :
echo get_post_meta( $post_id , 'team_league' , true );
break;
case 'current_paid_amount' :
echo '$' . get_post_meta( $post_id , 'current_paid_amount' , true );
break;
}
}
【问题讨论】:
标签: php wordpress custom-post-type