Skip to content

Instantly share code, notes, and snippets.

@gatespace
Last active January 11, 2018 01:30
Show Gist options
  • Save gatespace/c5535398ebe2caf92413d5e48cd3d551 to your computer and use it in GitHub Desktop.
Save gatespace/c5535398ebe2caf92413d5e48cd3d551 to your computer and use it in GitHub Desktop.
[WordPress] タグアーカイブのパーマリンクをタグIDにする ref: https://qiita.com/gatespace/items/4548fd0b9540f46b86b1
<?php
function numeric_term_link( $url, $term, $taxonomy ) {
global $wp_rewrite;
// post_tag only
if ( $taxonomy == 'post_tag' ) {
if ( $wp_rewrite->using_permalinks() ) {
$permastruct = $wp_rewrite->get_extra_permastruct( 'post_tag' );
$permastruct = str_replace( '%post_tag%', $term->term_id, $permastruct );
$url = home_url( user_trailingslashit( $permastruct, 'tag' ) );
}
}
return $url;
}
add_filter( 'term_link', 'numeric_term_link', 10, 3 );
function add_tag_id_to_qvar( $vars ) {
$vars[] = 'tag_id';
return $vars;
}
add_filter( 'query_vars', 'add_tag_id_to_qvar' );
function numeric_tag_rewrite_rules( $rules ) {
$custom_rules = array();
foreach ( $rules as $regex => $rewrite ) {
$regex = str_replace( '/(.+?)/', '/([0-9]{1,})/', $regex );
$rewrite = str_replace( '?tag=', '?tag_id=', $rewrite );
$custom_rules[$regex] = $rewrite;
}
return $custom_rules;
}
add_filter( 'post_tag_rewrite_rules', 'numeric_tag_rewrite_rules' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment