カスタムメニューを使って抜粋文を表示させたい
-
カスタムメニューに登録した投稿記事のサムネイルとタイトル、
そして記事の抜粋文を表示させることは出来ないのかと思い、
何とか行き着いたものが、下記ソースをfunctions.phpに記載することで、
画像とタイトルは表示させることが出来ました。しかしどうしても解決出来ないことがあり、投稿させていただきました。
よくある画像の横にタイトルと抜粋文という構成にしたいと思っているのですが、
抜粋文がどうしても表示できないのです。「$item_output .= get_the_post_thumbnail~」のように追記するのか、
「$item_output .= $args->link_before . apply_filters( ‘the_title’~」のようにするのか。あれこれ試してみたのですがいずれも表示できず。
ちなみに、カスタムメニューに追加した際に表示される「説明」の表示は実現できました。
しかし希望は記事の抜粋文なのです。class Walker_Nav_Menu_Test extends Walker_Nav_Menu { function start_el(&$output, $item, $depth, $args) { $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $classes = empty( $item->classes ) ? array() : (array) $item->classes; $classes[] = 'menu-item-' . $item->ID; $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) ); $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth ); $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; $output .= $indent . '<li' . $id . $class_names . '>'; $atts = array(); $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; $atts['target'] = ! empty( $item->target ) ? $item->target : ''; $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; $atts['href'] = ! empty( $item->url ) ? $item->url : ''; $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth ); $attributes = ''; foreach ( $atts as $attr => $value ) { if ( ! empty( $value ) ) { $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); $attributes .= ' ' . $attr . '="' . $value . '"'; } } $item_output = $args->before; $item_output .= '<a'. $attributes .'>'; $item_output .= get_the_post_thumbnail($item->object_id, '', array('alt' => $item->title)); $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } }
これで出力されるのが下記になります。
<a href="XXXX"> <img src="http://~/wp-content/uploads/2015/08/IMG_6694a.jpg" class="attachment-120x80 wp-post-image" alt="XXXXX /> ●●●●●●(記事タイトル) </a>
どうかご教授いただければ幸いです。
よろしくお願いいたします。
8件の返信を表示中 - 1 - 8件目 (全8件中)
8件の返信を表示中 - 1 - 8件目 (全8件中)
- トピック「カスタムメニューを使って抜粋文を表示させたい」には新たに返信することはできません。