将用户元文本作为链接返回到用户列中的后期编辑

时间:2011-01-21 作者:Philip

我使用此函数向用户列获取一些信息,

function column_company( $defaults ) {
    $defaults[\'usercolumn-company\'] = __(\'Company\', \'user-column\');
    $defaults[\'usercolumn-myoffers\'] = __(\'Applied Offers\', \'user-column\');
    $defaults[\'usercolumn-totaloffers\'] = __(\'Total Offers\', \'user-column\');
    return $defaults;
}

function custom_column_company($value, $column_name, $id) {
    if( $column_name == \'usercolumn-company\' ) {
        return get_the_author_meta( \'company\', $id );
    }
    elseif( $column_name == \'usercolumn-myoffers\' ) {
        return get_the_author_meta( \'offersiget\', $id );
    }
    elseif( $column_name == \'usercolumn-totaloffers\' ) {
        return get_the_author_meta( \'totaloffers\', $id );
    }
}

add_action(\'manage_users_custom_column\', \'custom_column_company\', 15, 3);
add_filter(\'manage_users_columns\', \'column_company\', 15, 1);
我返回get\\u the\\u author\\u meta(\'offersget\',$id);

这里我从db中得到一些帖子id作为文本,比如:8,12,51

是否可以返回此内容,而不是作为文本,而是作为指向编辑帖子的链接(/wp admin/post.php?post=(8)&;操作=编辑)?

我试着用@hakre

return sprintf(\'<a href="/wp-admin/post.php?post=\'.get_the_author_meta( \'offersiget\', $id ).\'&action=edit">\'.get_the_author_meta( \'offersiget\', $id ).\'</a>\');
它可以工作,但它会返回一个包含所有帖子id的链接,是否可以拆分它们?

有什么帮助吗?

谢谢,菲利普

2 个回复
最合适的回答,由SO网友:t31os 整理而成

你的get_the_author_meta() 调用必须返回一个字符串(在您的注释之后),所以您需要做的是将该字符串拆分为一个数组,在其上循环并构建一个链接数组,然后在最后使用分隔符将其重新连接在一起。

/*
    Meh, no license, do as you want with it..
    Example code
*/
function ids_to_post_editlink( $ids = array(), $seperator = \' \' ) {
    if( empty( $ids ) || \'\' == $ids )
        return;

    if( !is_array( $ids ) )
        $ids = explode( \',\', $ids );

    $links = array();
    $ids   = array_map( \'intval\', $ids );

    foreach( $ids as $pid )
        $links[] = "<a href=\'" . admin_url( "post.php?action=edit&post=$pid" ) . "\'>$pid</a>";

    return implode( $seperator, $links );
}
在代码旁边添加该函数,然后更新get_the_author_meta 调用以下内容之一。。

 // Default(space seperated)
 return ids_to_post_editlink( get_the_author_meta( \'your-meta-key\', $id ) );

 // Comma seperated 
 return ids_to_post_editlink( get_the_author_meta( \'your-meta-key\', $id ), \', \' );

 // Pipe seperated 
 return ids_to_post_editlink( get_the_author_meta( \'your-meta-key\', $id ), \' | \' );
希望这有帮助。。

SO网友:hakre

就您所关心的基于postID构建链接而言,以下是一些示例代码:

/**
 * Copyright 2011 by hakre <http://hakre.wordpress.com/>, some rights reserved
 *
 * Provided AS-IS, example code only. 
 *
 * Licensed under CC BY-SA 2.5, needs attribution.
 *   * Author: hakre <http://hakre.wordpress.com/> 
 *   * Where : With the code and the visible portion of 
 *             any remixed work.
 * Exceptions to this need the written form.
 *
 * If you make use of this code, please send me a deposit copy, thx.
 */
# get the link for a specific postId
$getPostIdLink = function($postId) {
    return sprintf(\'/wp-admin/post.php?post=%d&action=edit\', $postId);
}
$getHtmlOfLinks = function($postIdsCommaSeperatedText) use ($formatPostIdLink) {
    $links = array_map(function($postId) use ($formatPostIdLink) {
        $postId = intval(trim($postId));
        if ($postId) {
            $link = $formatPostIdLink($postId);
            return sprintf(\'<a href="%s">(%d)</a>\', $link, $postId);
        } else {
             return false;
        }
    }, explode(\',\', $postIdsCommaSeperatedText));
    $links = array_filter($links);
    return implode(\', \', $links);
}

echo $getHtmlOfLinks(\'8,12,51\'); # do the output, 
# should give you (8), (12), (51) [brackets are linked]
或更新问题:

return $getHtmlOfLinks(get_the_author_meta( \'offersiget\', $id ));

结束