人力検索はてな
モバイル版を表示しています。PC版はこちら
i-mobile

「wordpress」 で、「index.php」に記述したタグに基づき作成されるHTMLをキレイにできないかと悩んでます。

下記の希望を実現する方法をご存知の方がいらっしゃったら教えていただけないでしょうか。
※完璧でなくても、「有る程度できる」でも構いません。



●「index.php」に記述している内容

<?php wp_nav_menu(array('theme_location' => 'footerNav')); ?>



●今は、こんな感じでHTMLが作成されます。

<div id="footerNav">
<div class="menu-footernav-container"><ul id="menu-footernav" class="menu"><li id="111">・・・省略・・・</li>
<li id="222">・・・省略・・・</li>
<li id="333">・・・省略・・・</li>
</ul></div></div>



●こんな感じにできないでしょうか?

<div id="footerNav">
<div class="menu-footernav-container">
<ul id="menu-footernav" class="menu">
<li id="111">・・・省略・・・</li>
<li id="222">・・・省略・・・</li>
<li id="333">・・・省略・・・</li>
</ul>
</div>
</div>


どうぞ宜しくお願いいたします。


●質問者: kazoo9
●カテゴリ:インターネット ウェブ制作
✍キーワード:INDEX PHP wordpress キレイ タグ
○ 状態 :終了
└ 回答数 : 1/1件

▽最新の回答へ

1 ● rouge_2008
●60ポイント ベストアンサー

「wp-includes/nav-menu-template.php」を変更する必要があります。

※変更箇所は何箇所か程度ですが、変更箇所のあるソースを一部抜粋しましたので、バージョンに合わせて以下をそのまま貼り付けてみてください。

・バージョン3.0の場合(※57?235行目に該当)

/**
 * @see Walker::start_el()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param object $item Menu item data object.
 * @param int $depth Depth of menu item. Used for padding.
 * @param int $current_page Menu item ID.
 * @param object $args
 */
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';

$output .= $indent . "\t\t\t\t\t<li id=\"menu-item-". $item->ID . '"' . $value . $class_names .'>'; // Here my custom!

$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}

/**
 * @see Walker::end_el()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param object $item Page data object. Not used.
 * @param int $depth Depth of page. Not Used.
 */
function end_el(&$output, $item, $depth) {
$output .= "</li>\n";
}
}

/**
 * Displays a navigation menu.
 *
 * Optional $args contents:
 *
 * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank.
 * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
 * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
 * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
 * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
 * container_id - The ID that is applied to the container. Defaults to blank.
 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
 * before - Text before the link text.
 * after - Text after the link text.
 * link_before - Text before the link.
 * link_after - Text after the link.
 * echo - Whether to echo the menu or return it. Defaults to echo.
 * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0.
 * walker - allows a custom walker to be specified.
 * theme_location - the location in the theme to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
 *
 * @since 3.0.0
 *
 * @param array $args Arguments
 */
function wp_nav_menu( $args = array() ) {
static $menu_id_slugs = array();

$defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
'depth' => 0, 'walker' => '', 'theme_location' => '' );

$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'wp_nav_menu_args', $args );
$args = (object) $args;

// Get the nav menu based on the requested menu
$menu = wp_get_nav_menu_object( $args->menu );

// Get the nav menu based on the theme_location
if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );

// get the first menu that has items if we still can't find a menu
if ( ! $menu && !$args->theme_location ) {
$menus = wp_get_nav_menus();
foreach ( $menus as $menu_maybe ) {
if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) {
$menu = $menu_maybe;
break;
}
}
}

// If the menu exists, get its items.
if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
$menu_items = wp_get_nav_menu_items( $menu->term_id );

// If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
&& ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) )
return call_user_func( $args->fallback_cb, (array) $args );

// If no fallback function was specified and the menu doesn't exists, bail.
if ( !$menu || is_wp_error($menu) )
return false;

$nav_menu = $items = '';

$show_container = false;
if ( $args->container ) {
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
if ( in_array( $args->container, $allowed_tags ) ) {
$show_container = true;
$class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
$id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
$nav_menu .= '<'. $args->container . $id . $class . ">\n"; // Here my custom!
}
}

// Set up the $menu_item variables
_wp_menu_item_classes_by_context( $menu_items );

$sorted_menu_items = array();
foreach ( (array) $menu_items as $key => $menu_item )
$sorted_menu_items[$menu_item->menu_order] = $menu_item;

unset($menu_items);

$items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
unset($sorted_menu_items);

// Attributes
if ( ! empty( $args->menu_id ) ) {
$slug = $args->menu_id;
} else {
$slug = 'menu-' . $menu->slug;
while ( in_array( $slug, $menu_id_slugs ) ) {
if ( preg_match( '#-(\d+)$#', $slug, $matches ) )
$slug = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $slug);
else
$slug = $slug . '-1';
}
}
$menu_id_slugs[] = $slug;
$attributes = ' id="' . $slug . '"';
$attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';

$nav_menu .= "\t\t\t\t<ul". $attributes .">\n"; // Here my custom!

// Allow plugins to hook into the menu to add their own <li>'s
$items = apply_filters( 'wp_nav_menu_items', $items, $args );
$items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
$nav_menu .= $items;
unset($items);

$nav_menu .= "\t\t\t\t</ul>\n";

if ( $show_container )
$nav_menu .= "\t\t\t</" . $args->container . '>'; // Here my custom!

$nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );

if ( $args->echo )
echo $nav_menu;
else
return $nav_menu;
}

・バージョン3.0.1の場合(※57?239行目に該当)

/**
 * @see Walker::start_el()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param object $item Menu item data object.
 * @param int $depth Depth of menu item. Used for padding.
 * @param int $current_page Menu item ID.
 * @param object $args
 */
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$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 ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';

$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

$output .= $indent . "\t\t\t\t\t<li" . $id . $value . $class_names .">"; // Here my custom!

$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;

$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}

/**
 * @see Walker::end_el()
 * @since 3.0.0
 *
 * @param string $output Passed by reference. Used to append additional content.
 * @param object $item Page data object. Not used.
 * @param int $depth Depth of page. Not Used.
 */
function end_el(&$output, $item, $depth) {
$output .= "</li>\n";
}
}

/**
 * Displays a navigation menu.
 *
 * Optional $args contents:
 *
 * menu - The menu that is desired. Accepts (matching in order) id, slug, name. Defaults to blank.
 * menu_class - CSS class to use for the ul element which forms the menu. Defaults to 'menu'.
 * menu_id - The ID that is applied to the ul element which forms the menu. Defaults to the menu slug, incremented.
 * container - Whether to wrap the ul, and what to wrap it with. Defaults to 'div'.
 * container_class - the class that is applied to the container. Defaults to 'menu-{menu slug}-container'.
 * container_id - The ID that is applied to the container. Defaults to blank.
 * fallback_cb - If the menu doesn't exists, a callback function will fire. Defaults to 'wp_page_menu'.
 * before - Text before the link text.
 * after - Text after the link text.
 * link_before - Text before the link.
 * link_after - Text after the link.
 * echo - Whether to echo the menu or return it. Defaults to echo.
 * depth - how many levels of the hierarchy are to be included. 0 means all. Defaults to 0.
 * walker - allows a custom walker to be specified.
 * theme_location - the location in the theme to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
 *
 * @since 3.0.0
 *
 * @param array $args Arguments
 */
function wp_nav_menu( $args = array() ) {
static $menu_id_slugs = array();

$defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '',
'depth' => 0, 'walker' => '', 'theme_location' => '' );

$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'wp_nav_menu_args', $args );
$args = (object) $args;

// Get the nav menu based on the requested menu
$menu = wp_get_nav_menu_object( $args->menu );

// Get the nav menu based on the theme_location
if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );

// get the first menu that has items if we still can't find a menu
if ( ! $menu && !$args->theme_location ) {
$menus = wp_get_nav_menus();
foreach ( $menus as $menu_maybe ) {
if ( $menu_items = wp_get_nav_menu_items($menu_maybe->term_id) ) {
$menu = $menu_maybe;
break;
}
}
}

// If the menu exists, get its items.
if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
$menu_items = wp_get_nav_menu_items( $menu->term_id );

// If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
&& ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) )
return call_user_func( $args->fallback_cb, (array) $args );

// If no fallback function was specified and the menu doesn't exists, bail.
if ( !$menu || is_wp_error($menu) )
return false;

$nav_menu = $items = '';

$show_container = false;
if ( $args->container ) {
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
if ( in_array( $args->container, $allowed_tags ) ) {
$show_container = true;
$class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
$id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
$nav_menu .= '<'. $args->container . $id . $class . ">\n"; // Here my custom!
}
}

// Set up the $menu_item variables
_wp_menu_item_classes_by_context( $menu_items );

$sorted_menu_items = array();
foreach ( (array) $menu_items as $key => $menu_item )
$sorted_menu_items[$menu_item->menu_order] = $menu_item;

unset($menu_items);

$items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
unset($sorted_menu_items);

// Attributes
if ( ! empty( $args->menu_id ) ) {
$slug = $args->menu_id;
} else {
$slug = 'menu-' . $menu->slug;
while ( in_array( $slug, $menu_id_slugs ) ) {
if ( preg_match( '#-(\d+)$#', $slug, $matches ) )
$slug = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $slug);
else
$slug = $slug . '-1';
}
}
$menu_id_slugs[] = $slug;
$attributes = ' id="' . $slug . '"';
$attributes .= $args->menu_class ? ' class="'. $args->menu_class .'"' : '';

$nav_menu .= "\t\t\t\t<ul". $attributes .">\n"; // Here my custom!

// Allow plugins to hook into the menu to add their own <li>'s
$items = apply_filters( 'wp_nav_menu_items', $items, $args );
$items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
$nav_menu .= $items;
unset($items);

$nav_menu .= "\t\t\t\t</ul>\n"; // Here my custom!

if ( $show_container )
$nav_menu .= "\t\t\t</" . $args->container . ">\n"; // Here my custom!

$nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );

if ( $args->echo )
echo $nav_menu;
else
return $nav_menu;
}

※「\t」でタブを追加し、「\n」を追加してソースが改行されるようにしました。(※タブの個数に関しては、メニューの挿入位置によって異なると思いますので、「\t」の個数を変更して調整してください。)

※変更箇所には「// Here my custom!」とコメントを記述しています。

※バージョンアップの時にも上記の点を修正すれば、問題なく動作すると思います。


http://wpdocs.sourceforge.jp/%E3%83%86%E3%83%B3%E3%83%97%E3%83%A...

◎質問者からの返答

出来ました!!

コピペで試してみた後、アドバイスのとおり「/t」を調整して自分の好みのHTMLを生成することが出来るようになりました。

私一人では永遠にたどり着けない境地でした。

本当に有難うございました。

関連質問


●質問をもっと探す●



0.人力検索はてなトップ
8.このページを友達に紹介
9.このページの先頭へ
対応機種一覧
お問い合わせ
ヘルプ/お知らせ
ログイン
無料ユーザー登録
はてなトップ