【问题标题】:Display Custom Post Type Post Title in Column, by Referencing Post ID通过引用帖子 ID 在列中显示自定义帖子类型帖子标题
【发布时间】: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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-25
      • 2022-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 2016-12-29
      相关资源
      最近更新 更多