Trying to have different navigation menus on different pages. My theme doesn't let me edit the functions page. I created a new header template and page template. I want to change the menu used in the new header template, but I don't understand what the code below means so I'm not exactly sure what to change.
I'm trying to learn so could someone help translate what this code means?
<ul id="menu-hosting-menu" class="curved">
<?php if ( is_page() ) $highlight = 'page_item'; else $highlight = 'page_item
current_page_item'; ?>
<li class="<?php echo $highlight; ?> menu-item menu-item-type-custom menu-item-object-custom">Home</li>
<?php wp_list_pages( 'sort_column=menu_order&depth=3&title_li=&exclude=' ); ?>
</ul><!-- /#nav -->
<?php } ?>
<select>
<option value="" selected="selected">Select</option>
<?php
$menu_name = 'primary-menu'; //same as theme_location
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);
var_dump($menu_items);
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
if($menu_item->menu_item_parent){
$title = '-- '.$title;
}
$menu_list .= '<option value="' . $url . '">' . $title .
'</option>';
}} else {
$menu_list .= '<option value="">Menu "' . $menu_name . '" not
defined.</option>';
}
echo $menu_list;
?>
</select>
</nav>
Are you trying to load the menu on an entirely new template, or just a particular page?
I imagine one (simple) approach to this would be to create both a new page template, and a new header. You would simply then tell the new page template (selectable in your Admin panel in the CMS) to call the new (custom) header as well.
A great explanation on how this can be done is found here:
http://reflectingthedesigner.com/wordpress/2011/10/24/creating-a-new-wordpress-page-template/
Hope this helps.
Figured it out. I just deleted all of that code above and replaced it with
<?php wp_nav_menu( array('menu' => 'Hosting' )); ?>
Worked like a charm.
Related
I have added project-types to my website portfolio https://littleseabear.com/portfolio but it lists all possible tags, and not the tags directly linked to the portfolio.
For example, Cast should only be film, screenplay and work in progress, while When the Boys Come Home should be Radio and Produced.
Here is my code:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_terms( 'jetpack-portfolio-type' );
if( is_archive() || is_home() || is_front_page() ){
?>
<div class="post-wrapper">
<?php
infinity_news_post_thumbnail();
}
?>
<div class="post-thumbnail">
<?php
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>, ';
}
?>
<div class="article-details <?php if( is_single() ){ echo 'single-article-details'; } ?>">
Also... is there a way to get rid of the commas? Thanks
get_terms retrieves all available tags, not only the ones associated to the current post. Instead, modify the get_terms line at the top like so:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_the_terms( get_the_ID(), $taxonomy );
This will collect terms of the $taxonomy associated to the current post.
Edit:
I missed the question about the commas. To totally get rid of those, you can just remove them from the output:
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a> ';
}
I removed the , right behind the closing </a>, so it will now only be separated by whitespaces. If you want to have a special separator only between items, you can achieve this like so:
$separator = ''; // initialize empty
foreach ( $tax_terms as $tax_term ) {
echo $separator.'<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>';
if (empty($separator)) {
// Choose a custom separator HERE:
$separator = ', ';
}
}
I was working on a site with categories as primary menu .I have a new post type and added an archive content as menu item with label 'City Guide'.Now when I on City Guide page the menu item is not highlighting.Or with code the class 'current' is not printing in the menu item class.Here is my code:
<?php
$menu_name = PRIMARY_NAVIGATION_NAME;
$menu_items = array();
if( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ){
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = $menu ? wp_get_nav_menu_items($menu->term_id) : array();
}
?>
<nav class="visible-sm visible-md visible-lg" role="navigation">
<ul id="menu-primary-navigation" class="main-navigation">
<?php if( !empty($menu_items) ): ?>
<?php
$current_category_object = null;
if( !empty($wp_query->query_vars['category_name']) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug($wp_query->query_vars['category_name']);
}
?>
<?php foreach( $menu_items as $item): ?>
<?php
$active_class = false;
if ($item->object == 'category' && !empty($current_category_object) ){
$cat_object = get_category($item->object_id);
if ($cat_object->term_id == $current_category_object->term_id || $cat_object->term_id == $current_category_object->parent) {
$active_class = true;
}
}
?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class) echo ' current'?>">
<a href="<?php echo $item->url; ?>" class="main-category"<?php if( $item->target) echo ' target="' . $item->target . '"'; ?>><?php echo $item->title ?></a>
<?php if( $item->object == 'category'): ?>
<?php
$sub_categories = get_terms( array('category'), array('child_of' => $item->object_id, 'hide_empty' => 0) );
if (count($sub_categories) <= 4) {
include(locate_template('partials/sub-menu-two-featured.php'));
} else {
include(locate_template('partials/sub-menu-one-featured.php'));
}
?>
<?php endif; ?>
<?php if ($item->title == 'City Guide'): ?>
<?php include(locate_template('partials/sub-menu-city-guide.php')); ?>
<?php endif; ?>
</li>
<?php wp_reset_query(); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</nav>
How can i implement it into it using a condition to add current for my archive page menu item in the above code .
I tried
<?php if ($item->title == 'City Guide'): ?>
but it was echoing in all instance not when the menu is active.
Please help.
One possible solution is to use get_queried_object() function for this.
Codex says
if you're on a single post, it will return the post object
if you're on a page, it will return the page object
if you're on an archive page, it will return the post type object
if you're on a category archive, it will return the category object
if you're on an author archive, it will return the author object
If you start using $qo = get_queried_object() then no need to use this code
if( !empty( $wp_query->query_vars['category_name'] ) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug(
$wp_query->query_vars['category_name']
);
}
because:
If you are in category-archive page, $qo will contain category object, and $qo->term_id and $qo->parent will be available.
If you are in CPT archive page, then $qo will contain CPT object, and properties like CPT name, query_var, label and so on will be available.
So when you have custom queried object and with the help of some conditional tags like is_tax(), is_tag(), is_category(), is_post_type_archive() you will be able to determine exactly where you are.
P.S. You must check the codex first, because there have some notes for precedence when using get_queried_object()
<?php
$active_class = false;
$this_category = get_queried_object();
$this_category->name;
if ($this_category->name == 'city-guide' ){
$active_class = true;
} ?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class ==1 && sanitize_title($item->title) =='city-guide') echo ' current'; ?>">
this worked thanks #pgk for pointing me out
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>
Does anyone have a working example of a Wordpress menu outputted manually? The one on the Codex didn't seem to work, though my menu_name slug of "Footer" can't be that complicated..? I need to add offscreen spans within external menu links for accessibility purposes. I already have CSS classes; what I need is specific markup added via the functions.php file.
The basic code I'm looking for (see list items w/ 'external' class. Hrefs changed to # for code brevity, they link offsite in real life):
<ul id="menu-footer" class="menu">
<li class="menu-item">
About Us
</li>
<li class="external menu-item">
<a title="Link opens in a new window." target="_blank" href="#">Terms & Conditions <span class="offscreen">Opens in a new window</span></a>
</li>
<li class="menu-item">
Comments Policy
</li>
<li class="external menu-item">
<a title="Link opens in a new window." target="_blank" href="#">Privacy Statement<span class="offscreen">Opens in a new window</span></a>
</li>
<li class="menu-item">
Contact Us
</li>
</ul>
A custom "walker" class?
http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output
Option 1:
Assuming you've created your menu by using Appearance -> Menus , you can try the following.
Navigate to Appearance -> Menus
Click the "Screen Options" (top right) of the screen, make sure "CSS Classes" is selected.
Once you've done the above steps, each of the menu items will now have a "CSS Classses" textbox showing where you can on an individual basis assign CSS Classes (have a look at the screen grab).
Option 2:
You could also try http://codex.wordpress.org/Function_Reference/wp_nav_menu#Adding_Conditional_Classes_to_Menu_Items
<?php
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if(is_single() && $item->title == "Blog"){ //Notice you can change the conditional from is_single() and $item->title
$classes[] = "special-class";
}
return $classes;
}
?>
Option 3:
Provided the above 2 options don't work you have the 3rd option of using jQuery.
HTH
Answer: the nav menu has to be registered & assigned to the theme for the Codex example to work. DUH!
in functions.php:
// default register any navigation menus
register_nav_menus(
array(
'footer' => 'Footer Menu',
'links' => 'Links Menu'
)
);
Assign menu(s) to your theme:
Back in functions.php:
function accessibleAnchors($menu_name, $link_after = NULL){
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);
$menu_list = '<ul id="menu-' . $menu_name . '" class="menu">';
foreach ( (array) $menu_items as $key => $menu_item ) {
// uncomment next line to see all array values
// print_r($menu_item);
$linkText = $menu_item->title;
$url = $menu_item->url;
$target = $menu_item->target;
$titleAttr = $menu_item->attr_title;
// hard-coding the first & only CSS class here, you could loop through others
$cssClass = $menu_item->classes[0];
if($target == '_blank'):
$titleAttr = " title=\"$titleAttr\"";
$span = "<span class=\"offscreen\"> Opens in a new window</span>";
else:
$span = "";
endif;
$menu_list .= '<li class="menu-item '.$cssClass.'"><a href="' . $url . '"'.$titleAttr. '>' . $linkText . $span . $link_after .'</a></li>';
}
$menu_list .= '</ul>';
}
return $menu_list;
}
Now call the function in your template:
<?php echo accessibleAnchors('footer'); ?>
Boom.
You'll have to start in wp-includes/nav-menu-template.php to Copy and paste the function to your theme's functions.php and change the name or use a filter in functions.php to change selected markup of the hook. And possibly take a look in wp-includes/nav-menu.php as well.
Sorry I can't provide more detail.
Thank you all for a great site. I'm learning a lot. I'm trying to get my subcategory siblings to show when a single post is clicked on. I have set up my parent categories using the wordpress menu. I am using a php widget to call for the children (subcategories) in a separate menu (and then style with CSS). The code I'm using is showing the specific (relevant) children when each category is clicked on; however I am unable to figure out how to make them appear when viewing a post.
<?php
if(is_category()) {
$breakpoint = 0;
$thiscat = get_term( get_query_var('cat') , 'category' );
$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
if(empty($subcategories) && $thiscat->parent != 0) {
$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
}
$items='';
if(!empty($subcategories)) {
foreach($subcategories as $subcat) {
if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
$items .= '
<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
'.$subcat->name.'
</li>';
}
echo "<ul>$items</ul>";
}
unset($subcategories,$subcat,$thiscat,$items);
}
?>
I'm attempting to mimic the behavior of this menu at pioneer woman.com
Any help or a better solution would greatly be appreciated.
Thanks,
This lists all child categories of the current post's category:
<?php
echo '<ul>';
$post_child_cat = array();
foreach((get_the_category()) as $cat) {
$args = array( 'child_of' => $cat->cat_ID );
$categories = get_categories( $args );
if( $categories ) foreach( $categories as $category ) {
echo '<li class="cat-item cat-item-'.$category->term_id.'">'.
'<a title="'.$category->description.'" href="';
echo bloginfo('url');
echo '/category/'.$cat->slug.'/'.$category->slug.'">'.
$category->name.'</a></li>';
}
}
echo '</ul>';
?>