Wonderful! WordPress

いつのまにか get_category_link が機能していない

ふと、自分のサイトをあれこれ見ていたら、カスタム投稿のページのカスタム分類のリスト一覧のリンクが全く機能していないことに気が付きました。
そのカスタム投稿用のカスタム分類で、それぞれの分類に絞ったページを表示するためのリンクなのですが。
どうしたものかとソースを覗き見てみると、href="" となっていて、リンクの中身は空っぽで出力されていないという状態です。ついこの前までちゃんと機能していたのに、と思いつつ、4.9になってこうなったのかもしれないと。
とりあえずやっていたことはといえば、

<nav id="navx4">
	<a href="#" id="navx4a">Gulls list</a>
	<ul id="taxlist">
		<?php
			$args = array(
				'orderby' => 'name',
				'number' => '',
				'taxonomy' => 'gull',//カスタム分類名
			);
			$gulls = get_categories( $args );
			foreach( $gulls as $val ) {
				$descrips = explode( ':', $val->description );
				echo '<li><a href="' . get_category_link( $val->term_id ) . '">' . $descrips[0] . ' (' . $val->count . ')</a></li>';
			}
		?>
	</ul>
</nav>

なんてことはなく、 get_categories() でそのカスタム分類の項目を全て取得し、ループにおいてget_category_link() でそれぞれの分類で絞り込んだページとなるようにリンクを取得していたというだけのこと。本来はこれを使用しないのかもしれないのだけれど、これでちゃんと機能していたのであまり深く考えなかったということなのです。
で、いろいろとやってみると、 get_categories() はちゃんと機能していて、おかしくなったのはget_category_link() のほうだということがわかりました。
とりあえず、リンクをちゃんと機能させるということが先決なのでcodex を代わりの方法をと探して、実はそのまま使用できるものがあったのでそのままとってきて以下の方法に変更しました。

<nav id="navx4">
	<a href="#" id="navx4a">Gulls list</a>
	<ul id="taxlist">
		<?php
			$terms = get_terms( 'gull', 'orderby=name' );
			foreach ( $terms as $term ) {
				// この $term はオブジェクトなので、$taxonomy を指定しなくてよい。
				$term_link = get_term_link( $term );

				// エラーなら次のタームへ進む。
				if ( is_wp_error( $term_link ) ) {
					continue;
				}

				// リンクを取得できたのでプリントアウトする。
				$descrips = explode( ':', $term->description );
				echo '<li><a href="' . esc_url( $term_link ) . '">' . $descrips[0] . ' (' . $term->count . ')</a></li>';
			}
		?>
	</ul>
</nav>

get_terms() でカスタム分類の項目を全て取得し、リンクはget_term_link() で出力させるということです。まぁ、なんというか get_categories() は内部でget_terms() を呼び出して結果を取得しているし、get_category_link() も同様にget_term_link() を呼び出しているわけなので、同じ事と言えばそうであって、しかし余計な部分を通らないだけよほどスマートであるわけで、当然のことながら、これの方が本来のやり方ということではあります。

ところで、何がどう変わってget_category_link() が機能しなくなってしまったのかということがちょっと気になります。
get_category_link()/wp-includes/category-template.php の一番はじめに書いてあります。古いバージョンと見比べてみると、とてもシンプルなこの関数は全く変えられていません。その内部では、get_term_link() を呼び出しています。
で、次なるはget_term_link() を見比べてみると、get_category_link() にterm_id だけを渡した場合の処理においては、これも全く同じままでした。
そして、そのget_term_link() から呼ばれるget_term() においても同じであり、結果、最終的には/wp-includes/class-wp-term.phpclass WP_termget_instance() にたどりつきます。
そのget_instance() の変更されている箇所を含む、先頭の部分を抜き出してみると以下のとおり。

<?php
	public static function get_instance( $term_id, $taxonomy = null ) {
		global $wpdb;

		$term_id = (int) $term_id;
		if ( ! $term_id ) {
			return false;
		}

		$_term = wp_cache_get( $term_id, 'terms' );

		// If there isn't a cached version, hit the database.
		if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
			// Any term found in the cache is not a match, so don't use it.
			$_term = false;

			// Grab all matching terms, in case any are shared between taxonomies.
			$terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d", $term_id ) );
			if ( ! $terms ) {
				return false;
			}
			・
			・
			↓続く
?>

見比べてみたところ、15行目の短い一文「$_term = false;」が追加されているだけなのがわかりました。コメントは「Any term found in the cache is not a match, so don't use it.」と説明してあります。
ちなみに、この一文をコメントアウトしてやれば、当然のことながら以前のようにカスタム分類におけるget_category_link()がちゃんと機能するようになります。
この一文により、カスタム分類どころか「post_tag」も機能しなくなるので、これは「category」専用関数にしたということなのかも。

ふと、この件をネットで探して見ると、get_category_link() が機能しなくなったと言っている人がいて、その返答はやはりget_terms()get_term_link() を使用する方法を推奨していました。
まぁ、自分のサイトであればパーマリンク設定を変えることもないであろうし、たとえば自分の場合のカスタム分類「gull」へのリンクであれば、先のコードの14行目を下のようにすればすむだけということでもあります。

<?php
	echo '<li><a href="http://strix.main.jp/?gull=' . $val->slug . '">' . $descrips[0] . ' (' . $val->count . ')</a></li>';
?>

Leave a Reply!

JavaScript is necessary to send a comment.
You can edit and delete your comment if you input a edit key.
Edit key is necessary for attesting you when you edit and delete it.
The tag of HTML cannot be used in comment.
When you comment for the first time, it is displayed after the approval of the administrator.
Because I cannot speak English so much, it takes time to answer.
Required fields are marked *.

※Please enter more than 5 characters only alphabets.
※Edit or delete are possible for 2000 days after approval.

*

♠Simplistic Comment User Editable v4.0

♠When visitors leave comments on the site this site collect the data shown in the comments form, and also the visitor’s IP address and browser user agent string to help spam detection.
♠This site does not use cookie when visitors leave comments and commenter edit comment.
♠This site uses Akismet to reduce spam. Learn how your comment data is processed.

Comments feed

Trackback URL : https://strix.main.jp/wp-trackback.php?p=102759

Sanbanse Funabashi
2011.01.01 sunrise

Top

スクロールさせるか画像をクリックすると元に戻ります。