I am new to wordpress would be great help if anyone can help me out, i have designed a website using bootstrap framework, for making it CMS i am using wordpress. i need to get menus dynamically from wordpress which i have dine using the following code
<?php wp_nav_menu(array('menu_class' => 'nav nav-justified','container_class' => 'menu_bac')); ?>
but problem i am stuck with is to get sub menus dynamically, i have been advised to use navwalker , but no idea how to add it to my own code.
can someone please help me out
You need to put your code in you function.php file this file located in your theme folder. Implement below code for extend Walker_Nav_Menu class and put your custom code based on your requirement.
class Custom_Menu extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
//print_r($args);
$children = get_posts(array('post_type' => 'nav_menu_item', 'nopaging' => true, 'numberposts' => 1, 'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => $item->ID));
if (empty($children)) {
$output .= $indent . '<li' . $id . $class_names .'>';
} else {
$output .= $indent . '<li class="dropdown">';
}
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
if ($children) {
$item_output = $args->before;
$item_output .= '<a class="dropdown-toggle js-activated" data-toggle="dropdown" href="#">';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '<b class="caret"></b>';
$item_output .= '</a>';
$item_output .= $args->after;
} else {
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
/** This filter is documented in wp-includes/post-template.php */
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
}
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"dropdown-menu\">\n";
}
}
below code put in your header.php file
$defaults = array(
'theme_location' => 'primary',
'menu' => '',
'container' => 'div',
'container_class' => 'navbar-collapse collapse',
'container_id' => '',
'menu_class' => 'nav navbar-nav navbar-right',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'depth' => 0,
'walker' => new Custom_Menu
);
wp_nav_menu($defaults);
Hey you can use this code for your bootstrap menu in wordpress
PHP CODE:-include this code in your header.php file
<div class="navbar-collapse collapse no-padding-lr">
<?php
$site_defaults = array(
'theme_location' => 'primary',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => '',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul class="site-menu">%3$s</ul>',
'depth' => 0,
'walker' => '');
wp_nav_menu($site_defaults);
?>
</div>
Related
function diwp_services_custom_post_type()
{
$labels = array(
'name' => 'Services',
'singular_name' => 'Service',
'add_new' => 'Add Service',
'add_new_item' => 'Enter Service Details',
'all_items' => 'All Services',
'featured_image' => 'Add Service Image',
'set_featured_image' => 'Set Service Image',
'remove_featured_image' => 'Remove Service Image'
);
// Set Options for this custom post type;
$args = array(
'public' => true,
'label' => 'Services',
'labels' => $labels,
'description' => 'Services is a collection of services and their info',
'menu_icon' => 'dashicons-hammer',
'supports' => array('title', 'editor', 'thumbnail'),
'capability_type' => 'page',
);
register_post_type('services', $args);
}
add_action('init', 'diwp_services_custom_post_type');
// >> Create Shortcode to Display Services Post Types
function diwp_create_shortcode_services_post_type()
{
$args = array(
'post_type' => 'services',
'posts_per_page' => '10',
'publish_status' => 'published',
);
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$result .= '<div class="card">';
$result .= '<div class="card-block block-1">';
$result .= ' <h3 class="card-title">' . get_the_title() . '</div>';
$result .= ' <p class="card-text">' . get_the_content() . '</div>';
$result .= '</div>';
$result .= '</div>';
endwhile;
wp_reset_postdata();
endif;
return $result;
}
add_shortcode('services-list', 'diwp_create_shortcode_services_post_type');
// shortcode code ends here
I visited below link and implemented same as that but find no results
DIVEIN WP
Here is the screenshot as I am working on localhost
Any help regarding this will be appreciatable.
Thanks
First, you must fetch permalink from Setting >> permalink, and click Save changes
Seconds, Now you must add do_shortcode on the position you need to show all post from custom post type like :
echo do_shortcode('[services-list]');
I test your code in my end and work good https://abukotsh.me/wp/services/test-service/
Scroll down and you see test services
I code element with visual composer. I want to use fontawesome in element.
Code here show list font-awesome in param visual composer
<code>
array(
'type' => 'iconpicker',
'heading' => esc_html__('Fontawesome', 'interior'),
'param_name' => 'fontawesome_icon',
'settings' => array(
'type' => 'fontawesome'
),
'description' => esc_html__( 'Fontawesome list. Pickup your choice.', 'interior'
),
'dependency' => array(
'element' => 'icon_type',
'value' => array( 'fontawesome-icon' )
)
</code>
I showed list icon in element but i chosen it don't saved and i don't know get value font-awsome display html.
Help me!!!
I have made a code only for Font Awesome Icon... You can try this
below code. it's tested and definitely works. here I have used
"Facebook" in title...Let me know via comment if you found any issue
in code or having any trouble for this.
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if( !class_exists( 'RN_FA_Icon_list' ) ) {
class RN_FA_Icon_list {
private $shortcode;
function __construct() {
/* shortcode base */
$this->shortcode = 'rn_fa_icon_list';
add_action( 'init', array( $this, 'rn_map_shortcode' ) );
add_shortcode( $this->shortcode, array( $this, 'rn_create_shortcode' ) );
}
function rn_map_shortcode( $atts, $content = NULL ) {
if( function_exists( 'vc_map' ) ) {
vc_map(
array(
'name' => esc_html__( 'Font Awesome Icon', 'rn_shortcodes' ),
//'icon' => RN_SHORTCODES_URL . '/admin/img/vc_icons/fancy-list.png',
'base' => $this->shortcode,
'category' => 'Structual',
'class' => 'rn-vc-icon-module rn-structual-module',
'content_element' => true,
'params' => array(
array(
'type' => 'textfield',
'heading' => esc_html__( 'Description', 'rn_shortcodes' ),
'description' => esc_html__( 'Only for internal use. This adds a label to Visual Composer for an easier element identification.', 'rn_shortcodes' ),
'param_name' => 'list_description',
'admin_label' => true,
'group' => 'General'
),
array(
'type' => 'iconpicker',
'heading' => __( 'Icon', 'js_composer' ),
'param_name' => 'icon_fontawesome',
'value' => 'fa fa-adjust',
'group' => 'General',
'settings' => array(
'emptyIcon' => false,
'type' => 'fontawesome',
'iconsPerPage' => 4000,
),
'dependency' => array(
'element' => 'type',
'value' => 'fontawesome',
),
'description' => __( 'Select icon from library.', 'js_composer' ),
),
array(
'type' => 'css_editor',
'param_name' => 'css',
'group' => esc_html__( 'Design Options', 'rn_shortcodes' ),
),
)
)
); /* end mapping */
}
}
function rn_create_shortcode( $atts, $content = NULL ) {
extract( shortcode_atts( array (
'icon_fontawesome' => '',
'css' => ''
), $atts ) );
/* extract list items */
if( function_exists('vc_param_group_parse_atts') && !empty( $values ) ) {
$values = vc_param_group_parse_atts( $values );
}
/* unique listz ID */
$id = uniqid("rn_fa_");
$output = '';
$output .= '<div id="' . esc_attr( $id ) . '">';
$output .= '<i class="' . $icon_fontawesome . '"></i> Facebook';
$output .= '</div>';
return '<div class="wpb_content_element ' . apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, vc_shortcode_custom_css_class( $css, ' ' ), $this->shortcode, $atts ) . '">' . $output . '</div>';
}
}
}
new RN_FA_Icon_list;
?>
I'm following the official guide in ACF documentation but hasn't been able to get it right. I'm using Advanced custom fields and Custom post type UI plugins.
I have a custom post type named materials, each material has a files repeater field, one of the subfield is title. I want to query the posts based on the title and put the results onto the page using ajax.
Here's my functions.php:
function materialsSearchAjax() {
$html = "";
$keyword = $_POST['keyword'];
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'materials',
'meta_key' => 'type',
'meta_value' => 'students',
'meta_query' =>
array(
'key' => 'files_%_title',
'compare' => 'LIKE',
'value' => $keyword,
)
);
$query = new WP_Query( $args );
$posts = array();
$html .= '<div class="Materials-students">';
while( $query->have_posts() ) : $query->the_post();
$html .= '<div class="Files-list u-padding-left--12">';
if( have_rows('files') ){
while ( have_rows('files') ) : the_row();
$html .= '<div class="Files-item u-margin-right--30 u-margin-bottom--18">';
$html .= '<div class="Files-itemImage"></div>';
$html .= '<a href="' . the_sub_field("document") . '" target="_blank" class="Files-itemLink">';
$html .= the_sub_field('title');
$html .= '</a>';
$html .= '</div>';
endwhile;
}
$html .= '</div>';
endwhile;
$html .= '</div>';
wp_reset_query();
return $html;
}
// filter
function materials_where( $where ) {
$where = str_replace("meta_key = 'files_%", "meta_key LIKE 'files_%", $where);
return $where;
}
function igs_scripts_styles() {
wp_enqueue_script( 'ajaxMaterialsSearch', get_template_directory_uri() . '/assets/scripts/ajaxMaterialsSearch.js', array(), false, true );
wp_localize_script( 'ajaxMaterialsSearch', 'ajax_data_object', array( 'url' => admin_url( 'admin-ajax.php' )) );
}
add_action('wp_ajax_nopriv_materialsSearchAjax', 'materialsSearchAjax');
add_action('wp_ajax_materialsSearchAjax', 'materialsSearchAjax');
add_filter('posts_where', 'materials_where');
add_action('wp_enqueue_scripts', 'igs_scripts_styles');
Here's my ajax:
(function($) {
// Trigger submit
$('.Search-magnifier').on('click', function(){
var $form = $(this).parent();
$($form).submit();
});
$('.Search-form').on('submit', function(event){
event.preventDefault();
var $form = $(this);
var searchKeyword = $($form).find('input[type="search"]').val();
console.log('keyword: ' + searchKeyword);
$.ajax({
type: 'POST',
url: ajax_data_object.url,
data: {action: 'materialsSearchAjax', keyword: searchKeyword},
success: function(textStatus) {
// update the content
console.log(textStatus);
$('.Materials-students').replaceWith(textStatus);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(errorThrown);
}
});
});
})(jQuery);
The ajax and query work fine if I query all the materials post without filtering the title so the only think that's wrong is the query itself. I followed the guide but been stuck for hours.
I guess your only mistake is within the meta_query itself. Besides the (optional) first-level relation, a meta_query has to be an array of array(s). Try:
$args = array(
'posts_per_page' => -1,
'post_type' => 'materials',
'meta_key' => 'type',
'meta_value' => 'students',
'meta_query' => array(
array(
'key' => 'files_%_title',
'compare' => 'LIKE',
'value' => $keyword,
)
)
);
From WP Codex:
meta_query (array) - Contains one or more arrays with the following keys: […]
I replicated your case (except for the Ajax) and the query worked fine, so I guess this should also work over Ajax calls.
I have added a drop down menu to my wordpress theme. I've got it installed and it works fine. However, now I would like to display the latest posts from each category in drop down menu. Can anyone help point me in the right direction.
an example of what I'm looking for
Here is the current code of my drop down menu
class CSS_Menu_Walker extends Walker {
var $db_fields = array('parent' => 'menu_item_parent', 'id' => 'db_id');
function start_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul>\n";
}
function end_lvl(&$output, $depth = 0, $args = array()) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
global $wp_query;
$indent = ($depth) ? str_repeat("\t", $depth) : '';
$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;
/* Add active class */
if (in_array('current-menu-item', $classes)) {
$classes[] = 'active';
unset($classes['current-menu-item']);
}
/* Check for children */
$children = get_posts(array('post_type' => 'nav_menu_item', 'nopaging' => true, 'numberposts' => 1, 'meta_key' => '_menu_item_menu_item_parent', 'meta_value' => $item->ID));
if (!empty($children)) {
$classes[] = 'has-sub';
}
$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
$id = apply_filters('nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args);
$id = $id ? ' id="' . esc_attr($id) . '"' : '';
$output .= $indent . '<li' . $id . $value . $class_names .'>';
$attributes = ! empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) .'"' : '';
$attributes .= ! empty($item->target) ? ' target="' . esc_attr($item->target ) .'"' : '';
$attributes .= ! empty($item->xfn) ? ' rel="' . esc_attr($item->xfn ) .'"' : '';
$attributes .= ! empty($item->url) ? ' href="' . esc_attr($item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'><span>';
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= '</span></a>';
$item_output .= $args->after;
$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
function end_el(&$output, $item, $depth = 0, $args = array()) {
$output .= "</li>\n";
}
}
This method uses the built-in wp_get_recent_posts function. All you need to do is copy and paste the following code in your theme’s functions.php file or a site-specific plugin.
function wpb_recentposts_dropdown() {
$string .= '<select id="rpdropdown">
<option value="" selected>Select a Post<option>';
$args = array( 'numberposts' => '5', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts($args);
foreach( $recent_posts as $recent ){
$string .= '<option value="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</option> ';
}
$string .= '</select>
<script type="text/javascript"> var urlmenu = document.getElementById( "rpdropdown" ); urlmenu.onchange = function() {
window.open( this.options[ this.selectedIndex ].value, "_self" );
};
</script>';
return $string;
}
add_shortcode('rp_dropdown', 'wpb_recentposts_dropdown');
add_filter('widget_text','do_shortcode');
Here is my html for the menu:
<div id="main-menu-container">
<ul id="main-menu">
<li><span aria-hidden="true" class="icon-home"></span>Home</li>
<li><span aria-hidden="true" class="icon-briefcase"></span>Portfolio</li>
li><span aria-hidden="true" class="icon-cog"></span>Services</li>
</ul>
</div>
& the WordPress nav code:
<?php
wp_nav_menu(
array(
'theme_location' => 'primary',
'container_id' => 'main-menu-container',
'menu_id' => 'main-menu',
)
);
?>
As you can see, the problem is, I have different span class added to each list item. I can not use link_before & link_after, because the span classes are in between the link, not after & before.
Is there any solution to convert this kind of html menu to WordPress?
You need to use a Custom Walker. You can see a tutorial with several sample code sources here or a more general version below.
Add to functions.php
class description_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
$class_names = ' class="'. esc_attr( $class_names ) . '"';
$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$prepend = '<strong>';
$append = '</strong>';
$description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
if($depth != 0)
{
$description = $append = $prepend = "";
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
$item_output .= $description.$args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
your wp_nav_menu code
wp_nav_menu( array(
'container' =>false,
'menu_class' => 'nav',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => new description_walker())
) ;
code extracted from Kriesi