【发布时间】:2020-06-02 22:20:36
【问题描述】:
我正在尝试使用自定义字段 (ACF) 的内容向我的自定义帖子类型添加一个新的管理列。我要添加的字段是“帖子对象”字段,但它只显示帖子标题而不是来自 ACF 的链接帖子。我添加了一个屏幕截图。
这是我目前所拥有的:
function add_new_realisaties_column($columns) {
$columns['realisatie_line'] = 'Line';
return $columns;
}
add_filter('manage_realisaties_posts_columns', 'add_new_realisaties_column');
function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
if ($column == 'realisatie_line') {
$post_object = get_field('realisatie_line');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
$evdate = the_title();
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif;
echo $evdate;
}
}
add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
/* Make the column sortable */
function set_custom_realisaties_sortable_columns( $columns ) {
$columns['realisatie_line'] = 'realisatie_line';
return $columns;
}
add_filter( 'manage_edit-realisaties_sortable_columns', 'set_custom_realisaties_sortable_columns' );
function realisaties_custom_orderby( $query ) {
if ( ! is_admin() )
return;
$orderby = $query->get('orderby');
if ( 'realisatie_line' == $orderby ) {
$query->set( 'meta_key', 'realisatie_line' );
$query->set( 'orderby', 'meta_value' );
}
}
add_action( 'pre_get_posts', 'realisaties_custom_orderby' );
【问题讨论】:
-
您不能回显 WP_Post 对象(或任何其他 PHP 对象) - 您希望链接帖子中的哪些字段/字段/标记?
<a href="link-to-acfpost">title of linked post</a>之类的东西? -
我只需要链接帖子的标题
标签: php wordpress advanced-custom-fields