I have a challenge and will appreciate any assistance please.
I'm working with the wordpress sydney theme. I want to introduce an active(or current - if you like) effect to change the color of the menu of the current page.I have created a child theme, and have the necessary files in that folder but can't figure out where to effect the modification.I tried creating a new header file in the child theme to structure this but my website got crashed and i had to undo that. Kindly assist..
Using wp_get_nav_menu_items() you can apply active menu item effect.
<?php
$menu = wp_get_nav_menu_items('menu_name');
$id = get_the_ID();
foreach($menu as $menuitem)
{
if($id == $menuitem->object_id)
{
echo '<li class = "nav-active">'; //apply active class here
echo ''.$menuitem->title.'';
echo '</li>';
}
else
{
echo '<li>';
echo ''.$menuitem->title.'';
echo '</li>';
}
}
?>
Related
This is a continuation in a way of this question:
Wordpress Navigation Label in Browser tab
but I am afraid I need more guidance then the previous poster.
I too want to use the navigation label in the WordPress menu, to display the parent navigation label (and not the parent title!) before the page's actual title.
The code that works for the parent title is this:
<h1 class="page-title"><strong><?php
// If child page , also include the parent title:
if($post->post_parent)
{$parent_title = get_the_title($post->post_parent);
echo '<span>'.$parent_title.' — </span><br />';
}
the_title(); ?></strong></h1>
I would like to swap out .$parent_title. for the parent-navigation-label. Is there a way to echo that label?
Disclaimer: I am not a programmer by choice - only when I have to, so please be gentle...
Edit: This is the navigation label I am talking about btw:
The label is stored in the post object as title:
$parent = get_post( $post->post_parent );
echo '<span>' . $parent->title . ' — </span><br />';
(Ok - If there is a way to include formatted code in the comments - please let me know!)
Building on #diggys answer I added this:
<h1 class="page-title"><strong><?php
// Display the parent nav label:
$parent = get_post( $post->post_parent );
echo '<span>' . $parent->title . ' — </span><br />';
the_title(); ?>
</strong></h1>
but the output is empty - apart from the the — character (-). I must be missing something obvious, right?
I'm creating a wordpress theme with parallax features and html5, the home has all the main pages and two different kind of navigations.
Main one, is the navigation for parent pages. So I have About, Projects and Contact.
But each one of this pages has child pages. About page has one child that is below it, but as i cannot access from the main menu, each page that has childs or subpages has the second type of menu.
Also projects, has different pages: "Projects" (main), "Design" (child), Consulting (child), etc.
The second menu that is placed on the right of the page as rounded buttons lists the parent "about" and the child "about 2".
I already figured it out a way to list those and to make the links behave with scroll to (parallax behavior)
But, the way that i have hardcodes the parent page. What i would like to do is to find a way to get dinamically the page ID so it won't be hardcoded, but as the theme works as a only one page site, it's making me get troubles.
Here is the code that i have, could anyone help me to find a way so the code is going to identify the ID of each current page to list the main and the children pages ?
As you see $parent = 13; is hardcoded, and also it includes $pages = get_pages('hierarchical=0&include=13') ; with the id hardcoded too. So all the pages are showing the same. I know i can create a conditional with is_page for each hardcoding it, but the idea is to make it dynamic.
Any help will be great!
THanks,
<?php
$parent = 13;
$args= array(
'parent' => $parent,
'hierarchical' => 0
);
$pages = get_pages('hierarchical=0&include=13') ;
foreach ( $pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li>' . $page->post_title . '</li>';
}
$child_page_pages = get_pages($args);
foreach ( $child_page_pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li>' . $page->post_title . '</li>';
}
?>
To get post parent page ID's you can use get_post_ancestors( $post ) it should return the parent ID.
$ReturnParents = get_post_ancestors($post->ID);
Another method you can try is the global $post.
global $post;
global $wp_query;
$thePostID = $wp_query->post->ID;
$myParent = $post->post_parent;
echo "I am post ". $thePostID . " and my parent is " . $myParent;
all. This question probably has a devilishly simple answer but it has kept me occupied for several hours.
I have my main menu and it's corresponding block in a Drupal site I am building. Like all other Drupal menus it contains a bunch of links to various parts of the site. I can assign it's block to a region and the menu links come out all nice and formatted with a title thing and little bullet points. The problem though is that I am making a custom theme for this website and I need to be able to work with the links without all the cruft added, preferably in something simple like an ul.
Is there any function that takes a menu and produces an ul containing all the links?
Maybe there is some way you can reduce the menu's block to just an ul.
I have been experimenting with theme_menu_tree(...) and theme(...) to no avail.
Thank you!
I find you can do most changes through CSS such as setting <H2> titles to display: none and setting <LI> tags to float: left for a horizontal navbar.
But... if you want to build your own menu from the Drupal data, here is some code from a site I'm working on. It builds a two-level menu. I'm sure you could simplify this code even further if you need.
//----------- primary menu (horizontal with drop-downs) -------------------------
$params = array('max_depth' => 3);
$menu = menu_build_tree('main-menu', $params);
$variables['menu'] = $menu;
$html = '<ul>';
foreach($menu as $item_menu) { //for each main element
$isSecondLevel = isset($item_menu['below']) && !empty($item_menu['below']);
if ($isSecondLevel) {
$html.= '<li>';
} else {
$html.= '<li class="sg">';
}
$html.= '<a class="topLevel" href="'.url($item_menu['link']['link_path']).'">';
$html .= $item_menu['link']['link_title'];
$html .= '</a>';
//is there any sub elements to display
if ($isSecondLevel) {
$html.= '<ul>';
foreach($item_menu['below'] as $item_submenu) { //for each sub element
$isThirdLevel=isset($item_submenu['below']) && ! empty($item_submenu['below']) ? 'main-menu-third_level' : '';
$html.= '<li>';
$html.= '<a href="'.url($item_submenu['link']['link_path']).'">';
$html.= $item_submenu['link']['link_title'];
$html.= '</a>';
$html.= '</li>';
}
$html.= '</ul>';
}
$html.= '</li>';
}
$html.= '</ul>';
$variables['main_menu_html'] = $html;
This code was placed inside function pinkribbon_process_page(&$variables) in template.php. Menu is printed in the template by calling <?php echo $main_menu_html ?>
Simon.
P.S. Others, please feel free to edit this code for clarity/simplicity.
I advice you to use
menu_tree_output
like this:
print render(menu_tree_output(menu_build_tree('main-menu', $parameters)));
You could call menu_build_tree and look at it's output and build a ul from it. However, despite the default menu output having loads of "cruft" it is a ul and should be themeable with CSS.
If you really want to build the menu yourself, I would reverse engineer another module that does so like Nice Menus
I was like trying everything but i cant solve my problem.
I created a single-mainpage-news.php, it show the sidebar but doesnt show the links.
<?php
$has_subpages = false;
$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
$has_subpages = true;
}
$children = "";
if(is_category() && $post->post_parent) {
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
} else if($has_subpages) {
$children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
}
?>
<?php if ($children) { ?>
<?php echo $children; ?>
<?php } ?>
and it uses a main page category
could please someone help?
Could just be that your page is neither a parent page ($has_subpages) nor a category archive (is_category()) or a children ($post->post_parent)?
Note that having an AND in your conditional implies that to be true the page has to be at the same time a category archive AND a children page. Do you really have this type of page? Do you perhaps just want to check for a children page?
Also, note that a page can be a parent and children at the same time.
How in the WORLD is possible? I swear, I've read the equivalent of 3 encyclopedias to no avail. I've tried solutions within regions, page.tpl.php and blocks. None of them give me what I need... and I know there are so many other people that need this too!
I've come to the conclusion that I want to print out the menu within my page.tpl.php ... so no block solutions, please.
I want to be able to loop through the primary menu links (AND children) and rewrite the output so that there's no default Drupal class tagging. The closest I've found is this example:
<?php if (is_array($primary_links)) : ?>
<ul id="sliding-navigation">
<?php foreach ($primary_links as $link): ?>
<li class="sliding-element"><?php
$href = $link['href'] == "<front>" ? base_path() : base_path() . drupal_get_path_alias($link['href']);
print "<a href='" . $href . "'>" . $link['title'] . "</a>";
?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
As you can see, links are being reprinted with a custom UL and LI class ... that's GREAT! However, no children are being printed. How would I extend this code so that all children are a part of the list? NOTE: I don't want the children to only appear on their parent page, they must be present all the time. Otherwise, the drop-down menu I have planned is useless.
I sincerely thank you in advance to lessening my gargantuan headache!
It's hard to affect the output once it's got as far as the page.tpl - you might do better looking for template.php functions.
This is one I used to alter the classes of my primary links:
function primary_links_add_icons() {
$links = menu_primary_links();
$level_tmp = explode('-', key($links));
$level = $level_tmp[0];
$output = "<ul class=\"links-$level\">\n";
if ($links) {
foreach ($links as $link) {
$link = l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
$output .= '<li class="sublevel">' . $link .'</li>';
};
$output .= '</ul>';
}
return $output;
}
And then in page.tpl.php I just called it like this:
<?php if ($primary_links) :?>
<?php print '<div id="menu">'; ?>
<?php print primary_links_add_icons(); ?>
<?php print '</div>'; ?>
<?php endif;?>
I had to add a <span> to my links for styling, so I overrode theme_links() in includes/theme.inc
You can copy the function to your template.php, rename it to yourthemename_links(), and modify it as needed.
This function outputs the ul, li tags, the drupal_attributes, classes of 'first', 'last', 'active', etc, and affects the menus throughout the site.
You may also want to check out the functions in includes/menu.inc, including theme_menu_local_tasks() and menu_local_tasks(), if you need to output the primary and secondary differently.
MarkLNH