1

この JavaScript ブロックを作成するためにネストされたループとメソッドを多数試しているので、専門家の助けが必要です。すべての「メニューアイテム」を反復処理し、menu_item_parent 0 を持つものは最上位の親であり、残りは下位レベルを構築します。

これらのループに欠落している 2 つの文字を追加するにはどうすればよいですか?

コンマが足りない

,

それぞれの後

{'name' : '','url':'','subs':[
{'_id:':'1','title': '','img': ''}
,{'_id:':'1','title': '','img': ''}
]}

関数

    function make_menu_object(){
    $menu_name = 'sec_level';
    $menu = wp_get_nav_menu_object( $menu_name );
    $menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );
    $objpart=null;

    $menuitems_count = count($menuitems);
    $master_counter = 0;

    foreach( $menuitems as $item ){

        // get page id from using menu item object id
        $id = get_post_meta( $item->ID, '_menu_item_object_id', true );
        // set up a page object to retrieve page data
        $page = get_page( $id );
        $link = get_page_link( $id );
        // item does not have a parent so menu_item_parent equals 0 (false)    

        if ( $item->menu_item_parent==0 ){
            if($master_counter<>0){$objpart.=']},';}
            $objpart .= <<<EOT

'$item->title' : {
'url':'',
'sections': [

EOT;
        }else{

        $counter=1;
        $the_star=1;



        $objpart .= <<<EOT
{'name' : '$item->title','url':'','subs':   [

EOT;
            $args = array( 'numberposts' => 4, 'offset'=> 0, 'post_type' => 'post', 'category' => $item->object_id);
            $myposts = get_posts( $args );

            $the_count = count($myposts);
            foreach( $myposts as $post ) {//subs output into js object
                setup_postdata($post);
                $the_empty_thumbnail = '/wp-content/uploads/2013/03/holder_img_menu.jpg';
                $the_thumbnail = get_the_post_thumbnail($post->ID, 'thumbnail');
                //$the_thumbnail_img = (!empty($the_thumbnail) ? the_post_thumbnail() : $the_empty_thumbnail);
                $the_post_title = addslashes($post->post_title);
                $objpart .= <<<EOT
{'_id:':'1','title': '','img': ''}

EOT;
                if($counter<$the_count){$objpart .= ',';}
                    $counter++;
                }


            if($the_star==1||$the_star==$counter){$objpart .=']}';}else{$objpart .=',';}
                $the_star++;
        }

        $master_counter++;
    }
$objpart .=']}';

return '<pre>'.$objpart.'</pre>';

}

電流出力

'Item 1' : {'url':'','sections': [
    {'name' : '','url':'','subs':[
      {'_id:':'1','title': '','img': ''}
      ,{'_id:':'1','title': '','img': ''}
      ]}

    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ]}

  ]},

    'Item 2' : {
    'url':'',
    'sections': [
    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ]}]}

望ましい出力

'Item 1' : {'url':'','sections': [
    {'name' : '','url':'','subs':[
      {'_id:':'1','title': '','img': ''}
      ,{'_id:':'1','title': '','img': ''}
      ]}
    ,
    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ]}

  ]},

    'Item 2' : {
    'url':'',
    'sections': [
    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ]}]}
4

2 に答える 2

0
if ( $item->menu_item_parent==0 ){
    $objpart .= "]}";
}

これを 2 番目の後に追加する

于 2013-08-06T01:52:59.170 に答える