0

WordPress ブログ メニューのいくつかのリンクに PHP コードを追加したいと考えています。私が使う

Dashboard > Appearance > Menus

メニューを構成します。そこにPHPコードを追加してもうまくいかないようです。

次のようなリンクが必要です。

<a href="http://domain.com/signup.php?user=<?PHP code goes here?>&session=2">

そこに PHP コードを追加し、組み込みの WordPress メニュー生成方法を維持する方法はありますか?

4

1 に答える 1

0

そのためにカスタム ウォーカーをセットアップする必要があります。これを functions.php ファイルに追加するだけです:

class Query_Nav extends Walker_Nav_Menu
{
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        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, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

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

        $output .= $indent . '<li' . $id . $value . $class_names .'>';

        $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        ) .'"' : '';

        //ADD YOUR PHP HERE TO DETERMINE WHATEVER IT IS YOU NEED FOR YOUR LINK
        $addedStuff = 'some added stuff to append to your URL';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url.$addedStuff) .'"' : '';
        ////////////////////

        $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 );
    }
}

URL に何を追加したいのかわかりませんが、これで変更を開始できるはずです。必要に応じて URL の外観を作成したら、WP Nav Menuへの呼び出しを調整して、新しい Walker クラスを既存の引数に追加する必要があります。

wp_nav_menu(array('walker' => new Query_Nav()));

Nav Walkers に関するその他のドキュメント:

https://wordpress.stackexchange.com/questions/14037/menu-items-description-custom-walker-for-wp-nav-menu/14039#14039
http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output

于 2012-10-04T15:59:26.700 に答える