SHAFI ul BARI
Estaba tratando de colocar un menú de WordPress dentro de mi archivo de plantilla. Me resultó incómodo usar wp_nav_menu ya que genera un marcado que no es compatible con mi plantilla HTML estática. Sé que podría usar la clase de caminante de menú personalizado para modificar el marcado que wp_nav_menu. Pero en su lugar, quería usar solo el nombre del menú y su URL correspondiente y colocarlo en mi plantilla. Para esto, utilicé esta función para obtener una matriz de todos los atributos/propiedades del menú.
Aquí está el código que fue generado por esta función…
Lo encontré usando 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 Uncaught Error: Cannot use object of type WP_Post as array = About [url] = # [target] = [attr_title] = Uncaught Error: Cannot use object of type WP_Post as array = [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 Uncaught Error: Cannot use object of type WP_Post as array = Things I Can Do [url] = # [target] = [attr_title] = Uncaught Error: Cannot use object of type WP_Post as array = [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 Uncaught Error: Cannot use object of type WP_Post as array = A Few Accomplishments [url] = # [target] = [attr_title] = Uncaught Error: Cannot use object of type WP_Post as array = [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 Uncaught Error: Cannot use object of type WP_Post as array = Contact [url] = # [target] = [attr_title] = Uncaught Error: Cannot use object of type WP_Post as array = [classes] = Array ( [0] = ) [xfn] = ) )
Mi intención es obtener el valor de ‘post_title’ y ‘url’
¿Cómo puedo hacer eso?
Actualmente estoy luchando con estas líneas de código.
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="https://stackoverflow.com/questions/45018459/<?php $menu_object->url; ?>" class="active"><?php $menu_object->post_title; ?></a>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</nav>
Esto está generando este error:
Error fatal: error no detectado: no se puede usar el objeto de tipo WP_Post como matriz en D:\xampp\htdocs\mySite\wp-content\themes\mySite\header.php:98
Voy a publicar esta respuesta aquí como referencia …
Usando un foreach
círculo
foreach ($originalArray as $key => $data) {
$post_date = $data->post_date; // 2017-07-10 11:50:32
}
Porque para acceder a los datos dentro de un objeto, use ->
.
Ex: $data['post_date']
a $data->post_date
difster
Está recuperando un objeto, puede convertirlo en una matriz de la siguiente manera:
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;
}
Posible duplicado de ‘No se puede usar el objeto de tipo stdClass como matriz’ usando WordPress
– Aynber
10 de julio de 2017 a las 17:42
es un objeto Usar
->
en vez de[]
.– Aynber
10 de julio de 2017 a las 17:42
Además, probablemente no provenga de post_title y url. ¿Qué línea es 98?
– Aynber
10 de julio de 2017 a las 17:43
Con el debido respeto, lea primero mi código. Estoy usando -> cuando recupero datos del objeto. Pero me sigue dando este error.
– SHAFI ul BARI
10 de julio de 2017 a las 17:46
[CODE THAT SOLVED MY ISSUE] eliminar $menu_object = $menu_item[$key]; en la línea 98 en total. Luego, en la línea 101… use echo $menu_item->url; para hacer eco de la URL del menú, use echo $menu_item->post_title; para hacer eco del título del menú
– SHAFI ul BARI
10 de julio de 2017 a las 19:08