Something is really strange or am I doing something wrong, I can't figure it out. When I add the variable $submenu in the wp_nav_menu args it doesn't work. See the examples below.
Error Message
Strict Standards: Only variables should be passed by reference in
This Works
$args = array(
'menu_id' => 'sidebar-menu',
'theme_location' => 'primary',
'submenu' => 'About Us'
);
wp_nav_menu( $args );
This doesn't work
$args = array(
'menu_id' => 'sidebar-menu',
'theme_location' => 'primary',
'submenu' => get_top_level_title()
);
wp_nav_menu( $args );
This function gets the top most parent title nothing fancy.
function get_top_level_title(){
global $post;
$category = get_the_category( $post->ID );
$parent_title = get_the_title( navz_top_most_parent_page_id() );
if( is_single() ){
$category = get_the_category( $post->ID );
$post->post_parent = $category[0]->term_id;
return get_cat_name($category[0]->category_parent);
} else if( $parent_title and !is_category() ){
return $parent_title;
} else {
$cat_obj = get_category( get_query_var( 'cat' ) );
$parent = $cat_obj->parent;
return get_cat_name($parent);
}
}
This code gets the function above into a local variable and passess it to the wp_nav_menu to get the submenu.
$submenu = get_top_level_title();
$args = array(
'menu_id' => 'sidebar-menu',
'theme_location' => 'primary',
'submenu' => $submenu
);
wp_nav_menu( $args );
Related
I have created a template on wordpress where I want to output the child posts of certain parent posts. It will output 5 child posts to the corresponding parent post.
Parent post
Child Post
Child Post
Child Post
The code for this, looks like this:
function vd_list_child_pages( $atts = array() ) {
$atts = shortcode_atts( array(
'id' => 0,
'slug' => '',
), $atts );
$post_id = absint( $atts['id'] );
$post = get_post( $post_id ); // WP_Post on success.
$childpages = '';
if ( $post && is_post_type_hierarchical( 'page' ) ) {
$childpages = wp_list_pages( array(
'child_of' => $post->ID,
'title_li' => '',
'echo' => 0,
'depth' => 1,
'link_before' => '<i class="far fa-file-alt"></i>',
'walker' => '',
) );
}
if ( $childpages ) {
$childpages = '<ul class="child-posts-content">' . $childpages . '</ul>';
}
return $childpages;
}
add_shortcode( 'vd_childpages', 'vd_list_child_pages' );
However, I need the code in the template more often, with different "post_parent" ID's. Do I have to use the code everywhere like this, or can I create a "function", or which option is correct here?
First of all, sorry my horrible English =)
I'll try to explain how works everything:
I use this to create 'agreement' to be searched on my website, só the basic is the name, and where that 'agreement' works, for example, states and cities... but I have a problem when I try to select the 'states' and after show the cities to select where my 'agreement' can be used and save to show on the site.
But when I select the states, the WordPress just don't show the cities, and this problem happened after some att in WordPress plugins (exemple: Advanced custom field)
And know I have no idea how I can fix this.
Here is my code:
//Load cities - Convenio
function ag_apcef_load_cities_field_choices( $field ) {
// get selected
$selected = get_field( 'ag-estado-convenio', $post->ID );
// reset choices
$field['choices'] = array();
if($selected) {
$args = array(
'post_type' => 'cities',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'city_meta_box_state',
'value' => $selected->ID
)
)
);
$posts = get_posts( $args );
foreach($posts as $post) {
$field['choices'][$post->ID] = $post->post_title;
}
}
return $field;
}
add_filter('acf/load_field/name=ag-cidade-convenio', 'ag_apcef_load_cities_field_choices');
// Admin enqueue - Convenio
function ag_apcef_admin_convenio_enqueue( $hook ) {
// Allowed post types
$types = array( 'convenio' );
if( !in_array( get_post_type(), $types ) )
return;
wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate-admin.js' );
wp_localize_script( 'populate-area', 'populate_vars',
array(
'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
)
);
}
add_action( 'admin_enqueue_scripts', 'ag_apcef_admin_convenio_enqueue' );
//Enqueue - Convenio
function ag_apcef_convenio_enqueue() {
if ( is_page( 'portal-de-vantagens' ) || is_page( 'bem-estar' )) {
wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate.js' );
wp_localize_script( 'populate-area', 'populate_vars',
array(
'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
}
}
add_action( 'wp_enqueue_scripts', 'ag_apcef_convenio_enqueue' );
// Load cities by state
function ag_apcef_city_by_state( $selected_state ) {
// Verify nonce
if( !isset( $_POST['populate_nonce'] ) || !wp_verify_nonce( $_POST['populate_nonce'], 'populate_nonce' ) )
die('Permission denied');
// Get selected state
$selected_state = $_POST['state'];
// Populate arr_data
$arr_data = array();
if(!empty($selected_state)) {
$args = array(
'post_type' => 'cities',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'city_meta_box_state',
'value' => $selected_state
)
)
);
$posts = get_posts( $args );
foreach($posts as $post) {
$arr_data[] = [
'key' => $post->ID,
'value' => $post->post_title
];
}
}
return wp_send_json($arr_data);
die();
}
add_action('wp_ajax_apcef_city_by_state', 'ag_apcef_city_by_state');
add_action('wp_ajax_nopriv_apcef_city_by_state', 'ag_apcef_city_by_state');
I'm trying to learn some new stuff but I stuck to something easy.
I managed to manipulate a link from wp menu using this code
add_filter( 'nav_menu_link_attributes', 'menu_item_data_toggle', 10, 2 );
function menu_item_data_toggle( $atts, $item) {
// Manipulate attributes
if ( 74 == $item -> ID )
$atts['data-toggle'] = 'modal';
return $atts;
}
but I need to use this modal with "if" attribute for login user and I can't figure how to.
The code for this modal I want to use is from a button
<?php
if( is_user_logged_in() ){
$account_manage = ''.classifieds_get_option( 'my_profile_looks' ).'';
$submit_ad = add_query_arg( array( $classifieds_slugs['subpage'] => 'submit_ad' ), classifieds_get_permalink_by_tpl('page-tpl_my_profile') );
$modal = '';
}
else{
$account_manage = ''.classifieds_get_option( 'login_looks' ).'';
$submit_ad = '#register';
$modal = 'data-toggle="modal"';
}
$locations = get_nav_menu_locations();
if ( isset( $locations[ 'top-navigation' ] ) ) {
wp_nav_menu( array(
'theme_location' => 'top-navigation',
'menu_class' => 'nav navbar-nav clearfix',
'container' => false,
'echo' => true,
'items_wrap' => '<ul class="%2$s">%3$s',
'depth' => 10,
'walker' => new classifieds_walker,
) );
}
if(get_option('users_can_register')){
echo '<li>'.$account_manage.'</li><li class="submit-add">'.esc_html__( 'SUBMIT AD', 'classifieds' ).'</li></ul>';
}
?>
Can you please me give me a hint to figure it out and use that menu link with this modal attributes?
Thank you!
Really struggling with this regarding sub-menus. I've made a custom theme for Wordpress using Bootstrap. I successfully managed to get to make my main nav menu dynamic with drop down sub menu links using wp_bootstrap_navwalker.
I've also made a nav-pills menu to display as a dynamic sub menu just below the main site nav menu and the page header. It shows all the child pages, of that particular parent.
My issue however, is that when you go on to a page with this menu, the active item on the menu does not display as active.
Here is my HTML/PHP:
<div class="nav-grey-band">
<?php
$args = array(
'menu' => 'primary',
'theme_location' => 'primary',
'submenu' => 'The Team',
'container_class' => 'centered-pills',
'menu_class' => 'nav nav-pills'
);
wp_nav_menu( $args );
?>
</div>
Here is what's in my functions.php:
// Register Custom Navigation Walker
require_once('wp_bootstrap_navwalker.php');
add_filter( 'wp_nav_menu_objects', 'submenu_limit', 10, 2 );
function submenu_limit( $items, $args ) {
if ( empty( $args->submenu ) ) {
return $items;
}
$ids = wp_filter_object_list( $items, array( 'title' => $args->submenu ), 'and', 'ID' );
$parent_id = array_pop( $ids );
$children = submenu_get_children_ids( $parent_id, $items );
foreach ( $items as $key => $item ) {
if ( ! in_array( $item->ID, $children ) ) {
unset( $items[$key] );
}
}
return $items;
}
function submenu_get_children_ids( $id, $items ) {
$ids = wp_filter_object_list( $items, array( 'menu_item_parent' => $id ), 'and', 'ID' );
foreach ( $ids as $id ) {
$ids = array_merge( $ids, submenu_get_children_ids( $id, $items ) );
}
return $ids;
}
I am trying to exclude pages from wp_nav_menu
Here is the function to exclude pages from menu
Works well when
<?php $page = get_page_by_title('my title' );
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID ) ); ?>
Works properly
BUt when using
<?php $page = get_page_by_title('my title' );
$pag1 = get_page_by_title('my title 1' );
$pag2 = get_page_by_title('my title2' );
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID,$pag1->ID,$pag2->ID ) ); ?>
Doesn't work properly.
try this
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => '".$page->ID.",".$pag1->ID.",".$pag2->ID."' ) );
checkout this tutorial
Here is the correct solution
I hope it helps some other people
<?php
$page = get_page_by_title('my title' );
$pag1 = get_page_by_title('my title 1' );
$pag2 = get_page_by_title('my title2' );
$ids = "{$page->ID},{$pag1->ID},{$pag2->ID}";
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $ids ) );
?>
Managed it using the exclude method, but exclude by page id:
wp_nav_menu(
array(
'theme_location' => 'menu-2',
'exclude' => '599, 601'
)
);
(599 & 601 are page id's)
which is written here:
http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/wordpress-tip-how-to-exclude-pages-from-wp_nav_menu-function/
and finding the page id manually is written here:
http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-find-tag-page-post-link-category-id-in-wordpress/
It should work if you put the pages that you want to exclude into an array.
Check out this: http://ehsanis.me/2010/11/30/wordpress-wp_nav_menu-to-exclude-pages/
You can use a Custom Walker Function to just skip the rendering of the menu item.
wp_nav_menu( array(
'theme_location' => 'menu-2',
'walker' => new custom_navigation
) );
class custom_navigation extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
parent::start_el($item_html, $item, $depth, $args);
$exclude = array();
$exclude[] = get_page_by_title( 'my title' );
$exclude[] = get_page_by_title( 'my title 1' );
$exclude[] = get_page_by_title( 'my title2' );
if ( ! in_array( $item->object_id, $exclude ) ) {
// add your menu item html to the $output variable
}
}
}