I need to add a wrapper div around all the content within the author box.
What filter code do I need to change the genesis_author_box into this:
<section class="author-box" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
<div class="author-box-wrap">
<img alt="" src="" srcset="" class="avatar avatar-150 photo" height="150" width="150">
<h4 class="author-box-title">About <span itemprop="name">Author Name</span></h4>
<div class="author-box-content" itemprop="description">Description Text</div>
</div>
</section>
Here is the default Genesis markup:
<section class="author-box" itemprop="author" itemscope="" itemtype="https://schema.org/Person">
<img alt="" src="" srcset="" class="avatar avatar-150 photo" height="150" width="150">
<h4 class="author-box-title">About <span itemprop="name">Author Name</span></h4>
<div class="author-box-content" itemprop="description">Description Text</div>
</section>
Here is the code that assembles the default Genesis markup:
/**
* Echo the the author box and its contents.
*
* The title is filterable via `genesis_author_box_title`, and the gravatar size is filterable via
* `genesis_author_box_gravatar_size`.
*
* The final output is filterable via `genesis_author_box`, which passes many variables through.
*
* #since 1.3.0
*
* #global WP_User $authordata Author (user) object.
*
* #param string $context Optional. Allows different author box markup for different contexts, specifically 'single'.
* Default is empty string.
* #param bool $echo Optional. If true, the author box will echo. If false, it will be returned.
* #return string HTML for author box if `$echo` param is falsy.
*/
function genesis_author_box( $context = '', $echo = true ) {
global $authordata;
$authordata = is_object( $authordata ) ? $authordata : get_userdata( get_query_var( 'author' ) );
$gravatar_size = apply_filters( 'genesis_author_box_gravatar_size', 70, $context );
$gravatar = get_avatar( get_the_author_meta( 'email' ), $gravatar_size );
$description = wpautop( get_the_author_meta( 'description' ) );
// The author box markup, contextual.
if ( genesis_html5() ) {
$title = __( 'About', 'genesis' ) . ' <span itemprop="name">' . get_the_author() . '</span>';
/**
* Author box title filter.
*
* Allows you to filter the title of the author box. $context passed as second parameter to allow for contextual filtering.
*
* #since unknown
*
* #param string $title Assembled Title.
* #param string $context Context.
*/
$title = apply_filters( 'genesis_author_box_title', $title, $context );
$heading_element = 'h1';
if ( 'single' === $context && ! genesis_get_seo_option( 'semantic_headings' ) ) {
$heading_element = 'h4';
} elseif ( genesis_a11y( 'headings' ) || get_the_author_meta( 'headline', (int) get_query_var( 'author' ) ) ) {
$heading_element = 'h4';
}
$pattern = sprintf( '<section %s>', genesis_attr( 'author-box' ) );
$pattern .= '%s<' . $heading_element . ' class="author-box-title">%s</' . $heading_element . '>';
$pattern .= '<div class="author-box-content" itemprop="description">%s</div>';
$pattern .= '</section>';
} else {
$title = apply_filters( 'genesis_author_box_title', sprintf( '<strong>%s %s</strong>', __( 'About', 'genesis' ), get_the_author() ), $context );
$pattern = '<div class="author-box">%s<h1>%s</h1><div>%s</div></div>';
if ( 'single' === $context || get_the_author_meta( 'headline', (int) get_query_var( 'author' ) ) ) {
$pattern = '<div class="author-box"><div>%s %s<br />%s</div></div>';
}
}
$output = sprintf( $pattern, $gravatar, $title, $description );
/**
* Author box output filter.
*
* Allows you to filter the full output of the author box.
*
* #since unknown
*
* #param string $output Assembled output.
* #param string $context Context.
* #param string $pattern (s)printf pattern.
* #param string $context Gravatar.
* #param string $context Title.
* #param string $context Description.
*/
$output = apply_filters( 'genesis_author_box', $output, $context, $pattern, $gravatar, $title, $description );
if ( $echo ) {
echo $output;
return null;
} else {
return $output;
}
My PHP is really rough... Thank you for your help!
Give this a try:
add_filter( 'genesis_author_box', 'my_filter_author_box', 10, 3 );
function my_filter_author_box( $output, $context ) {
$output = preg_replace( '/<section (.+?)>(.+)<\/section>/ms', '<section $1><div class="author-box-wrap">$2</div></section>', $output );
return $output;
}
It may be worth reconsidering why you need to add the inner wrapping div and see if you can work out a better way to style it instead.
Related
I'm converting the static HTML/CSS menu into the WordPress menu, it works fine for me in case there's no dropdown, no child menu but I want to make changes in code so I could work fine even for the dropdown menu, now in my case, if I put dropdown menu using WordPress menu, it breaks website header design.
Into Header.php
<?php wp_nav_menu(array(
'menu' => 'HeaderMenu',
'menu_class' => 'navbar-nav mr-auto',
'container' => '',
'li_class' => 'nav-item',
'a_class' => 'nav-link',
'active_class' =>'active' ,
)); ?>
Into Functions.php
register_nav_menus(array(
'HeaderMenu' => __( 'Header Menu', 'theme')
));
function add_class_li($classes, $item, $args){
if(isset($args-> li_class)){
$classes[] = $args->li_class;
}
if(isset($args-> active_class) && in_array('current-menu-item', $classes)){
$classes[] = $args->active_class;
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_li',10,3);
function add_anchor_class($attr, $item, $args){
if(isset($args-> a_class)){
$attr['class'] = $args->a_class;
}return $attr;
}
add_filter('nav_menu_link_attributes', 'add_anchor_class',10,3);
HTML/CSS CODE
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="index.html">HOME<span class="sr-only">(current)</span></a> </li>
<li class="nav-item">
<a class="nav-link" href="about.html">ABOUT</a></li>
<li class="nav-item">
<a class="nav-link" href="service.html">SERVICES</a></li>
<li class="#" href="#">
<a class="nav-link" href="blog.html">BLOG</a></li>
<li class="nav-item" href="#">
<a class="nav-link" href="contact.html">CONTACT</a></li>
<li class="last"><img src="images/search-icon.png" alt="icon"></li>
</ul>
Add this custom nav Walker class in you theme's functions.php
class Custom_Nav_Walker extends Walker_Nav_Menu
{
/**
* #var array $menu_items
*/
private $menu_items;
/**
* #var int $last_menu_id
*/
private $last_menu_id;
/**
* #var int $first_menu_id
*/
private $first_menu_id;
/**
* #inheritdoc
*/
public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
if( is_null( $this->menu_items ) ) {
$this->menu_items = wp_get_nav_menu_items( $args->theme_location );
if( $this->menu_items != false ) {
$first_menu_index = array_key_first( $this->menu_items );
$last_menu_index = array_key_last( $this->menu_items );
$this->first_menu_id = $this->menu_items[ $first_menu_index ]->ID;
$this->last_menu_id = $this->menu_items[ $last_menu_index ]->ID;
}
}
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
/**
* Filters the arguments for a single nav menu item.
*
* #since 4.4.0
*
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param WP_Post $item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
*/
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
/**
* Filters the CSS classes applied to a menu item's list item element.
*
* #since 3.0.0
* #since 4.1.0 The `$depth` parameter was added.
*
* #param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
* #param WP_Post $item The current menu item.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$class_names = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth );
$custom_menu_class = [ 'nav-item' ];
if( in_array( 'current-menu-item', $class_names ) ) {
$custom_menu_class[] = 'active';
}
$class_names = implode( ' ', $custom_menu_class);
if( $item->ID == $this->last_menu_id ) {
$class_names = ' class="last"';
} else {
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
}
$output .= $indent . '<li' . $class_names . '>';
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
if ( '_blank' === $item->target && empty( $item->xfn ) ) {
$atts['rel'] = 'noopener';
} else {
$atts['rel'] = $item->xfn;
}
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
$atts['aria-current'] = $item->current ? 'page' : '';
/**
* Filters the HTML attributes applied to a menu item's anchor element.
*
* #since 3.6.0
* #since 4.1.0 The `$depth` parameter was added.
*
* #param array $atts {
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
*
* #type string $title Title attribute.
* #type string $target Target attribute.
* #type string $rel The rel attribute.
* #type string $href The href attribute.
* #type string $aria_current The aria-current attribute.
* }
* #param WP_Post $item The current menu item.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( 'the_title', $item->title, $item->ID );
/**
* Filters a menu item's title.
*
* #since 4.4.0
*
* #param string $title The menu item's title.
* #param WP_Post $item The current menu item.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
if( $item->ID == $this->last_menu_id ) {
$item_output = '<img src="images/search-icon.png" alt="icon">';
} else if ( $item->ID == $this->first_menu_id ) {
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . $title . '<span class="sr-only">(current)</span>';
$item_output .= '</a>';
$item_output .= $args->after;
} else {
$item_output = $args->before;
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;
}
/**
* Filters a menu item's starting output.
*
* The menu item's starting output only includes `$args->before`, the opening `<a>`,
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
* no filter for modifying the opening and closing `<li>` for a menu item.
*
* #since 3.0.0
*
* #param string $item_output The menu item's starting HTML output.
* #param WP_Post $item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
* #param stdClass $args An object of wp_nav_menu() arguments.
*/
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
and then add this class into wp_nav_menu via walker argument.
wp_nav_menu(
array(
'theme_location' => 'your theme location',
'echo' => true,
'walker' => new Custom_Nav_Walker(),
'menu_class' => 'navbar-nav mr-auto'
)
);
also no need to add any extra filter for menu classes.
This is my code for displaying dropdowns in nav menu. I've found it somewhere in internet and edited. I need to display SVG icon element in parent <li> if has submenu / dropdown menu.
<?php
if ( !class_exists( 'WP_Bootstrap_Navwalker' ) ) {
/**
* WP_Bootstrap_Navwalker class
*
* #package Template
* #subpackage Bootstrap4
*
* #since 1.0.0
* #see https://getbootstrap.com/docs/4.0/components/navbar/
* #extends Walker_Nav_Menu
* #author Javier Prieto
*/
class WP_Bootstrap_Navwalker extends Walker_Nav_Menu {
/**
* Starts the list before the elements are added.
*
* #since WP 3.0.0
*
* #see Walker_Nav_Menu::start_lvl()
*
* #param string $output Used to append additional content (passed by reference).
* #param int $depth Depth of menu item. Used for padding.
* #param stdClass $args An object of wp_nav_menu() arguments.
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
// Default class to add to the file.
$classes = array( 'sub-menu menu__list' );
/**
* Filters the CSS class(es) applied to a menu list element.
*
* #since WP 4.8.0
*
* #param array $classes The CSS classes that are applied to the menu `<ul>` element.
* #param stdClass $args An object of `wp_nav_menu()` arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
/*
* The `.dropdown-menu` container needs to have a labelledby
* attribute which points to it's trigger link.
*
* Form a string for the labelledby attribute from the the latest
* link with an id that was added to the $output.
*/
$labelledby = '';
// Find all links with an id in the output.
preg_match_all( '/(<a.*?id=\"|\')(.*?)\"|\'.*?>/im', $output, $matches );
// With pointer at end of array check if we got an ID match.
if ( end( $matches[2] ) ) {
// Build a string to use as aria-labelledby.
$labelledby = 'aria-labelledby="' . esc_attr( end( $matches[2] ) ) . '"';
}
$output .= '<div class="nav-links__submenu nav-links__submenu--type--menu">';
$output .= '<div class="menu menu--layout--classic">';
$output .= "{$n}{$indent}<ul$class_names $labelledby role=\"menu\">{$n}";
}
/**
* Starts the element output.
*
* #since WP 3.0.0
* #since WP 4.4.0 The {#see 'nav_menu_item_args'} filter was added.
*
* #see Walker_Nav_Menu::start_el()
*
* #param string $output Used to append additional content (passed by reference).
* #param WP_Post $item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $id Current item ID.
*/
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
/*
* Initialize some holder variables to store specially handled item
* wrappers and icons.
*/
$linkmod_classes = array();
$icon_classes = array();
/*
* Get an updated $classes array without linkmod or icon classes.
*
* NOTE: linkmod and icon class arrays are passed by reference and
* are maybe modified before being used later in this function.
*/
$classes = self::separate_linkmods_and_icons_from_classes( $classes, $linkmod_classes, $icon_classes, $depth );
// Join any icon classes plucked from $classes into a string.
$icon_class_string = join( ' ', $icon_classes );
/**
* Filters the arguments for a single nav menu item.
*
* WP 4.4.0
*
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param WP_Post $item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
*/
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
// Add .dropdown or .active classes where they are needed.
if ( isset( $args->has_children ) && $args->has_children ) {
$classes[] = 'dropdown';
}
if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) {
$classes[] = 'active';
}
// Add some additional default classes to the item.
$classes[] = 'menu-item-' . $item->ID;
$classes[] = 'nav-item menu__item';
// Allow filtering the classes.
$classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth );
// Form a string of classes in format: class="class_names".
$class_names = join( ' ', $classes );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
/**
* Filters the ID applied to a menu item's list item element.
*
* #since WP 3.0.1
* #since WP 4.1.0 The `$depth` parameter was added.
*
* #param string $menu_id The ID that is applied to the menu item's `<li>` element.
* #param WP_Post $item The current menu item.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement"' . $id . $class_names . '>';
// Initialize array for holding the $atts for the link item.
$atts = array();
/*
* Set title from item to the $atts array - if title is empty then
* default to item title.
*/
if ( empty( $item->attr_title ) ) {
$atts['title'] = ! empty( $item->title ) ? strip_tags( $item->title ) : '';
} else {
$atts['title'] = $item->attr_title;
}
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
// If the item has children, add atts to the <a>.
if ( isset( $args->has_children ) && $args->has_children && 0 === $depth && $args->depth > 1 ) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['aria-haspopup'] = 'true';
$atts['aria-expanded'] = 'false';
$atts['class'] = 'dropdown-toggle nav-link';
$atts['id'] = 'menu-item-dropdown-' . $item->ID;
} else {
$atts['href'] = ! empty( $item->url ) ? $item->url : '#';
// For items in dropdowns use .dropdown-item instead of .nav-link.
if ( $depth > 0 ) {
$atts['class'] = 'dropdown-item';
}
}
$atts['aria-current'] = $item->current ? 'page' : '';
// Update atts of this item based on any custom linkmod classes.
$atts = self::update_atts_for_linkmod_type( $atts, $linkmod_classes );
// Allow filtering of the $atts array before using it.
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
// Build a string of html containing all the atts for the item.
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
// Set a typeflag to easily test if this is a linkmod or not.
$linkmod_type = self::get_linkmod_type( $linkmod_classes );
// START appending the internal item contents to the output.
$item_output = isset( $args->before ) ? $args->before : '';
/*
* This is the start of the internal nav item. Depending on what
* kind of linkmod we have we may need different wrapper elements.
*/
if ( '' !== $linkmod_type ) {
// Is linkmod, output the required element opener.
$item_output .= self::linkmod_element_open( $linkmod_type, $attributes );
} else {
// With no link mod type set this must be a standard <a> tag.
$item_output .= '<a' . $attributes . '>';
}
/*
* Initiate empty icon var, then if we have a string containing any
* icon classes form the icon markup with an <i> element. This is
* output inside of the item before the $title (the link text).
*/
$icon_html = '';
if ( ! empty( $icon_class_string ) ) {
// Append an <i> with the icon classes to what is output before links.
$icon_html = '<i class="' . esc_attr( $icon_class_string ) . '" aria-hidden="true"></i> ';
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( 'the_title', esc_html( $item->title ), $item->ID );
/**
* Filters a menu item's title.
*
* #since WP 4.4.0
*
* #param string $title The menu item's title.
* #param WP_Post $item The current menu item.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
// If the .sr-only class was set apply to the nav items text only.
if ( in_array( 'sr-only', $linkmod_classes, true ) ) {
$title = self::wrap_for_screen_reader( $title );
$keys_to_unset = array_keys( $linkmod_classes, 'sr-only', true );
foreach ( $keys_to_unset as $k ) {
unset( $linkmod_classes[ $k ] );
}
}
// Put the item contents into $output.
$item_output .= isset( $args->link_before ) ? $args->link_before . $icon_html . $title . $args->link_after : '';
/*
* This is the end of the internal nav item. We need to close the
* correct element depending on the type of link or link mod.
*/
if ( '' !== $linkmod_type ) {
// Is linkmod, output the required closing element.
$item_output .= self::linkmod_element_close( $linkmod_type );
} else {
// With no link mod type set this must be a standard <a> tag.
$item_output .= '</a>';
}
$item_output .= isset( $args->after ) ? $args->after : '';
// END appending the internal item contents to the output.
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
/**
* Traverse elements to create list from elements.
*
* Display one element if the element doesn't have any children otherwise,
* display the element and its children. Will only traverse up to the max
* depth and no ignore elements under that depth. It is possible to set the
* max depth to include all depths, see walk() method.
*
* This method should not be called directly, use the walk() method instead.
*
* #since WP 2.5.0
*
* #see Walker::start_lvl()
*
* #param object $element Data object.
* #param array $children_elements List of elements to continue traversing (passed by reference).
* #param int $max_depth Max depth to traverse.
* #param int $depth Depth of current element.
* #param array $args An array of arguments.
* #param string $output Used to append additional content (passed by reference).
*/
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
if ( ! $element ) {
return; }
$id_field = $this->db_fields['id'];
// Display this element.
if ( is_object( $args[0] ) ) {
$args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] ); }
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
/**
* Menu Fallback.
*
* If this function is assigned to the wp_nav_menu's fallback_cb variable
* and a menu has not been assigned to the theme location in the WordPress
* menu manager the function with display nothing to a non-logged in user,
* and will add a link to the WordPress menu manager if logged in as an admin.
*
* #param array $args passed from the wp_nav_menu function.
*/
public static function fallback( $args ) {
if ( current_user_can( 'edit_theme_options' ) ) {
// Get Arguments.
$container = $args['container'];
$container_id = $args['container_id'];
$container_class = $args['container_class'];
$menu_class = $args['menu_class'];
$menu_id = $args['menu_id'];
// Initialize var to store fallback html.
$fallback_output = '';
if ( $container ) {
$fallback_output .= '<' . esc_attr( $container );
if ( $container_id ) {
$fallback_output .= ' id="' . esc_attr( $container_id ) . '"';
}
if ( $container_class ) {
$fallback_output .= ' class="' . esc_attr( $container_class ) . '"';
}
$fallback_output .= '>';
}
$fallback_output .= '<ul';
if ( $menu_id ) {
$fallback_output .= ' id="' . esc_attr( $menu_id ) . '"'; }
if ( $menu_class ) {
$fallback_output .= ' class="' . esc_attr( $menu_class ) . '"'; }
$fallback_output .= '>';
$fallback_output .= '<li class="nav-item">' . esc_html__( 'Add a menu', 'wp-bootstrap-navwalker' ) . '</li>';
$fallback_output .= '</ul>';
if ( $container ) {
$fallback_output .= '</' . esc_attr( $container ) . '>';
}
// If $args has 'echo' key and it's true echo, otherwise return.
if ( array_key_exists( 'echo', $args ) && $args['echo'] ) {
echo $fallback_output; // WPCS: XSS OK.
} else {
return $fallback_output;
}
}
}
/**
* Find any custom linkmod or icon classes and store in their holder
* arrays then remove them from the main classes array.
*
* Supported linkmods: .disabled, .dropdown-header, .dropdown-divider, .sr-only
* Supported iconsets: Font Awesome 4/5, Glypicons
*
* NOTE: This accepts the linkmod and icon arrays by reference.
*
* #since 4.0.0
*
* #param array $classes an array of classes currently assigned to the item.
* #param array $linkmod_classes an array to hold linkmod classes.
* #param array $icon_classes an array to hold icon classes.
* #param integer $depth an integer holding current depth level.
*
* #return array $classes a maybe modified array of classnames.
*/
private function separate_linkmods_and_icons_from_classes( $classes, &$linkmod_classes, &$icon_classes, $depth ) {
// Loop through $classes array to find linkmod or icon classes.
foreach ( $classes as $key => $class ) {
/*
* If any special classes are found, store the class in it's
* holder array and and unset the item from $classes.
*/
if ( preg_match( '/^disabled|^sr-only/i', $class ) ) {
// Test for .disabled or .sr-only classes.
$linkmod_classes[] = $class;
unset( $classes[ $key ] );
} elseif ( preg_match( '/^dropdown-header|^dropdown-divider|^dropdown-item-text/i', $class ) && $depth > 0 ) {
/*
* Test for .dropdown-header or .dropdown-divider and a
* depth greater than 0 - IE inside a dropdown.
*/
$linkmod_classes[] = $class;
unset( $classes[ $key ] );
} elseif ( preg_match( '/^fa-(\S*)?|^fa(s|r|l|b)?(\s?)?$/i', $class ) ) {
// Font Awesome.
$icon_classes[] = $class;
unset( $classes[ $key ] );
} elseif ( preg_match( '/^glyphicon-(\S*)?|^glyphicon(\s?)$/i', $class ) ) {
// Glyphicons.
$icon_classes[] = $class;
unset( $classes[ $key ] );
}
}
return $classes;
}
/**
* Return a string containing a linkmod type and update $atts array
* accordingly depending on the decided.
*
* #since 4.0.0
*
* #param array $linkmod_classes array of any link modifier classes.
*
* #return string empty for default, a linkmod type string otherwise.
*/
private function get_linkmod_type( $linkmod_classes = array() ) {
$linkmod_type = '';
// Loop through array of linkmod classes to handle their $atts.
if ( ! empty( $linkmod_classes ) ) {
foreach ( $linkmod_classes as $link_class ) {
if ( ! empty( $link_class ) ) {
// Check for special class types and set a flag for them.
if ( 'dropdown-header' === $link_class ) {
$linkmod_type = 'dropdown-header';
} elseif ( 'dropdown-divider' === $link_class ) {
$linkmod_type = 'dropdown-divider';
} elseif ( 'dropdown-item-text' === $link_class ) {
$linkmod_type = 'dropdown-item-text';
}
}
}
}
return $linkmod_type;
}
/**
* Update the attributes of a nav item depending on the limkmod classes.
*
* #since 4.0.0
*
* #param array $atts array of atts for the current link in nav item.
* #param array $linkmod_classes an array of classes that modify link or nav item behaviors or displays.
*
* #return array maybe updated array of attributes for item.
*/
private function update_atts_for_linkmod_type( $atts = array(), $linkmod_classes = array() ) {
if ( ! empty( $linkmod_classes ) ) {
foreach ( $linkmod_classes as $link_class ) {
if ( ! empty( $link_class ) ) {
/*
* Update $atts with a space and the extra classname
* so long as it's not a sr-only class.
*/
if ( 'sr-only' !== $link_class ) {
$atts['class'] .= ' ' . esc_attr( $link_class );
}
// Check for special class types we need additional handling for.
if ( 'disabled' === $link_class ) {
// Convert link to '#' and unset open targets.
$atts['href'] = '#';
unset( $atts['target'] );
} elseif ( 'dropdown-header' === $link_class || 'dropdown-divider' === $link_class || 'dropdown-item-text' === $link_class ) {
// Store a type flag and unset href and target.
unset( $atts['href'] );
unset( $atts['target'] );
}
}
}
}
return $atts;
}
/**
* Wraps the passed text in a screen reader only class.
*
* #since 4.0.0
*
* #param string $text the string of text to be wrapped in a screen reader class.
* #return string the string wrapped in a span with the class.
*/
private function wrap_for_screen_reader( $text = '' ) {
if ( $text ) {
$text = '<span class="sr-only">' . $text . '</span>';
}
return $text;
}
/**
* Returns the correct opening element and attributes for a linkmod.
*
* #since 4.0.0
*
* #param string $linkmod_type a sting containing a linkmod type flag.
* #param string $attributes a string of attributes to add to the element.
*
* #return string a string with the openign tag for the element with attribibutes added.
*/
private function linkmod_element_open( $linkmod_type, $attributes = '' ) {
$output = '';
if ( 'dropdown-item-text' === $linkmod_type ) {
$output .= '<span class="dropdown-item-text"' . $attributes . '>';
} elseif ( 'dropdown-header' === $linkmod_type ) {
/*
* For a header use a span with the .h6 class instead of a real
* header tag so that it doesn't confuse screen readers.
*/
$output .= '<span class="dropdown-header h6"' . $attributes . '>';
} elseif ( 'dropdown-divider' === $linkmod_type ) {
// This is a divider.
$output .= '<div class="dropdown-divider"' . $attributes . '>';
}
return $output;
}
/**
* Return the correct closing tag for the linkmod element.
*
* #since 4.0.0
*
* #param string $linkmod_type a string containing a special linkmod type.
*
* #return string a string with the closing tag for this linkmod type.
*/
private function linkmod_element_close( $linkmod_type ) {
$output = '';
if ( 'dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type ) {
/*
* For a header use a span with the .h6 class instead of a real
* header tag so that it doesn't confuse screen readers.
*/
$output .= '</span>';
} elseif ( 'dropdown-divider' === $linkmod_type ) {
// This is a divider.
$output .= '</div>';
}
return $output;
}
}
}
EDIT: No any idea?
You can do that using CSS and this function in your function.php:
add_filter( 'wp_nav_menu_objects', 'add_menu_parent_class' );
function add_menu_parent_class( $items ) {
$parents = array();
foreach ( $items as $item ) {
//Check if the item is a parent item
if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
$parents[] = $item->menu_item_parent;
}
}
foreach ( $items as $item ) {
if ( in_array( $item->ID, $parents ) ) {
//Add "menu-parent-item" class to parents
$item->classes[] = 'menu-parent-item';
}
}
return $items;
}
I don't know which classes you used but something like this should work:
#yourmenuclass ul li.menu-parent-item:before {
content: url(image.file);
}
I am trying to add some post titles from a custom post category. I currently have it printing the correct amount of <li> but unfortunately it is the same name. You will see I narrow it down using meta, so I am getting 5 results all the same name.
I do not pretend to be a pro at this, so I am humbly asking for any help that the community may have.
Thanks In advance.
I have tried doing a foreach and also doing the if have_posts. both have yielded the same result.
$page_title = get_the_title();
$args = array(
'orderby' => 'title',
'post_type' => 'person',
'post_status' => 'publish',
'meta_key' => 'division',
'meta_value' => 'Singles'
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ):
$string .= '<ul>';
while( $query->have_posts() ):
$query->the_post();
$string .= '<li>' . get_the_title() . '</li>';
endwhile;
$string .= '</ul>';
echo $string;
wp_reset_postdata();
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
So what we are going for is looking for how many posts exist in "Singles" (in my case that is 5) and printing the post title for each one in an <li>. Currently it prints 1 of the 5, 5 times.
get_the_title() has the following implementation
function get_the_title( $post = 0 ) {
$post = get_post( $post );
$title = isset( $post->post_title ) ? $post->post_title : '';
$id = isset( $post->ID ) ? $post->ID : 0;
if ( ! is_admin() ) {
if ( ! empty( $post->post_password ) ) {
/**
* Filters the text prepended to the post title for protected posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Protected: %s'.
* #param WP_Post $post Current post object.
*/
$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
$title = sprintf( $protected_title_format, $title );
} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
/**
* Filters the text prepended to the post title of private posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Private: %s'.
* #param WP_Post $post Current post object.
*/
$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
$title = sprintf( $private_title_format, $title );
}
}
/**
* Filters the post title.
*
* #since 0.71
*
* #param string $title The post title.
* #param int $id The post ID.
*/
return apply_filters( 'the_title', $title, $id );
}
As you can see you can pass a post to it and then it will get its title. Since you didn't pass a post to it, it didn't get a title for you. Suggestion:
$page_title = get_the_title();
$args = array(
'orderby' => 'title',
'post_type' => 'person',
'post_status' => 'publish',
'meta_key' => 'division',
'meta_value' => 'Singles'
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ):
$string .= '<ul>';
while( $query->have_posts() ):
$string .= '<li>' . get_the_title($query->the_post()) . '</li>';
endwhile;
$string .= '</ul>';
echo $string;
wp_reset_postdata();
else :
// No, we don't have any posts, so maybe we display a nice message
echo "<p class='no-posts'>" . __( "Sorry, there are no posts at this time." ) . "</p>";
endif;
I have a custom post type that has a bunch of posts all formatted like so
Artist - Song Title
for example
The Smashing Pumpkins - Quiet
I am trying to put 'Artist' in a variable $artist and 'Song Title' in a variable $song
$artistsong = get_the_title();
$songeach = explode("-", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
But this does not work. Echo-ing $artist gets the full title
The Smashing Pumpkins - Quiet
and echoing $song does not output anything
This works if I am just starting from plaintext, but not with 'get_the_title()'
$song = "The Smashing Pumpkins - Quiet";
$songeach = explode("-", $song);
$artist = trim($songeach[0]);
$song = trim($songeach[1]);
echo $artist;
//echos 'The Smashing Pumpkins'
echo $song;
//echos 'Quiet'
Is there another way to put the full title into a variable initially other than get_the_title() which does not seem to be working for me, or am I missing something else?
Add this code to your functions.php
function get_the_title_keep_hyphen( $post = 0 ) {
$post = get_post( $post );
$title = isset( $post->post_title ) ? $post->post_title : '';
$id = isset( $post->ID ) ? $post->ID : 0;
if ( ! is_admin() ) {
if ( ! empty( $post->post_password ) ) {
/**
* Filter the text prepended to the post title for protected posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Protected: %s'.
* #param WP_Post $post Current post object.
*/
$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
$title = sprintf( $protected_title_format, $title );
} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {
/**
* Filter the text prepended to the post title of private posts.
*
* The filter is only applied on the front end.
*
* #since 2.8.0
*
* #param string $prepend Text displayed before the post title.
* Default 'Private: %s'.
* #param WP_Post $post Current post object.
*/
$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
$title = sprintf( $private_title_format, $title );
}
}
/**
* Filter the post title.
*
* #since 0.71
*
* #param string $title The post title.
* #param int $id The post ID.
*/
return $title;
}
And use this code in your single.php
$artistsong = get_the_title_keep_hyphen();
$songeach = explode(" - ", $artistsong);
$artist = $songeach[0];
$song = $songeach[1];
See the last line
I change from return apply_filters( 'the_title', $title, $id ); to return $title;
Because apply_filters function change the hyphen from - => –.
It's because of the dash symbol.
Try $songeach = explode("P", $artistsong); and you'll see what I mean. You could try a different character between artist and song title - although probably not ideal.
I seem to be having issues with RW Meta Box API as what I trying to create is have 2 fields on custom field.
as I wish to have field with text field for slider name as text and slider itself as integer using this for percentages
you can see the plugin api here on github https://github.com/rilwis/meta-box
below is sample of my code but as of right now I sure the id and name fields are conflicting so they are not saving as the array I need.
/**
* Get div HTML
*
* #param string $html
* #param mixed $meta
* #param array $field
*
* #return string
*/
static function html( $html, $meta, $field )
{
if ( ! is_array( $meta ) ) $meta = ( array ) $meta;
if ( ! is_array( $html ) ) $html = ( array ) $html;
echo print_r($meta, true);
//echo $field['id'];
$html[] = sprintf(
'<div class="clearfix">
<input type="text" id="%s" name="%s" value="%s">
</div>',
$field['id'][0],
$field['field_name'],
$meta[0]
);
$html[] = sprintf(
'<div class="clearfix">
<div class="rwmb-slider" id="%s" data-options="%s"></div>
<span class="rwmb-slider-value-label">%s<span>%s</span>%s</span>
<input type="hidden" name="%s" value="%s" />
</div>',
$field['id'][1],
esc_attr( json_encode( $field['js_options'] ) ),
$field['prefix'], $meta[1], $field['suffix'],
$field['field_name'], $meta[1]
);
return implode( '<br />', $html );
}
anyone know how I can pull this off as I have tried everything and I had hired 4 people to try pull this off too and have failed to make this work for me.
Stephen