【问题标题】:Add ACF admin column to custom post type将 ACF 管理列添加到自定义帖子类型
【发布时间】: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 对象) - 您希望链接帖子中的哪些字段/字段/标记? &lt;a href="link-to-acfpost"&gt;title of linked post&lt;/a&gt; 之类的东西?
  • 我只需要链接帖子的标题

标签: php wordpress advanced-custom-fields


【解决方案1】:

我注意到了几件事。一是您实际上不需要使用setup_postdata() 函数,因为ACF Post Object field 使用了get_post(),它已经返回了一个完整的对象。仅当您打算覆盖全局 $post 对象时才这样做,例如在 single-{post_type}.php 模板上。

另一件事是,在帖子列中使用switch 语句而不是if/else 通常更为常见。有点迂腐,但有一点需要注意。

最后,the_title() 默认会回显标题,因此分配它,然后稍后回显它可能会导致问题(即在文档周围留下散落的变量)。如果您打算将其分配给变量,请考虑使用get_the_title()。另外,我不会详细介绍令人难以忍受的细节,但仅使用 setup_postdata 可能不足以让 post helper 函数从您想要的位置提取数据。

现在,说了这么多,您应该能够从get_field() 回显$post_objectpost_title 字段,因为它返回full formed WP_Post object。我把它放在我的一个测试站点上,它按预期工作:

add_filter('manage_realisaties_posts_custom_column', 'add_new_realisaties_admin_column_show_value', 10, 2);
function add_new_realisaties_admin_column_show_value( $column, $post_id ){
    switch( $column ){
        case 'realisatie_line':
            if( $post_object = get_field('realisatie_line') ){
                echo $post_object->post_title;
            }
            break;
    }
}

这是它在管理员中的样子,注意我刚刚为 Post Object 关系字段抓取了一个随机帖子:

【讨论】:

    【解决方案2】:

    尝试将您的 add_new_realisaties_admin_column_show_value 函数更改为以下代码。如果 ACF 字段名称为 realisatie_line,您还需要传递 $post_id 以获取每个特定帖子的特定元数据。

    function add_new_realisaties_admin_column_show_value( $column, $post_id ) {
    
        //Try using a switch/case for the column name
        switch ( $column ) {
           case 'realisatie_line': //Name of new column from add_new_realisaties_column function
              echo get_the_title( get_post_meta( $post_id, 'realisatie_line', true ) ); //Getting the ACF post meta using the $post_id, passing it through the get_the_title function to get title of attached post
              break;
    
           default:
              break;
    
        }
    }
    

    【讨论】:

    • 我不相信这会实现@Thessa Verbruggen 正在寻找的具体目标。此外,请考虑在基于代码的答案中添加一些文本以提供上下文。 什么你改变了,为什么你改变了它,新结果通常是很好的补充信息。
    • @Xhynk - 如果我理解原始问题,它应该可以工作。使用其他上下文更新了答案。
    • 这将返回行的 id 而不是标题。有没有办法显示链接帖子的标题?
    • @ThessaVerbruggen 您可以尝试将get_post_meta 包装在get_the_title 电话中吗?请参阅上面的更新代码。
    猜你喜欢
    • 2019-02-14
    • 2013-08-04
    • 1970-01-01
    • 2018-07-18
    • 2015-01-07
    • 1970-01-01
    • 2016-12-10
    • 2011-11-19
    • 1970-01-01
    相关资源
    最近更新 更多