Month: September 2016

WordPress li nav menu set in 2 columns

WordPress li nav menu set in 2 columns

We can customized wp_nav_menu using walker class.

below snippet to set menu in 2 columns. Add in functions.php file.


class DW_Walker_Nav_Menu extends Walker_Nav_Menu {

var $current_menu = null;
var $break_point = null;

function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {

global $wp_query;

if( !isset( $this->current_menu ) )
$this->current_menu = wp_get_nav_menu_object( $args->menu );

if( !isset( $this->break_point ) )
$this->break_point = ceil( $this->current_menu->count / 2 ) + 1;

$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

$class_names = $value = '';

$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;

$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';

$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

if( $this->break_point == $item->menu_order )
$output .= $indent . '</li></ul><ul><li' . $id . $value . $class_names .'>';
else
$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 .'>';
$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 );
}
}

Lastly, you’ll also need to set you wp_nav_menu call to use a custom walker, which also means you’ll need to pass the args as an array, here’s an example.


wp_nav_menu( array(
'menu' => 'your-menu-id',
'walker' => new DW_Walker_Nav_Menu
) );

Advertisements

Wet Dynamic Limit Post Excerpt Length In WordPress

Many ways to we can change WordPress post excerpt length

  • WordPress hook
  • Custom function

Add bellow snippet in functions.php file.

1. WordPress hook.

function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

2. Custom function Using Number Of Words

function excerpt_limit_word($string, $limit) {
if (str_word_count($string, 0) > $limit) {
$words = str_word_count($string, 2);
$pos = array_keys($words);
$string = substr($string, 0, $pos[$limit]) . '...';
}
return $string;
}

above function to we can add limit base on word as like used limit 20 to set limit 20 words.


echo excerpt_limit_word($content, 20);

3. Custom function Using Number Of Characters


function excerpt_limit_characters($string, $limit){
if(!empty($string)){
$string = strip_tags($string);
if (strlen($string) > $limit) {
$stringCut = substr($string, 0, $limit);
$string = substr($stringCut, 0, strrpos($stringCut, ' '));
}
return $string;
}else{
return false;
}
}

above function to we can add limit base on characters as like used limit 100 to set limit 100 characters.


echo excerpt_limit_characters($content, 100);