Create a gist now

Instantly share code, notes, and snippets.

%book-cat%を置き換える関数
/*
* %book-cat%を置き換えて表示
*/
function post_type_book_filter_post_link( $permalink, $post ) {
if ( false === strpos( $permalink, '%book-cat%' ) ) {
// パーマリンクの中に%book-cat%がなければそのまま返す
return $permalink;
}else{
// %book-cat%を投稿記事で指定したタクソノミーに置き換える
$terms = current(wp_get_post_terms( $post->ID, 'post_type_book_category', array('fields' => 'slugs')));
$permalink = str_replace( '%book-cat%', $terms , $permalink );
return $permalink;
}
}
add_filter('post_link', 'post_type_book_filter_post_link' , 10, 2 );
add_filter('post_type_link','post_type_book_filter_post_link' , 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment