10

I was trying to place a WordPress menu inside my template file. I found using wp_nav_menu uncomfortable since it outputs markup that is not compatible with my static HTML template. I know I could just use custom menu walker class to modify the markup that wp_nav_menu. But instead, I wanted to just use only the menu name and its corresponding url and place it in my template. For this, I used this function to get an array of all the attributes/properties of the menu.

Here is the code that was generated by this function...

I found it by using var_dump().
    Array
(
    [0] = WP_Post Object
        (
            [ID] = 20
            [post_author] = 1
            [post_date] = 2017-07-10 11:50:32
            [post_date_gmt] = 2017-07-10 10:50:32
            [post_content] = 
            [post_title] = About
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = about
            [to_ping] = 
            [pinged] = 
            [post_modified] = 2017-07-10 11:50:40
            [post_modified_gmt] = 2017-07-10 10:50:40
            [post_content_filtered] = 
            [post_parent] = 0
            [guid] = http://localhost/mySite/?p=20
            [menu_order] = 1
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 20
            [menu_item_parent] = 0
            [object_id] = 20
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = About
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

    [1] = WP_Post Object
        (
            [ID] = 21
            [post_author] = 1
            [post_date] = 2017-07-10 11:50:32
            [post_date_gmt] = 2017-07-10 10:50:32
            [post_content] = 
            [post_title] = Things I Can Do
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = things-i-can-do
            [to_ping] = 
            [pinged] = 
            [post_modified] = 2017-07-10 11:50:40
            [post_modified_gmt] = 2017-07-10 10:50:40
            [post_content_filtered] = 
            [post_parent] = 0
            [guid] = http://localhost/mySite/?p=21
            [menu_order] = 2
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 21
            [menu_item_parent] = 0
            [object_id] = 21
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = Things I Can Do
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

    [2] = WP_Post Object
        (
            [ID] = 22
            [post_author] = 1
            [post_date] = 2017-07-10 11:50:32
            [post_date_gmt] = 2017-07-10 10:50:32
            [post_content] = 
            [post_title] = A Few Accomplishments
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = a-few-accomplishments
            [to_ping] = 
            [pinged] = 
            [post_modified] = 2017-07-10 11:50:40
            [post_modified_gmt] = 2017-07-10 10:50:40
            [post_content_filtered] = 
            [post_parent] = 0
            [guid] = http://localhost/mySite/?p=22
            [menu_order] = 3
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 22
            [menu_item_parent] = 0
            [object_id] = 22
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = A Few Accomplishments
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

    [3] = WP_Post Object
        (
            [ID] = 23
            [post_author] = 1
            [post_date] = 2017-07-10 11:50:32
            [post_date_gmt] = 2017-07-10 10:50:32
            [post_content] = 
            [post_title] = Contact
            [post_excerpt] = 
            [post_status] = publish
            [comment_status] = closed
            [ping_status] = closed
            [post_password] = 
            [post_name] = contact
            [to_ping] = 
            [pinged] = 
            [post_modified] = 2017-07-10 11:50:40
            [post_modified_gmt] = 2017-07-10 10:50:40
            [post_content_filtered] = 
            [post_parent] = 0
            [guid] = http://localhost/mySite/?p=23
            [menu_order] = 4
            [post_type] = nav_menu_item
            [post_mime_type] = 
            [comment_count] = 0
            [filter] = raw
            [db_id] = 23
            [menu_item_parent] = 0
            [object_id] = 23
            [object] = custom
            [type] = custom
            [type_label] = Custom Link
            [title] = Contact
            [url] = #
            [target] = 
            [attr_title] = 
            [description] = 
            [classes] = Array
                (
                    [0] = 
                )

            [xfn] = 
        )

)

My intention is to get the value of 'post_title' and 'url'

How can I do that?

I am currently struggling with these lines of code

function mt_get_menu_items($menu_name) {
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
    $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    return wp_get_nav_menu_items($menu->term_id);
}
}
$menu_items = mt_get_menu_items ('main_menu');
<nav id="nav">
<ul>
    <?php if (isset ($menu_items) ) : ?>
    <?php foreach ( (array) $menu_items as $key => $menu_item ) : ?>
    <?php // $menu_item_array = get_object_vars($menu_item[$key]);
                    $menu_object = $menu_item[$key];
                    // this line is 98
                 ?>
    <li>
         // this line is 101
        <a href="<?php $menu_object->url; ?>" class="active"><?php $menu_object->post_title; ?></a>
    </li>
    <?php endforeach; ?>
    <?php endif; ?>
</ul>
</nav>

This is generating this error:

Fatal error: Uncaught Error: Cannot use object of type WP_Post as array in D:\xampp\htdocs\mySite\wp-content\themes\mySite\header.php:98

intcreator
  • 4,206
  • 4
  • 21
  • 39
SHAFI ul BARI
  • 181
  • 1
  • 2
  • 8
  • Possible duplicate of ['Cannot use object of type stdClass as array' using Wordpress](https://stackoverflow.com/questions/6171699/cannot-use-object-of-type-stdclass-as-array-using-wordpress) – aynber Jul 10 '17 at 17:42
  • 1
    It's an object. Use `->` instead of `[]`. – aynber Jul 10 '17 at 17:42
  • Also, it's probably not coming from post_title and url. Which line is 98? – aynber Jul 10 '17 at 17:43
  • With due respect, please read my code first. I am using -> when retrieving data out of object. But it just keeps giving me this error. – SHAFI ul BARI Jul 10 '17 at 17:46
  • $menu_object = $menu_item[$key]; This line is line number 98 of header.php in my editor – SHAFI ul BARI Jul 10 '17 at 17:49
  • Then `$menu_item` is an object. Use `$menu_item->$key` instead. – aynber Jul 10 '17 at 17:50
  • using $menu_item->$key gives off this message Notice: Trying to get property of non-object in D:\xampp\htdocs\mySite\wp-content\themes\mySite\header.php on line 101 And this line is line number 101 post_title; ?> – SHAFI ul BARI Jul 10 '17 at 17:54
  • What is `$menu_object` when you dump that, after using menu_item as an object? – aynber Jul 10 '17 at 17:57
  • @aynber after var_dump($menu_object)........ (array(8) { ["_menu_item_type"]=> array(1) { [0]=> string(6) "custom" } ["_menu_item_menu_item_parent"]=> array(1) { [0]=> string(1) "0" } ["_menu_item_object_id"]=> array(1) { [0]=> string(2) "20" } ["_menu_item_object"]=> array(1) { [0]=> string(6) "custom" } ["_menu_item_target"]=> array(1) { [0]=> string(0) "" } ["_menu_item_classes"]=> array(1) { [0]=> string(17) "a:1:{i:0;s:0:"";}" } ["_menu_item_xfn"]=> array(1) { [0]=> string(0) "" } ["_menu_item_url"]=> array(1) { [0]=> string(1) "#" } }) – SHAFI ul BARI Jul 10 '17 at 18:03
  • That does not look like what you're looking for. Was your first dump from `$menu_items`? If so, skip line 98, and use `$menu_item` for line 101 instead of `$menu_object`. – aynber Jul 10 '17 at 18:11
  • If I skip skipping line 98 and var_dump($menu_item) then it gives me an object like this .....|| object(WP_Post)#2934 (37) { ["ID"]=> int(20) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2017-07-10 11:50:32" ["post_date_gmt"]=> string(19) "2017-07-10 10:50:32" ["post_content"]=> string(0) "" ["post_title"]=> string(5) "About" ["post_excerpt"]=> string(0) "" .....................................to be continued.............. – SHAFI ul BARI Jul 10 '17 at 18:28
  • That's the object you want to get the post_title and url from, then. `$menu_item->post_title` and `$menu_item->url` – aynber Jul 10 '17 at 18:31
  • 1
    [CODE THAT SOLVED MY ISSUE] remove $menu_object = $menu_item[$key]; at line 98 altogether. Then at line 101.... use echo $menu_item->url; to echo out the menu url use echo $menu_item->post_title; to echo out the menu title – SHAFI ul BARI Jul 10 '17 at 19:08

2 Answers2

21

I'm going to post this answer here for reference...

Using a foreach loop

foreach ($originalArray as $key => $data) {
  $post_date = $data->post_date; // 2017-07-10 11:50:32
}

Because to access data inside an object, use ->.

Ex: $data['post_date'] to $data->post_date

JM-AGMS
  • 1,670
  • 1
  • 15
  • 33
2

You're retrieving an object, you can convert it to an array as follows:

function object_to_array($obj) {
if(is_object($obj)) $obj = (array) $obj;
    if(is_array($obj)) {
        $new = array();
        foreach($obj as $key => $val) {
            $new[$key] = object_to_array($val);
        }
    }
    else $new = $obj;
    return $new;       
}
Difster
  • 3,264
  • 2
  • 22
  • 32