サブループの呼び出しセットです。
ショートコードでも対応できると思いますが、装飾が面倒なので。
optionblock samplesample subloo set
サブループの呼び出しセットです。
ショートコードでも対応できると思いますが、装飾が面倒なので。
グリッド表示&カルーセル動作、ループタイトルなし
アコーディオン動作となり、記事へはリンクしません。
FAQなどで使われると思いますので、「本文を全て表示」としてあります。
特定のタクソノミーのタームだけ、アーカイブやシングルに出力したい時に。
例えば、
みたいなケース。
function post_terms( $atts ){
if( !empty( $atts['tax'] ) ){
$tax = $atts['tax'];
}else{
$tax = 'category';
}
if( !empty( $atts['wrapper'] ) ){
$wrapper = $atts['wrapper'];
}else{
$wrapper = 'span';
}
if( !empty( $atts['class'] ) ){
$class = ' ' . $atts['class'];
}else{
$class = '';
}
$output = '';
$output .= '<' . $wrapper . ' class="taglist-post-terms taglist-post-terms-' . $tax . $class . '">';
$terms = get_the_terms( '', $atts['tax'] );
if( !empty( $terms ) ){
foreach ( $terms as $term ){
$output .= '<span class="taglist-tax taglist-tax-' . $tax . '">';
$parent = $term->parent;
if( !empty( $parent ) ){
$parent_class = ' parent-term-id-' . $parent;
}else{
$parent_class = ' parent-term-none';
}
if( !empty( $atts['link'] ) && $atts['link'] == 'false' ){
$link_begin = '';
$link_end = '';
}else{
$link_begin = '<a href="' . get_term_link( $term ) . '" class="taglist-term taglist-term-' . esc_html( $term->slug ) . ' term-id-' . $term->term_id . $parent_class . '"' . '>';
$link_end = '</a>';
}
$output .= $link_begin . $term->name . $link_end;
$output .= '</span>';
}
$output .= '</' . $wrapper . '>';
}
return $output;
}
add_shortcode( 'post_terms', 'post_terms' );
cssクラスは適当に変更してください。
タームの階層構造には対応していませんが、一応親子にそれぞれclassは付与しています。※孫も子扱い
デフォルトのテンプレートタグに近しいものがあったと思うけど、使用中のテーマで都合がいい形にしたい場合に。
で、いちいちタクソノミー情報を取得して、タームをforeachで拾って、、、とかが面倒なときに。
<?php echo do_shortcode( '[post_terms tax="post_tag" link="false" wrapper="p" class="taglist"]' ); ?>
※ショートコードが動作しないよう[]を[]に変換しています。お試しの際は[]に書き戻してください。
ショートコードタクソノミータームPHPカスタマイズ関数
以前書いた「Search & Filter Pro 検索結果ページに検索ワードを出力する」は、チェックボックスの複数選択に対応していなかったので書き直し。多次元連想配列の操作はなかなか難しい。
global $searchandfilter;
$sf_current_query = $searchandfilter->get( '●●●' )->current_query();
$result = $sf_current_query->get_array();
$free_word = $sf_current_query->get_search_term();
echo '<h5 class="search-result">';
echo '<span class="search-label">検索条件:</span>';
if( empty( $result ) && empty( $free_word ) ){
echo '<span class="search-word tax-word">指定なし</span>';
}else{
foreach( $result as $key1 => $value1 ){
$value1 = $value1["active_terms"];
echo '<span class="search-word term-word search-condition' . $key1 . '">「';
foreach( $value1 as $key2 => $value2 ){
echo '<span class="search-item">' . $value2["name"] . '</span>';
}
echo '」</span>';
}
if( !empty( $free_word ) ){
echo '<span class="search-word free-word">「' . $free_word . '」</span>';
}
}
echo '</span>';
echo '</h5>';
●●●はお馴染みのID。
ついでに、条件を一切指定しなかった場合に「指定なし」と出すように。
検索項目ごとにクラス名を付与しているので、区切りなどはCSSで適当に。
「wp_nav_menu」で出力されるメニュー構文は「ul li a」のような最低限。
ここに、少し細工が必要な場合のカスタマイズ方法です。
class custom_walker_nav_menu extends Walker_Nav_Menu {
function start_lvl( &$output, $depth = 0, $args = array() ) {
$output .= '<button class="submenu-button">▼</button><ul class="sub-menu">';
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$output .= '</ul>';
}
}
「sub-menu」直前に「button」が挿入されるので、あとはマウスオーバーやタップに応じてjQuery等でアクションを追加します。
「button」の挿入自体もJavaScriptで簡単にできるので、こちらはwalkerを使わなくてもいいかも。
class custom_walker_nav_menu extends Walker_Nav_Menu {
function start_lvl( &$output, $depth = 0, $args = array() ) {
$output .= '<div class="sub-menu"><ul class="ul-sub-menu">';
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$output .= '</ul></div>';
}
}
こちらはサブメニューにコンテナを追加します。CSSだけではきれいに整形できない場合に。
$args = array(
'theme_location' => 'primary',
'container_id' => 'navigation-menu',
'container_class' => 'menu-main-container',
'menu_id' => 'menu-main',
'menu_class' => 'menu nav-menu',
'walker' => new custom_walker_nav_menu
);
wp_nav_menu( $args );
メニューを複数登録してあり、そのうちの「primary」に適用しています。「responsive」やら「humbarger」になることもあるでしょう。
クラスやIDは任意で。
もっと複雑な入れ子にしたり、見出しを足したり、たいがいの事はこの処理で出来そうです。
参考:
カスタム投稿をテキストエリアで追加する際、連想配列だと応用が利かないな、と気が付いて多次元配列で書き直してみました。
目次
news,ニュース,text
q_and_a,Q and A,text
event,イベント,grid
document,資料,thumbnail
job_information,求人情報,grid
memo,メモ書き,text
if ( !function_exists( 'add_post_types' ) ){
function add_post_types(){
global $custom_post_types;
if( get_option( 'option_custom_post_types' ) ){
$custom_post_text = get_option( 'option_custom_post_types' );
}else{
$custom_post_text = '';
}
$custom_post_text_row = str_replace( array( "\r\n", "\r", "\n" ), "\n", $custom_post_text );
$custom_post_data_rows = explode( "\n", $custom_post_text_row );
if( $custom_post_text ){
foreach( $custom_post_data_rows as $custom_post_data_row ){
$custom_post_types[] = explode( ",", $custom_post_data_row );
}
}
}
add_action( 'after_setup_theme', 'add_post_types' );
}
オプション「option_custom_post_types」を読み込み、
改行で分解して配列に代入⇒さらにカンマで分解して多次元配列に代入しています。
global $custom_post_types;
if( get_option( 'option_custom_post_types' ) ){
$custom_post_text = get_option( 'option_custom_post_types' );
}else{
$custom_post_text = '';
}
if( $custom_post_text ){
foreach( $custom_post_types as $custom_post_type ){
$post_type_name = $custom_post_type[0];
if( empty( $custom_post_type[1] ) ){
$post_type_label = $custom_post_type[0];
}else{
$post_type_label = $custom_post_type[1];
}
$tax1_name = $post_type_name . '_tax1';
$tax1_label = $post_type_label . '分類1';
$tax2_name = $post_type_name . '_tax2';
$tax2_label = $post_type_label . '分類2';
register_post_type( $post_type_name, array(
'labels' => array(
'name' => __( $post_type_label ),
'singular_name' => __( $post_type_label )
),
'description' => '',
'public' => true,
'has_archive' => true,
'show_in_rest' => true,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ,'comments', 'page-attributes' )
));
register_taxonomy(
$tax1_name,
$post_type_name,
array(
'show_in_rest' => true,
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'label' => $tax1_label,
'singular_label' => $tax1_label,
'public' => true,
'show_ui' => true,
'show_admin_column' => true
)
);
register_taxonomy(
$tax2_name,
$post_type_name,
array(
'show_in_rest' => true,
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'label' => $tax2_label,
'singular_label' => $tax2_label,
'public' => true,
'show_ui' => true,
'show_admin_column' => true
)
);
}
};
投稿タイプすべてに決め打ちで「分類1」「分類2」を追加します。
投稿タイプごとにタクソノミーを変えたい場合は条件分岐しまくりで変更することになると思います。
global $custom_post_types;
if( get_option( 'option_custom_post_types' ) ){
$custom_post_text = get_option( 'option_custom_post_types' );
}else{
$custom_post_text = '';
}
foreach( $custom_post_types as $custom_post_type ){
if( !empty( $custom_post_type[0] ) && !empty( $custom_post_type[2] ) ){
if( get_post_type() === $custom_post_type[0] ){
$displaystyle = $custom_post_type[2];
}
}
}
投稿タイプ毎に「表示スタイル」を取り出しています。
どっちを使うか、私のように場当たりで書いているとなかなか判断が付かないのですが、
こんな印象です。
多次元配列なら「投稿タイプ名(スラッグ),ラベル,表示スタイル、タクソノミー1のスラッグ、タクソノミー1のラベル、タクソノミー2のスラッグ、タクソノミー2のラベル」
のようにいくらでも値を追加できるので、今ケースは多次元配列でいいかな。
現在「固定ページ」を表示中
post_type namepage
post_type label固定ページ
is_front_pagefalse
is_pagetrue
is_singlefalse
is_attachmentfalse
is_homefalse
is_post_type_topfalse
get_post_typetrue
is_post_type_archivefalse
is_archivefalse
is_categoryfalse
is_tagfalse
is_taxfalse
is_searchfalse
is_authorfalse
is_datefalse
is_dayfalse
is_monthfalse
is_yearfalse
is_404false
wp_is_mobilefalse
タッチ
画面短辺
水平方向幅
デバイス方向
user agent:
CCBot/2.0 (https://commoncrawl.org/faq/)