Custom set "current-menu-item" class for specific pages in WP - wordpress

I'm creating a custom WP theme and I'm using the Wordpress navigation bar. When switching between different pages, WP adds a "current-menu-item" class to link in navigation bar which corresponds to the current page.
However, if that page/post is not present in the nav bar, it doesn't add the "current-menu-item" to any of the nav bar items.
My problem is, when a user visits the page "BLOG" (which is actually a category page) and clicks on a certain post which opens the single.php template, I want the nav bar item "BLOG" to be underlined as it would if the visitor was visiting a blog page.
Sometimes I will want another nav bar item to be underlined when landing on SINGLE, depending on where the user came from (I also have homepage posts, then I'd like HOME to be underlined)
How do I accomplish this? Thanks.

You can use a filter and check what page you are currently on using conditional statements and add classes before the menu gets displayed:
function add_custom_classes($classes, $item){
if(is_single() && $item->title == 'BLOG'){
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter('nav_menu_css_class' , 'add_custom_classes' , 10 , 2);

Related

Change a homepage based on theme wordpress

I have 2 themes in my WP installation: Theme, Theme2, and separately I have 2 Homepages, one for each theme.
Is it any way for me to change a homepage based on active theme dynamically?
So, Example if i'll activate Theme2, Homepage2 becomes a main homepage?
Ideally, i want something like:
if ( current_user_can( 'manage_options' ) ) {
switch_theme('twentytwelve');
/* Activate Homepage_1 */
} else {
switch_theme('twentythirteen');
/* Activate Homepage_2 */
}
}
Change a homepage based on theme wordpress
You can follow this steps and set the homepage page as you have activated the theme.
Click on “Pages” from your dashboard.
Click the “Add New” button on the top and create a new page called, “Homepage.”
Go to “Settings” from the dashboard, and click on “Reading.”
The first option available is called “Front Page Displays.” Change it to “A static page.”
In the “Front Page” drop down, you should see your new homepage. Select it and save your changes.
Go back into “Pages” and click on your homepage.
Add the shortcodes you want to display.
Update your homepage.

Archive displayed on a custom page to be used in menu structure

I would like to direct users to appropriate archive pages from within the menu. If I want this I need a page that I can attach to the menu.
How would I display the exact same stuff as on the archive page (archive.php) on another page so that pagination and functionalities remain the same, but some stuff will be taken from the actual page ? (can create custom page template of course)
I'll still have to show a sidebar for the page that you visited and not archive's sidebar
Breadcrumbs path will still have to show current menu item position and not archive page path
To show sidebar from the actual page is of most importance here.
EDIT
What I want to achieve is this actually:
Lets say I have a page
http://my.page/subpage/something/notifications/
I want that on this page, I can display exactly the same stuff as on the certain archive page which is here:
http://my.page/subpage/notification/
('notification' is a custom post type here)
I already have a solution that displays all archive stuff on another page (created a page template for that), but its a bit complicated to display title, breadcrumbs and sidebar properly for each page, since some of these should stay the same as they would be, but some should take the value of another site.
You can create a custom page template and assign it to a page. On that page you can use get_posts() to query the posts you want, like this:
global $post;
$posts = query_posts( /* Your args here*/ );
foreach($posts as $post) {
setup_postdata($post);
// Do your stuff as in archive.php
}
wp_reset_postdata();

How to stop showing menu in static home page wordpress

Ok, I'm creating a wordpress theme. I don't want my navigation menu to show in the home page "Only" if the site admin setup Front page displays > A static page (select below).
otherwise I want to show the menu in home page & other pages too. I've used this <?php if(!is_front_page()):?> function, but it is not working.
some one suggest me <?php if(!is_home()):?>, but it is not working either.
So how do I make it work?
Please follow the below steps:
Dashborad > Settings > Reading
Select A static page (select below) option in Front page displays section.
Select the page you want to display from Front Page drop down box.
In header.php in the code for displaying menu, make the alteration as below.
if ( ! is_page( '{slug of the selected page in Front Page drop down box}' ) ) {
//enter your code to display menu here
}
?>

How to hide menu only on Home page wordpress

I m using a one page theme. I would like to disable or choose not to show the menu on the home page. But when user scrolls down to the next page the menu should become fixed. I have removed the menu from the home page now, however i'm not able to get the menu to be fixed on top for the other pages.
Please advice.
You can also go the CSS way.
WordPress adds a class home to the body attribute in home page. You can target that particular attribute to hide the menu as shown below.
.home .menu-class-name{
display:none;
}
This will hide the menu from home page but will show it on other pages
Note - replace .menu-class-name with your own menu class
you can do it some thing like the below code:
<?php
if(is_front_page())
{
//Just comment out the menu section
}
else
{
//write your code to show the menu section
}
?>

Making wordpress menu remain highlighted (class="current-page-item" in the menu) for a category listing (not a page)

I have a site that I have completely customized the theme.
Unfortunately the theme and the way it's set up doesn't like you to view the actual page permalink for both the blog and the portfolio to view its content.
Regular pages work fine:
site.com/about
site.com/contact
However, if you go to /portfolio or /blog the pages show up blank
As a result, I had to use appearance>menu to make a custom menu that links to:
/category/portfolio/#all
/category/blog
This makes the content now show up, the only problem is when they click on the portfolio or the blog, wordpress thinks that it's not actually viewing that page, so the tab in the main menu doesnt remain highlighted (to show you what page youre currently on)
Does anyone have any idea how to fix this?
http://eastcoastefx.com/ggqq
Thank you :)
I have done this before. It's not pretty, but it works if both items are WordPress pages.
// hack the queried_object_id for wp_list_pages
global $wp_query;
$queried_object_id = $wp_query->queried_object_id;
if ($season_page) {
// ensure season pages have Season highlighed in nav
$wp_query->queried_object_id = 22;
}
wp_list_pages($args);
// set queried_object_id back to the original
$queried_object_id = $queried_object_id;
I'd be interested to know if you found anything better on the WordPress forum per Mike Schinkel's comment as the above is clearly a hack.

Resources