1

Wordpressのメニューにデータを入力するために、動的な(WP以外の)コンテンツを取得しようとしています。このために、ここに示すようにウォーカークラスを拡張しました。

http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output

だから私のメニューは次のようになります

  • ブログ
  • ニュース
  • 特徴
  • 大会-
    • Comp1
    • Comp2

Comp1とComp2が別のサイトのDBからプルされる場合。これらは外部サイトへのリンクにすぎないため、関連する値は「CompTitle」と「CompURL」のみです。

私のクラス内の主なメソッドは次のとおりです。

 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 . '<li id="menu-item-'. $item->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        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

        $prepend = '<strong>';
        $append = '</strong>';
        $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

        if($depth != 0)
        {
            $description = $append = $prepend = "";
        }

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

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

            $this->loopComps($output , $args);

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

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

そして、loopCompsメソッドは次のようになります:(getGalleryComps()は動的コンテンツをプルします)

 function loopComps($output , $args)
    {
        $openComps = $this->getGalleryComps();
        foreach($openComps as $comp)
        {
            $item =  new StdClass;
            $item->ID = 9999;
            $item->post_author = 5;
            $item->post_date = '2012-11-16 10:48:44';
            $item->post_date_gmt = '2012-11-16 10:48:44';
            $item->title = $comp['competition_name'];
            $item->post_type = 'nav_menu_item';
            $item->post_name = $comp['competition_name'];
            $item->post_title = $comp['competition_name'];
            $item->post_excerpt = $comp['competition_name'];
            $item->guid = $comp['competition_name'];
            $item->url = 'http://www.mycomps';
            $item->post_status = 'publish';
            $item->post_parent = 0;
            $item->filter = 'raw';
            $item->menu_item_parent = '6845';
            $item->object_id = '99999';
            $item->object = 'custom';
            $item->type = 'custom';
            $item->classes = array(null , 'menu-item' , 'menu-item-type-custom' , 'menu-item-object-custom');
            $item->menu_order = 6;


            return $this->start_el($output, $item, 1, $args);


        }

    }

これはすべて正常に機能しているようです。$itemが実際のメニューに添付されないことを期待してください。navメニューループ中に$itemsを印刷すると、「psuedo Post」を作成した動的コンテンツがそこにあることがわかりますが、メニューに添付されることはありません。

メニュー項目を挿入する簡単な方法はありますか?

更新1:

loopCompsメソッドを次のように調整しました。

 function loopComps($output)
    {
        $openComps = $this->getGalleryComps();

        $output .= '<ul>';
        foreach( $openComps as $openComp )
        {
            $output .= '<li><a href='.$openComp->url.'>'.$openComp['competition_name]'].'</a></li>';
        }
        $output .= '</ul>';

        return $output;
  }

これははるかに理にかなっていますが、問題を解決しないため、データはそこにありますが、メニューに表示されることはありません。

4

3 に答える 3

1

あなたの質問を正しく理解できたかどうかはわかりませんが、あなたが尋ねたので、フックのようなものIs there an easier way of injecting menu items?を使用して簡単にメニュー項目を追加できると思いますwp_nav_menu_items

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
    // First get the data from your database and replace menu titles and links
    // then loop the result and add items
    $myMenu='<li><a href="#">External Links</a><ul>'; // parent
    $myMenu.='<li><a target="_blank" href="http://facebook.com">Facebook</a></li>';
    $myMenu.='<li><a target="_blank" href="http://google.com">Google</a></li></ul></li>';
    $items .= $myMenu;
    return $items;
}

にコードを貼り付けてfunctions.php、メニュー タイトルとリンクを自分のものに置き換えるだけです。

于 2013-01-04T21:21:08.830 に答える
1

競技用のメニュー項目の HTML を に追加するだけで済みます$output。たとえば、

$output .= '<ul>';
foreach( $comps as $comp ) {
    $output .= '<li><a href='.$comp->url.'>.$comp->title.'</a></li>';
}
$output .= '</ul>';

これが、 $output が参照 ('&') によって に渡される理由start_el()です。

この例は、WP で生成されたメニュー項目が従うプロトタイプに合わせて調整する必要があるため、おそらく単純化されていますが、原理を示しています。モックの WordPress アイテムを作成し、WordPress をだまして作業をさせようとするのは、正しいアプローチとは思えません。

于 2013-01-04T20:44:43.433 に答える
0

質問と回答を確認した後、「コンペティション」親メニューの下に「GOOGLE」サブメニュー項目を追加したので、メニューバーを編集するための完全なソリューションがここにあります。

  • 1

    最初にこのコードをメニューバーが呼び出される場所に配置し、header.php にする必要があります。コメントで言及したカスタムメニューを変更します

    class description_walker extends Walker_Nav_Menu
    {
      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 . '<li id="menu-item-'. $item->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        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
        $prepend = '<strong>';
        $append = '</strong>';
        $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
        if($depth != 0){
         $description = $append = $prepend = "";
        }
        if($item->title=='Competitions'){
          $item_output = $args->before;
          $item_output .= '<a'. $attributes .'>';
          $item_output .= $args->link_before .$prepend.apply_filters(  'the_title', $item->title, $item->ID ).$append;
          $item_output .= $description.$args->link_after;
          $item_output .= '</a>';
          $item_output .= $args->after;
          /* YOUR CUSTOM MENU OR SUBMENU -START- */
          $output .= '<ul class="sub-menu">';
          $output .= '<li><a href='.'http://www.google.com'.'>'.'GOOGLE'.'</a></li></ul>';
          /* YOUR CUSTOM MENU OR SUBMENU -END- */
          $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }else{
          $item_output = $args->before;
          $item_output .= '<a'. $attributes .'>';
          $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
          $item_output .= $description.$args->link_after;
          $item_output .= '</a>';
          $item_output .= $args->after;
          $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
      }
    }
    
    • 2 wp_nav_menu を呼び出し、作成したばかりのカスタム walker オブジェクトを渡します。

    wp_nav_menu( array( 'container' =>false, 'menu_class' => 'menuPrincipal', 'echo' => true, 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'depth' => 0, 'walker' => new description_walker()) );

コンペティションメニューの下にGOOGLEサブメニューを追加します

最初の答えは正しいですが、クラス メンバーの出力は loopComps で設定できませんでした。また、出力メンバーを設定するためだけに出力を返すべきではないため、直接サブ メニューをウォーカーに設定するだけです。

それは正常に動作します。

于 2015-07-11T07:32:47.987 に答える