LABE

[WP] Permalink Category assigned by Post Meta

Mar 10th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. /* https://ja.wordpress.org/support/topic/複数カテゴリーの場合、パーマリンクになるカテ/ */
  2. function change_post_link_category( $cat, $cats, $post ) {
  3.     $cats_id = array_map( function ( $cat ) {
  4.         return $cat->term_id;
  5.     }, $cats );
  6.     $category = false;
  7.     $post_meta = get_post_meta( $post->ID, 'permalink_cat', true );
  8.     if ( is_numeric( $post_meta ) )
  9.         $category = get_term( absint( $post_meta ), 'category' );
  10.     if ( ! $category )
  11.         $category = get_term_by( 'slug', $post_meta, 'category' );
  12.     if ( $category && in_array( $category->term_id, $cats_id ) )
  13.         return $category;
  14.     return $cat;
  15. }
  16. add_filter( 'post_link_category', 'change_post_link_category', 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment