I am really new to wordpress. I have a custom menu that I would like to only display specific menu item when the users browser lands on a specific page.
wp_nav_menu(array('theme_location' => 'menu-main'));
Is there a way to have wp_nav_menu() hook to only display specific menu items that have specific css class items?
If I understand your question well, Yes, there is a way. You can use conditionals. PHP if statements. WordPress allows for you check the page title, ID, category before you execute a command e.g
if( is_page( array( 'about-us', 'contact', 'management' ) ) {
//either in about us, or contact, or management page is in view
//execute something
} else {
//none of the page about us, contact or management is in view
//execute something
}
And you can add a class/ID to the wp_nav_menu
So joining the two ideas, you can have:
// When Page 42 (ID) is being displayed.
if ( is_page( 42 ) ) {
wp_nav_menu(array('menu_id' => 'pines', 'menu_class' => 'pnav'));
} elseif( is_page( array( 'about-us', 'contact', 'management' ) ) {
wp_nav_menu(array('menu_id' => 'bananas', 'menu_class' => 'nav'));
} else {
wp_nav_menu(array('menu_id' => 'main-id', 'menu_class' => 'main-nav'));
}
Having added unique IDs and classes, you can style this in the way you want.
Reference https://codex.wordpress.org/Conditional_Tags
Consider searching for existing plugins prior writing custom functions. For your specific feature, I would use the Page Specific Menu Items plugin.
If you would like to add to an existing function, consider using the body_class filter. With this function, you would be able to add a css class to your body element, then write css that changes how your menu appears for any specific page.
I'm building a WordPress site and I need to create a menu made of child pages inside a parent page. So, I've achieved this correctly in 2 ways:
Building a custom menu and adding it inside a widget
Following this tutorial:
http://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/
What I'm trying to get is smth similar as in the left side of this site: http://optinmonster.com/how-it-works/actionable-insights/
But I'm having a problem, cause when I click on the menu elements I created, they open up as pages of their own. While I want them to show up on the parent page... The idea is that I want different content to display in the parent page based on the menu items I click on. What should I do?? Is there a plugin for this??
P.S, I believe that I haven't yet found a solution for this problem because I'm googling it wrong, so is there a definite name for the problem I'm having??
I would really appreciate some help.
<?php
if ($post->post_parent) {
$page = $post->post_parent;
} else {
$page = $post->ID;
}
$children = wp_list_pages(array(
'child_of' => $page,
'echo' => '0',
'title_li' => ''
));
if ($children) {
echo "<ul>\n".$children."</ul>\n";
}
?>
i would like to show the submenu of my page in a custom page template on the left side in a Sidebar-area. Does any know the code for showing the submenu.
I'm using Wordpress 3.4.1.
Register Menus
Firstly, in your theme's functions.php, you need to write a function to register the names of your menus.
function register_my_menus() {
register_nav_menus(
array( 'sidebar-menu' => __( 'Sidebar Menu' ) )
);
}
add_action( 'init', 'register_my_menus' );
Display Menus on Theme
Once you've done that, your theme will be almost ready. The last preparation step is to tell the theme where you want the menus to show up.
<?php wp_nav_menu( array( 'theme_location' => 'sidebar-menu' ) ); ?>
That's all the background work. To finish, you would simply visit the Appearance -> Menus panel in your site admin.
Please read the wordpress codex site.. it has plenty of information on how to do this..
http://codex.wordpress.org/Navigation_Menus
No matter what parameters I pass to wp_nav_menu it always comes out blank (NULL).
Here's how I registered the nav support in functions.php:
function mytheme_addmenus() {
add_theme_support( 'nav-menus' );
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
array(
'rightsidebar' => 'Right Sidebar Menu'
)
);
}
}
add_action( 'init', 'mytheme_addmenus' );
Then I create a new menu in the admin and assign it to the "Right Sidebar Menu" location. The ID of the menu is 5, the name is test.
None of the following return anything (NULL to be precise). I'm calling it in sidebar.php:
wp_nav_menu('menu=test');
or
wp_nav_menu(array('menu' => 'test'));
or
wp_nav_menu($a = array('menu' => 'test'));
or
wp_nav_menu('menu_id=5');
or
wp_nav_menu('menu=5');
or
wp_nav_menu('menu=rightsidebar');
However, when I call the following I get a valid object containing the menu meta data:
wp_get_nav_menu_object('test')
So clearly 'test' is a valid menu= parameter.
Am I missing something? Please help!
PS. Wordpress version 3.3. Theme is super basic built from scratch.
you are calling the menu by menu id (which you are saying is 5) but you are registering it by a theme location.
Try
wp_nav_menu( array( 'theme_location' => 'rightsidebar' ));
wp_nav_menu echoes the reult instead of returning it. If you want to return the menu string, try using :
$args['echo'] = false;
Is there any way to disable the option of adding a new post under a Custom Post Type in WordPress (3.0)? I've looked into labels and arguments but can't find anything that would resemble such a feature.
There is a meta capability create_posts that is documented here and is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
),
'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
You'll probably want to set map_meta_cap to true as well. Without it, you won't be able to access the posts' editing pages anymore.
The combinations of the solutions above work in hiding the links (although someone could quite easily type the URL in directly.
The solution mentioned #3pepe3 relies on get_post_type() which will only work if there is already a post in the listing. If there are no posts, the function will not return anything, and the "Add New" link will be available. An alternative method:
function disable_new_posts() {
// Hide sidebar link
global $submenu;
unset($submenu['edit.php?post_type=CUSTOM_POST_TYPE'][10]);
// Hide link on listing page
if (isset($_GET['post_type']) && $_GET['post_type'] == 'CUSTOM_POST_TYPE') {
echo '<style type="text/css">
#favorite-actions, .add-new-h2, .tablenav { display:none; }
</style>';
}
}
add_action('admin_menu', 'disable_new_posts');
EDIT: To prevent direct access if someone types the URL in themselves: https://wordpress.stackexchange.com/a/58292/6003
WordPress Networks: I found that Seamus Leahy's answer doesn't work if you are logged in as a super admin of the network, it doesn't matter if the user doesn't have the capability, mapped or otherwise, when current_user_can($cap) is called by the CMS. By digging into the core I found you can do the following.
register_post_type( 'custom_post_type_name', array(
'capability_type' => 'post',
'capabilities' => array(
'create_posts' => 'do_not_allow', // Removes support for the "Add New" function, including Super Admin's
),
'map_meta_cap' => true, // Set to false, if users are not allowed to edit/delete existing posts
));
The accepted answer hides the menu item, but the page is still accessible.
In wordpress and for all the post types there is the capability create_posts. This capability is used in several core files :
wp-admin\edit-form-advanced.php
wp-admin\edit.php
wp-admin\includes\post.php
wp-admin\menu.php
wp-admin\post-new.php
wp-admin\press-this.php
wp-includes\admin-bar.php
wp-includes\class-wp-xmlrpc-server.php
wp-includes\post.php
So if you really want to disable this feautere you must do it per role and per post type.
I use the great plugin "User Role Editor" to manage the capabilities per role.
But what about the capability create_posts? Well this capability is not mapped and also create_posts is equal to create_posts so we should fix this and map the capability per post type.
So you can add this piece of code in your functions.php and the you can manage this capability.
function fix_capability_create(){
$post_types = get_post_types( array(),'objects' );
foreach ( $post_types as $post_type ) {
$cap = "create_".$post_type->name;
$post_type->cap->create_posts = $cap;
map_meta_cap( $cap, 1);
}
}
add_action( 'init', 'fix_capability_create',100);
So here we are not hiding or removing menu elements... here we are removing the capability for users (including xmlrpc requests).
The action was init and not admin_init or anything else because init at priority 100 prevents the display of "add new" on admin bar, sidebar, etc (in all the wp interface).
Disable creating new post for registered post-types: (example for post and page)
function disable_create_newpost() {
global $wp_post_types;
$wp_post_types['post']->cap->create_posts = 'do_not_allow';
//$wp_post_types['page']->cap->create_posts = 'do_not_allow';
//$wp_post_types['my-post-type']->cap->create_posts = 'do_not_allow';
}
add_action('init','disable_create_newpost');
add_action("load-post-new.php", 'block_post');
function block_post()
{
if($_GET["post_type"] == "custom_type")
wp_redirect("edit.php?post_type=custom_type");
}
# Staffan Estberg,
This is best way to hide the Add New or Create New button in custom postypes
'capability_type' => 'post',
'capabilities' => array( 'create_posts' => false ),
'map_meta_cap' => true,
It disable to create new post in custom post types both side in admin menu and above the list of post type.
I found this simplest way for this. Just ad this code into theme’s function.php.
function hd_add_buttons() {
global $pagenow;
if (is_admin()) {
if ($_GET['post_type'] == 'custom_post_type_name') {
echo '<style>.add-new-h2{display: none !important;}</style>';
}
}
}
add_action('admin_head', 'hd_add_buttons');
As the question is 'how to disable add-new button on custom post type', and not 'how to restrict user editing custom post types', in my opinion the answer should be purely hiding the buttons with css, by adding this to the functions.php file :
add_action( 'admin_head', function(){
ob_start(); ?>
<style>
#wp-admin-bar-new-content{
display: none;
}
a.page-title-action{
display: none !important;
}
#menu-posts-MY-CUSTOM-POST-TYPE > ul > li:nth-child(3) > a{
display:none;
}
</style>
<?php ob_end_flush();
});