get_the_terms
でタームが取得出来ます。
関数リファレンス/get the terms
manbo 様
回答ありがとうございます。
初心者なもので、あまりWPについてリファレンスを見ただけでは実装に
までには至りませんでした。
もしよければ、コードを教えていただいてもよろしいでしょうか
現在、検索結果ページには
<?php if(is_search()): ?>
<?php single_term_title('今表示しているカテゴリは'); ?>
<?php endif; ?>
という風にしており、
用途(タクソノミー名:use)
・家用
・会社用
場所(タクソノミー名:place)
・県内
・県外
どちらかを選択した場合は
今表示しているカテゴリは家用
と表示されますが
二つを同時に検索した場合は
片方しか表示されません。
検索結果より結果に至るページのコードを提示してください。
manbo様
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<dl>
<dt>用途</dt>
<dd>
<select id="input-id" name="use">
<option value="" selected="true">選択してください</option>
<?php
$terms = get_terms('use' , 'get=all');
foreach($terms as $term){
echo '<option value="' . $term->slug . '">' .
esc_html($term -> name) .
'</option>';
}
?>
</select>
</dd>
</dl>
<dl>
<dt>場所</dt>
<dd>
<input type="hidden" name="s">
<input type="hidden" name="post_type" value="construction" />
<select id="input-id" name="place">
<option value="" selected="true">選択してください</option>
<?php
$terms = get_terms('place' , 'get=all');
foreach($terms as $term){
echo '<option value="' . $term->slug . '">' .
esc_html($term -> name) .
'</option>';
}
?>
</select>
</dd>
</dl>
<div class="btn"><button type="submit" class="submit-btn">検索</button></div>
</form>
このような感じです
ちょっと勘違いしてました。
テンプレートそのものを触らないと無理じゃないでしょうか。
今、手元に試せる環境が無いので曖昧ですが…
manbo 様
そうなのですか。
テンプレをどのように修正したら、よろしいでしょうか?
検索結果を表示するテンプレートに以下のように書いたらどうでしょうか?
<?php
global $post_type;
if ( $post_type === 'mono' ) {
$use_id = term_exists( $_GET['use'] );
$place_id = term_exists( $_GET['place'] );
echo '用途:'.get_term( $use_id )->name .'場所:'.get_term( $place_id)->name;
}
?>
manbo 様
ありがとうございます。
うまく表示できました。
タクソノミーを選択しないで、空検索した場合に
「用途:場所:」になってしまうのを
「全ての用途・場所」のように表示するテキストを切り替えるのは可能なのでしょうか?
未選択は全く考慮してませんでした。両方未選択、片方未選択のパターンで考えるとこんな感じでしょうか?
<?php
global $post_type;
if ( $post_type === 'mono' );
if ( $_GET['use'] ) {
$use_id = term_exists( $_GET['use'] );
$use_text = '用途:'.get_term( $use_id )->name ;
} else {
$use_text = '全ての用途';
}
if ( $_GET['place'] ) {
$place_id = term_exists( $_GET['place'] );
$place_text = '場所:'.get_term( $place_id )->name;
} else {
$place_text = '全ての場所';
}
echo $use_text, $place_text;
?>
manbo 様
ありがとうございました。
うまくできました。
毎回他のトピックでも、回答いただきまして、
ありがとうございます。
また、よろしくお願い致します。