-3

如何在数字前插入字符串?

我有以下 Wordpress 功能:

get_the_term_list( $post->ID, 'level', '', ', ', '' )

此函数输出如下数字:

<a href="#">1</a>

或者,如果数据库中有多个条目:

<a href="#">1</a>, <a href="#">2</a>

如何在数字前插入“级别”?使它看起来像这样:

<a href="#">level 1</a>

或者:

<a href="#">level 1</a>, <a href="#">level 2</a>
4

4 回答 4

4

功能参考/获取术语列表

worpress 手册会告诉您确切的方法。

我不使用 wordpress,但通过“worpress get_the_term_list”找到它

于 2012-11-08T21:05:22.657 回答
2

您只需匹配数字即可。在正则表达式\d+中可用于小数。

 $text = preg_replace('#>(\d+)<#', '>level $1<', $text);

你可以只使用><锚点。这里$1重新插入找到的小数。

于 2012-11-08T21:05:36.967 回答
0

测试这些。您可能需要调整偏移量、位置编号。这将使您达到 99%。

$numPos = strpos($string, "1");

$firstStr = substr($string, 0, $pos); $secondStr = substr($string, $pos);

$newStr = $firstStr 。“等级” 。$secondStr;

于 2012-11-08T21:10:25.073 回答
0

以下是您的操作方法:

get_the_term_list( $post->ID, 'level', 'Level: ', ', ', '' );

参考:http ://codex.wordpress.org/Function_Reference/get_the_term_list

编辑:

编辑到get_the_term_list_wp-includes/category-template.php

/**
 * Retrieve a post's terms as a list with specified format.
 *
 * @since 2.5.0
 *
 * @param int $id Post ID.
 * @param string $taxonomy Taxonomy name.
 * @param string $before Optional. Before list.
 * @param string $sep Optional. Separate items using this.
 * @param string $after Optional. After list.
  * @param string $beforeEach Optional. After each term.
 * @return string
 */
function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '', $beforeEach = '' ) {
    $terms = get_the_terms( $id, $taxonomy );

    if ( is_wp_error( $terms ) )
        return $terms;

    if ( empty( $terms ) )
        return false;

    foreach ( $terms as $term ) {
        $link = get_term_link( $term, $taxonomy );
        if ( is_wp_error( $link ) )
            return $link;
        $term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">'. $beforeEach . $term->name . '</a>';
    }

    $term_links = apply_filters( "term_links-$taxonomy", $term_links );

    return $before . join( $sep, $term_links ) . $after;
}

用法:

get_the_term_list( $post->ID, 'level', '', ', ', '', 'Level: ');
于 2012-11-08T21:06:52.440 回答