サポート » 使い方全般 » wp_list_pages()以外でcurrent_page_itemを付与させたい

  • 解決済 t.harada

    (@tharada)


    サイドバーに以下のように記述し、固定ページの子ページ一覧をカスタムフィールドに設定した値と共に表示させています。

    <ul id="sub">
    <?php
    	$child_posts = get_posts( '&order=ASC&orderby=date&post_type=page&meta_key=textfield_business' );
    	if ( $child_posts ) {
    		foreach ( $child_posts as $child ) {
    			$eye_catch = get_post_thumbnail_id($child->ID);
    			$c_pagelink = get_page_link( $child->ID );
    			$c_title = apply_filters( 'the_title', $child->post_title );
    			$c_business = get_post_meta( $child->ID, textfield_business, true );
    			$c_name = get_post_meta( $child->ID, textfield_name, true );
    ?>
    <li><a href="<?php echo $c_pagelink; ?>">
    <span class="text10"><?php echo $c_business; ?></span><br />
    <?php echo $c_name; ?></a>
    </li>
    <?php
    		}
    	}
    ?>
    </ul>

    と、ここまでは良かったのですが、表題の通り、現在表示しているページにcurrent_page_itemクラスを付与したいのです。
    wp_list_pages()を使えば自動的に付与されますが、カスタムフィールド値を表示させつつ、current_page_itemクラスを付与するにはどうしたら良いでしょうか?
    現在の子ページ・孫ページにいる場合、親ページをアクティブ表示が、これに近いのかなとは思うのですが、なにぶんPHPがよく分からず断念…。

    現在表示中のページへのリンクにcurrent_page_itemや任意のクラスを付与するプラグインでも結構です。
    ご存知の方、是非ご教授頂けませんでしょうか?

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 t.harada

    (@tharada)

    紆余曲折の末、$child->IDとget_the_ID()を変数に代入し、それぞれが等しい時にcurrent_page_itemクラスを付与するように書き換えてみました。

    <ul id="sub">
    <?php
    	$child_posts = get_posts( '&order=ASC&orderby=date&post_type=page&meta_key=textfield_business' );
    	if ( $child_posts ) {
    		foreach ( $child_posts as $child ) {
    			$eye_catch = get_post_thumbnail_id($child->ID);
    			$c_pagelink = get_page_link( $child->ID );
    			$c_title = apply_filters( 'the_title', $child->post_title );
    			$c_business = get_post_meta( $child->ID, textfield_business, true );
    			$c_name = get_post_meta( $child->ID, textfield_name, true );
    			$postid = $child->ID;
    			$postid2 = get_the_ID();
    ?>
    <li class="post-<?php echo $postid; ?><?php if ($postid == $postid2) { echo' current_page_item'; } ?>"><a href="<?php echo $c_pagelink; ?>">
    <span class="text10"><?php echo $c_business; ?></span><br />
    <?php echo $c_name; ?></a>
    </li>
    <?php
    		}
    	}
    ?>
    </ul>

    我ながら汚いコードですが、ようやく解決しました…。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「wp_list_pages()以外でcurrent_page_itemを付与させたい」には新たに返信することはできません。