I need the parent element's title as a variable and also the submenu of it.
Currently I have:
function navigation_footer() {
$menu_name = 'footer_nav';
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$type = get_post_meta( $item->menu_item_parent, '_menu_item_type', true );
$title_parent = get_post( $object_id )->post_title;
$menu_list .= "\t\t\t\t". '<div class="fl-links"><h2>'. $title_parent .'</h2>' ."\n";
$menu_list .= "\t\t\t\t\t". '<p>'. $title .'</p></div>' ."\n";
}
}
echo $menu_list;
}
The variable type gets me the title of a post which was kinda workaround.
Still, it's in a foreach so I'm getting a mess instead of what I actually want.
As I'm a fairly beginner in PHP I am stuck.
Each element in WP is going to be an individual link. The parent is only being used as a placeholder for the title followed by the links.
[Current output](https://i.stack.imgur.com/xsGRS.png)
[My desired output](https://i.stack.imgur.com/SP14p.png)
[WP Menu Parent/Child](https://i.stack.imgur.com/Z5d8K.png)
Related
I am trying to create a list of all links in primary navigation on my WordPress site. I am trying to output the link as URL: in a script tag using a foreach loop.
I am stuck trying to list of of the navitems links using $navItem->url in my foreach loop. It doesn't return anything / false.
Instead of $locations[$menuLocations] I have tried simply just write primary menu name as well: $locations['primary'] but no luck so far.
function get_nav_menu_items_by_location( $location, $args = [] ) {
$html = '<script type="application/ld+json" alt="hejtest">';
// Get all locations
$menuLocations = get_nav_menu_locations();
// Get object id by location
$object = wp_get_nav_menu_object( $locations[$menuLocations] );
// Get menu items by menu name
$menu_items = wp_get_nav_menu_items($object);
foreach ( $menu_items as $navItem ) {
$html .= '"url":"' . $navItem->url . '"';
}
$html .= "</script>";
echo $html;
}
add_action('wp_head', 'get_nav_menu_items_by_location');
Well... I did a refactor of your code and it's works ;)
Try with this way:
function get_nav_items() {
$menu_slug_to_retrieve = 'main'; // This can be main, primary, what ever...
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_slug_to_retrieve ] );
$menu_items = wp_get_nav_menu_items( $menu->term_id );
$menu_items_json = array(); // Prepare the array to convert to json
// Loop it
if ( $menu_items ) {
foreach ( $menu_items as $item ) {
$menu_items_json[] = array( 'url' => $item->url );
}
$html = sprintf(
'<script type="application/ld+json" id="custom-json">%s</script>',
json_encode( $menu_items_json )
);
echo $html;
}
}
add_action( 'wp_head', 'get_nav_items' );
Regards!
I would like to know how to retrieve as variable the name of the current page.
For exemple, My menu is like :
Item 1
Item 2 => sub-item 1
Item 1 & 2 are custom links. sub-item1 is my page.
When I'm on this page, I want to retrieve "Item 2" name (to build my custom breadcumbs)
Thanks
BREADCRUMBS:
Here is a breadcrumbs function that work with the hierarchy of your menu. Add this to your theme's 'functions.php'.
function my_menu_breadcrumb($theme_location, $separator = ' > ') {
$locations = get_nav_menu_locations();
if ( isset( $locations[ $theme_location ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
_wp_menu_item_classes_by_context( $menu_items );
$breadcrumbs = array();
foreach ( $menu_items as $menu_item ) {
if ($menu_item->current) {
$breadcrumbs[] = "<span title=\"{$menu_item->title}\">{$menu_item->title}</span>";
}
else if ($menu_item->current_item_ancestor) {
$breadcrumbs[] = "{$menu_item->title}";
}
}
echo implode($separator, $breadcrumbs);
}
}
You can then call as <?php my_menu_breadcrumb('header-menu'); ?> where 'header-menu' is the menu location name. In the loop, $menu_item->title will return page title and $menu_item->url it's URL.
PARENT MENU TITLE:
Here is the function to get parent menu item title(s) of current page - add this to your theme's 'functions.php'.
function my_menu_parent($theme_location) {
$locations = get_nav_menu_locations();
if ( isset( $locations[ $theme_location ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
_wp_menu_item_classes_by_context( $menu_items );
$breadcrumbs = array();
foreach ( $menu_items as $menu_item ) {
if ($menu_item->current_item_ancestor) {
$breadcrumbs[] = $menu_item->title;
}
}
return $breadcrumbs;
}
}
You can then call as below where 'header-menu' is the menu location name.
$parentitems = my_menu_parent( 'header-menu' );
foreach ( $parentitems as $parentitem ) {
echo $parentitem."<br>";
}
You can user this
$pagename = get_query_var('pagename');
if ( !$pagename && $id > 0 ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = $wp_query->get_queried_object();
$pagename = $post->post_name;
}
There are plenty of tutorials and code snippets online describing how to show the active page’s children (or if on one of those children, the active page’s siblings) as a sub-menu in WordPress. Usually something like this:
<?php
$ref_post = empty($post->post_parent) ? $post->ID : $post->post_parent;
$children = wp_list_pages('title_li=&child_of='.$ref_post.'&echo=0');
if ($children) {
echo "<ul>$children</ul>";
}
?>
for ref:-https://www.minddevelopmentanddesign.com/blog/showing-current-pages-parents-sub-menu-items-custom-nav-menu-wordpress/
I want to create a drop down menu on first it shows main categories then all child categories with all of their sub child list like the one shown in below pic desired drop down menu
I am using a readymade hiearchical database. my databse structure is like my database structure
here is my false approach categoriesTest.php
<?php
include 'common.php';
$query = "SELECT cat_id, parent_id, cat_name FROM " .$DBPrefix. "categories ORDER BY cat_name";
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$items = mysql_fetch_assoc($res);
$html = '';
$parent = 0;
$parent_stack = array();
// $items contains the results of the SQL query
$children = array();
foreach ( $items as $item )
$children[$items['parent_id']][] = $item;
while ( $option = each( $children[$parent] ) )
{
if ( !empty( $option ) )
{
// 1) The item contains children:
// store current parent in the stack, and update current parent
if ( !empty( $children[$option['value']['id']] ) )
{
$html .= '<li>' . $option['value']['title'] . '</li>';
$html .= '<ul>';
array_push( $parent_stack, $parent );
$parent = $option['value']['id'];
}
// 2) The item does not contain children
else
$html .= '<li>' . $option['value']['title'] . '</li>';
}
// 3) Current parent has no more children:
// jump back to the previous menu level
else
{
$html .= '</ul>';
$parent = array_pop( $parent_stack );
}
}
// At this point, the HTML is already built
echo $html;
?>
the above code does not do the job.
I am quiet newbie and cant figure out.
tried all stacked questions but no success
can anyone suggest me the correct php code?
I want have the structure of posts on wordpress site enter link description here
The top row name (slug) posts:
<?php
$tags = get_categories('orderby=name&taxonomy=references&order=ASC');
$output = '<table><tr><td> </td>';
foreach ( $tags as $tag ) {
$term = get_term_by('id', (int)$tag->term_id, 'references');
$output .= '<td>' . $tag->name . '</td>';
}
echo $output . '</tr>';
?>
Then the left column same name posts:
<?php
$tags2 = get_categories('orderby=name&taxonomy=references&order=ASC');
$output = '';
foreach ( $tags2 as $tag ) {
$term = get_term_by('id', (int)$tag->term_id, 'references');
$output .= '<tr><td>' . $tag->name . '</td><td>THIS PLACE -PROBLEM </td></tr>';
}
echo $output . '</table>';
?>
I have custom taxonomy "references". Terms in this taxonomy have same name as the name (slug) of post. Even posts link to several term in taxonomy and even term in taxonomy belongs to several posts.
At intersections of of rows and columns I need to get the value (true or false): if the post of the column to the left refers to the post of the top line put the true, if not, false.
I think it could be resolved if you creat two-dimensional matrix.
For example
$tags = get_categories('orderby=name&taxonomy=references&order=ASC');
$tags2 = get_categories('orderby=name&taxonomy=references&order=ASC');
Then
foreach($tags as $v1){
foreach($tags2 as $v2){
if( $v1 == $v2 /* some action I haven`t understand what you wrote*/){
/*output code */
}
}
}
Or I have mistake ?
I'm trying to get custom field info from a page into the nav menu. I've had problems with this before... I just don't "get" the walker menu and how it works.
Basically, In addition to the title of the page, I want to have it output the URL of an image and image description from the custom fields and create a menu item linked to a normal WP page.
In the nav-menu-template.php file, I've tried modifying the start_el function by adding get_post_custom_keys() like this with no success:
$item_output .= '<a'. $attributes .'>';
$item_output .= get_post_custom_values("product_image", $item->ID);
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
I've also tried get_post_meta(); and others with limited success. The best I've been able to do is either get one of the images repeating in all the links by specifying a a hard integer value. Or, I've been able to get it to output the correct post/page value in text but without an image.
Anyone know of a solution.. what I'm doing wrong?
It would probably be easier to iterate through your nav menu and render it dynamically. The below code will let you iterate through a given nav menu assigned to a nav menu location you would have registered in your functions.php file:
<ul id="some-menu-id" class="my-fancy-menu">
<?php
$menu_name = 'primary';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
foreach ( (array) $menu_items as $key => $menu_item ) {
// at this point you can get the custom meta from the page
$image = get_post_meta($menu_item->object_id, '_custom_field_image_url', true);
$image_description = get_post_meta($menu_item->object_id, '_custom_field_image_description', true);
// here we are getting the title and URL to the page
$title = $menu_item->title;
$url = $menu_item->url;
$slug = basename($menu_item->url);
// this allows us to add a current class
if (basename($_SERVER['REQUEST_URI']) == $slug) { $current = ' current-menu-item'; } else { $current = ''; }
$menu_list .= '<li class="page-id-'.$menu_item->object_id.$current.'">' . $title . '<br /><p>'.$image_description.'<br />'.$image.'</p></li>';
}
}
echo $menu_list;
?>
</ul>