1

Trying many nested loops and methods to build this JavaScript block, I need experts help. I iterate through all 'menuitems' and the ones with menu_item_parent 0 are top parent, the rest I build lower levels.

How can i add those two characters missing onto these loops?

It's missing a comma

,

after each

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

Function

    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>';

}

Current Output

'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': ''}
    ]}]}

Desired Output

'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
    Your desired output is not a js object. You should make the object in php then use json_encode to convert it to a js object. Commented Aug 6, 2013 at 1:21
  • 2
    Manually constructing a JSON string is a bad idea. It's slow, and it's prone to error. Construct your object as a PHP array, then use 'json_encode()' to produce your JSON object. Your code is too complex to recode it as an answer. Have a go, and come back with a question if you get stuk. Commented Aug 6, 2013 at 1:29
  • @MikeW I got it to work almost. I'm missing a comma now within sections array elements. Updating Question now... Commented Aug 6, 2013 at 5:00
  • @Musa made some changes. still need one comma. any ideas? Commented Aug 6, 2013 at 5:12

2 Answers 2

1

As the comments to your question states, you should make the object in PHP and then use json_encode.

Also as mentioned in the comments, it is too complex code to rewrite it as an answer.

But to point you in the right direction, some pseudo code to show how to build the PHP object:

function make_menu_object() {
    $itemsForJS = new array(); // Initialize an empty array to hold the menu items

    // Code to retrieve the menu items
    // ...

    $currTopLevelNode = null; // Because output from WP is flat list, state is needed

    foreach ( $menuitems as $item ) {
        // Code to retrieve data about the menu item
        // ...

        if ( $item->menu_item_parent==0 ) {
            $currTopLevelNode = new stdClass; // Initialize an empty object 
            $currTopLevelNode->url = ''; // Or something more interesting... 
            $currTopLevelNode->sections = array(); // Empty holder for the sections to come
            $itemsForJS[$item->title] = $currTopLevelNode; 
        } else {
            $section = new stdClass; 
            $section->name = ''; // Or something more interesting
            $section->url = ''; // Or maybe $item->url? 
            $section->subs = array(); // Empty array to hold data for the subs

            // Code to retrieve post
            // ... 

            foreach ($mypost as $post) {
                $sub = new stdClass; // Empty object to hold data for the page

                // Code to retrieve data about the post
                // ...

                $sub->id = '1'; // Or maybe $post->post_id? 
                $sub->title = ''; // Maybe $post->post_title? 
                $sub->url = ''; // Maybe $post->post_url? 

                $section->subs[] = $sub; // Adding the post to the section
            }

            $currTopLevelNode->sections[] = $section; // Adding the section to the item
        }
    }
    return $itemsForJS; // This is an associative array, not string as in your code
}

Use this function similar to:

$menuObject = make_menu_object(); 
echo '<pre>'.json_encode($menuObject).'</pre>'; 

Of course, this is pseudo code. For example, it assumes that the first $item received must be a top level one - or $currTopLevelNode will be null and the code will crash. Error checks needed.

Sign up to request clarification or add additional context in comments.

Comments

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

add this after the second if

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.