通过引用帖子ID在列中显示自定义帖子类型帖子标题

问题描述 投票:0回答:1

我有一个(可能)复杂的问题与WordPress自定义帖子类型有关,以及如何在网站的管理部分的列中显示这些类型的自定义元。我的网站上有两种自定义帖子类型:“联盟”和“团队”。每个团队都有一个自定义用户元,将其连接到一个联盟。该团队的元设置为“team_league”,其数字值等于相应联盟CPT的帖子ID。

我添加了一些代码,使“联盟”元显示在“团队”自定义帖子类型下的列中。但是,我想更进一步,因为它只显示相应联赛的帖子ID。是否可以在列中显示帖子标题,通过查找帖子ID和打印帖子标题来引用。

示例:帖子ID 98215 =帖子标题“春天 - 女子联盟 - Th”。

以下是我在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
1个回答
0
投票

好吧,在尝试了一些事情之后,我找到了解决自己问题的方法。我使用下面的代码代替上面发布的内容将列打印到列:

case 'team_league' :
        echo get_post(get_post_meta(get_the_ID(), 'team_league', true))->post_title; 
        break;

这产生了我正在寻找的结果,打印帖子标题而不是帖子ID。

© www.soinside.com 2019 - 2024. All rights reserved.