Skip to content

Instantly share code, notes, and snippets.

@alisspers
Last active December 17, 2015 20:38
Show Gist options
  • Save alisspers/5668670 to your computer and use it in GitHub Desktop.
Save alisspers/5668670 to your computer and use it in GitHub Desktop.
Kodsnuttar från blogginlägget som visar hur du kan markera menyval som aktivt när besökaren är inne på ett enskilt inlägg i en Custom Post Type. http://www.webbgaraget.se/2013/05/30/markera-menyval-som-aktivt-nar-besokaren-ar-inne-pa-en-custom-post-type/
<?php
add_action(
'init',
function ()
{
// Vår meny
register_nav_menu( 'main-menu', 'Huvudmeny' );
// Custom Post Type för event
$labels = array(
'name' => 'Events',
'singular_name' => 'Event',
'all_items' => 'All Events',
'add_new_item' => 'Add Event',
'edit_item' => 'Edit Event',
'new_item' => 'New Event',
'view_item' => 'View Event',
'search_items' => 'Search Events',
'not_found' => 'No events found',
'not_found_in_trash' => 'No events found in trash',
);
register_post_type(
'event',
array(
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'public' => true,
)
);
}
);
add_filter(
'nav_menu_css_class',
function ( $classes, $menu_item )
{
if ( is_singular( 'event' ) && in_array( 'events', $classes ) )
{
$classes[] = 'current-menu-item';
}
return $classes;
},
10,
2
);
<?php
add_action(
'init',
function ()
{
// Vår meny
register_nav_menu( 'main-menu', 'Huvudmeny' );
// Custom Post Type för event
$labels = array(
'name' => 'Events',
'singular_name' => 'Event',
'all_items' => 'All Events',
'add_new_item' => 'Add Event',
'edit_item' => 'Edit Event',
'new_item' => 'New Event',
'view_item' => 'View Event',
'search_items' => 'Search Events',
'not_found' => 'No events found',
'not_found_in_trash' => 'No events found in trash',
);
register_post_type(
'event',
array(
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'public' => true,
)
);
}
);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php bloginfo( 'title' ); ?></title>
<?php
wp_enqueue_style( 'wptemp', '/wp-content/themes/webbgaraget/style.css' );
wp_head();
?>
</head>
<body <?php body_class(); ?>>
<header>
<h1>Markera menyval i singelsidor för Custom Post Types</h1>
<p>Meny:</p>
<nav>
<?php wp_nav_menu( array( 'theme_location' => 'main-menu' ) ); ?>
</nav>
</header>
<section role="main">
/*
Theme Name: Markera menyval för CPT
Theme URI:
Description: Visar hur du på två olika sätt kan markera menyval vid visning av custom post types
Author: Webbgaraget
Author URI: http://www.webbgaraget.se/
Version: 1.0
*/
.current-menu-item a,
.single-event .menu-item.events {
font-weight: bold;
}
/*
Theme Name: Markera menyval för CPT
Theme URI:
Description: Visar hur du på två olika sätt kan markera menyval vid visning av custom post types
Author: Webbgaraget
Author URI: http://www.webbgaraget.se/
Version: 1.0
*/
.current-menu-item a {
font-weight: bold;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment