^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数add_rewrite_tag()用法示例

add_rewrite_tag( string $tag,  string $regex,  string $query = '' )

Add a new rewrite tag (like %postname%).


描述

The $query parameter is optional. If it is omitted you must ensure that you call this on, or before, the ‘init’ hook. This is because $query defaults to "$tag=", and for this to work a new query var has to be added.


参数

$tag

(string)
(Required)
Name of the new rewrite tag.

$regex

(string)
(Required)
Regular expression to substitute the tag for in rewrite rules.

$query

(string)
(Optional)
String to append to the rewritten query. Must end in ‘=’.

Default value: ”


源代码

File: wp-includes/rewrite.php

function add_rewrite_tag( $tag, $regex, $query = '' ) {
	// validate the tag's name
	if ( strlen( $tag ) < 3 || $tag[0] != '%' || $tag[ strlen($tag) - 1 ] != '%' )
		return;

	global $wp_rewrite, $wp;

	if ( empty( $query ) ) {
		$qv = trim( $tag, '%' );
		$wp->add_query_var( $qv );
		$query = $qv . '=';
	}

	$wp_rewrite->add_rewrite_tag( $tag, $regex, $query );
}

更新日志

Version 描述
2.1.0 Introduced.

相关函数

Uses

  • wp-includes/class-wp.php:
    WP::add_query_var()
  • wp-includes/class-wp-rewrite.php:
    WP_Rewrite::add_rewrite_tag()

Used By

  • wp-includes/class-wp-taxonomy.php:
    WP_Taxonomy::add_rewrite_rules()
  • wp-includes/class-wp-post-type.php:
    WP_Post_Type::add_rewrite_rules()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

    Contributed by bcworkz

    In the following examples, imagine a site has a custom taxonomy ‘location’ and all posts are assigned a location term like “Paris” or “Madrid”. We add a rewrite tag “%location%” to establish the location query var. We also add a rewrite rule so that an URL such as example.com/goto/madrid/budget-lodging/ is properly handled.

    add_action('init', 'add_my_rewrites');
    function add_my_rewrites() {
        add_rewrite_tag('%location%', '([^&]+)', 'location=');
        add_rewrite_rule('^goto/([^/]*)/([^/]*)/?','index.php?location=$matches[1]&name=$matches[2]','top');
    }

    Even though rewrite tags look just like permalink structure tags, if you try to use your rewrite tag in a permalink structure, the URLs generated by WordPress will look something like example.com/goto/%location%/budget-lodging/. The proper term does not replace the rewrite tag as you might expect. To make your tag behave like a structure tag, use the “post_link” filter to replace the tag with the proper term.

    // Assign value to %location% rewrite tag
    add_filter('post_link', 'my_filter_post_link', 10, 2 );
    function my_filter_post_link( $permalink, $post ) {
    
        // bail if %location% tag is not present in the url:
        if ( false === strpos( $permalink, '%location%'))
            return $permalink;
    
        $terms = wp_get_post_terms( $post->ID, 'location');
        // set location, if no location is found, provide a default value.
        if ( 0 < count( $terms ))
            $location = $terms[0]->slug;
        else
            $location = 'timbuktu';
        $location = urlencode( $location );
        $permalink = str_replace('%location%', $location , $permalink );
     
        return $permalink;
    }

    Anytime you change something 相关函数 to the Rewrite API, don’t forget to flush the rewrite rules! This can be done without code by going to Permalink Settings and clicking Save Changes. You don’t actually need to make any changes on the settings screen.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈