how to add custom HTML in woocommerce breadcrumb? [duplicate] - php

This question already has an answer here:
Replacing the home link in woocommerce breadcrumbs
(1 answer)
Closed 5 years ago.
I am working on WooCommerce breadcrumb. I just want to add span to home crumb. When i trying to do it, woocommerce removing it
add_filter( 'woocommerce_breadcrumb_defaults', 'change_breadcrumb' );
function change_breadcrumb( $defaults ) {
$defaults['wrap_before'] = '<ol class="breadcrumb breadcrumb1 animated wow slideInLeft" data-wow-delay=".5s">';
$defaults['wrap_after'] = '</ol>';
$defaults['before'] = '<li>';
$defaults['after'] = '</li>';
$defaults['delimiter'] = '';
//add span tag
$defaults['home'] = '<span class="glyphicon glyphicon-home" aria-hidden="true"></span>Home';
return $defaults;
}
What do i get:
<ol class="breadcrumb breadcrumb1 animated wow slideInLeft" data-wow-delay=".5s">
<li>Home</li>
<li>Groceries</li>
</ol>
What do i need:
<ol class="breadcrumb breadcrumb1 animated wow slideInLeft" data-wow-delay=".5s">
<li><span class="glyphicon glyphicon-home" aria-hidden="true"></span>Home</li>
<li class="active">Groceries</li>
</ol>
image
And would be interesting how to add active class to non-home crumbs.
I think the answer is overriding breadcrumb.php
if(sizeof( $breadcrumb ) !== $key + 1){
echo $before;
} else {
echo $active;
}
if($key == 0){
echo '' . $home . '';
}
else if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
And i realized that i can add my own defaults
$defaults['active'] = '<li class="active">';

well.. i suggest a twist on that. would it be possible to use the css3 selector :first-child? (as in tutorial https://www.w3schools.com/cssref/sel_firstchild.asp) to set a background to the first li's link
ul.breadcrumb1:first-child a{
Background:moo!
}
with proper padding you will obtain the same effect

Related

Check if data attribute of button is same as post class

I'm trying to create a post filter by adding a hidden-class to element that don't have a specific category. I have multiple buttons with a 'data-thema'-attribute, I want to check the attribute value of the clicked button and compare it to the classes of the post elements. When the post has no class that equals the data-thema value, I want to add a hidden-class to that element.
Now when a button is clicked, all posts get the class hidden. This is what I want, but it needs to check if there are any posts with the particular class and then remove the hidden-class again.
This is what I have so far:
jQuery
$('.intro .themaWrapper span').on('click', function() {
var clickedTheme = $(this).attr('data-thema');
$('.posts .post').each(function() {
// $(this).addClass('hidden');
if ($(this).hasClass(clickedTheme)) {
$(this).removeClass('hidden');
} else {
$('.posts .post').addClass('hidden');
}
});
});
PHP
foreach( $themas as $category ) {
if( $category->parent == 0 ) {
echo '<span class="btn blue" data-thema="'. esc_attr( $category->slug ) .'">';
echo $category->name;
echo '</span>';
}
}
while ( $query->have_posts() ) {
$query->the_post();
$f = get_fields();
$classes = '';
$terms = wp_get_post_terms( get_the_ID(), 'thema');
foreach($terms as $t) {
$classes .= $t->slug . ' ';
}
echo "<div class='post ". $classes ."'>";
echo "<h2>".get_the_title()."</h2>";
echo "</div>";
}
You can resume the whole logic to a single chained expression:
$('span').on('click', function() {
$('.posts .post')
// Add the hidden class to all the posts
.addClass('hidden')
.removeClass('red')
// Filter those with the same thema
.filter('.' + $(this).data('thema'))
// Remove the hidden class for the filter results
.removeClass('hidden')
.addClass('red');
});
.posts {
margin-top: 20px;
}
.hidden {
display: none;
}
.red {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span data-thema="thema-1">Thema 1</span>
<span data-thema="thema-2">Thema 2</span>
<span data-thema="thema-3">Thema 3</span>
<div class="posts">
<div class="post thema-1">Thema 1 div</div>
<div class="post thema-3">Thema 3 div</div>
<div class="post thema-2">Thema 2 div</div>
<div class="post thema-2 thema-1">Thema 1 and Thema 2 div</div>
</div>

Dynamically build html menu (and sub-menus) using PHP

I want to look for any html files in a specific folder, and build up my menu and sub-menus based on the results:
For example my project files may be : 'project1_qa_qa2.html',..., 'project2_dev_dev1.html', etc... And the explode() should return 'project1', 'qa', 'qa2' ; ... ; 'project2', 'dev', 'dev1' ; ...
I need to loop through each elements {project ( 1 or 2), department (qa or dev) , and id (dev1, qa2, ..)} to build my menu / sub-menus in a dynamic way.
I got a base source from : http://callmenick.com/_development/slide-down-menu/
I added my hardcoded sample code, to show what I'd wanted it to be like.
<?php
$cdir = scandir('./projects');
foreach ($cdir as $filename){
if(preg_match('/(\w+).html/',$filename,$project_name)){
$projects_details = explode('_',$project_name[1]);
}
?>
<div class="container">
<nav>
<ul class="content clearfix">
<li>Welcome</li>
<li class="dropdown">
Projects
<ul class="sub-menu" style="display: none;">
<li>Project1</li>
<li class="dropdown">
Project2
<ul class="sub-menu" style="display: none;">
<li class="dropdown">
Quality Insurance
<ul class="sub-menu" style="display: none;">
<li>QA1</li>
<li>QA2</li>
</ul>
</li>
<li class="dropdown">
Development
<ul class="sub-menu" style="display: none;">
<li>Dev1</li>
<li>Dev2</li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</nav>
</div>
This is just to show you which code I've used and point you in the right direction. For scanning the directory:
function getFromDir( $dir ) {
$cdir = scandir( $dir );
$result = array();
foreach( $cdir as $key => $value ) {
if( !in_array( $value, array('.', '..') ) ) {
if( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) {
$result[$value] = getFromDir($dir . DIRECTORY_SEPARATOR . $value);
} else {
$result[] = $value;
}
}
}
return $result;
}
As shown in the comment for creating the html:
function outputMenu(array $array, $baseUrl = '/') {
$html = '';
foreach( $array as $key => $item ) {
if( is_array( $item ) ) {
$html .= '<li>'.$key.'<ul>';
$html .= outputMenu($item, $baseUrl.$key.'/');
$html .= '</ul></li>';
} else {
$html .= '<li>'.ucfirst(substr($item, 0, -4)).'</li>';
}
}
return $html;
}
echo outputMenu(getFromDir('./projects'));

Create wordpress shortcode for jQuery UI Tabs

I Am trying to create a wordpress shortcode that allows users to creat jQuery UI tabs. I found this great script online which claims to do it - however it has some quite unwelcome results in my page.
Firstly, the script I am currently using is as follows:
add_shortcode( 'tabgroup', 'jqtools_tab_group' );
function jqtools_tab_group( $atts, $content )
{
$GLOBALS['tab_count'] = 0;
do_shortcode( $content );
if( is_array( $GLOBALS['tabs'] ) ){
foreach( $GLOBALS['tabs'] as $tab ){
$tabs[] = '<li><a class="" href="#">'.$tab['title'].'</a></li>';
$panes[] = '<div class="pane"><h3>'.$tab['title'].'</h3>'.$tab['content'].'</div>';
}
$return = "\n".'<!-- the tabs --><ul class="tabs">'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- tab "panes" --><div class="panes">'.implode( "\n", $panes ).'</div>'."\n";
}
return $return;
}
add_shortcode( 'tab', 'jqtools_tab' );
function jqtools_tab( $atts, $content )
{
extract(shortcode_atts(array(
'title' => 'Tab %d'
), $atts));
$x = $GLOBALS['tab_count'];
$GLOBALS['tabs'][$x] = array( 'title' => sprintf( $title, $GLOBALS['tab_count'] ), 'content' => $content );
$GLOBALS['tab_count']++;
}
However, the HTML output I am TRYING to achieve is this:
<div class="tabs">
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div id="tabs-1">
<p>Content</p>
</div>
<div id="tabs-2">
<p>Content</p>
</div>
<div id="tabs-3">
<p>Content</p>
</div>
</div>
The shortcode php gives a slightly different output to what I need in order to make this work - here is my current outputted HTML:
<!-- the tabs -->
<ul class="tabs">
<li><a class="" href="#">Tab 0</a></li>
<li><a class="" href="#">Tab 1</a></li>
<li><a class="" href="#">Tab 2</a></li>
</ul>
<!-- tab "panes" -->
<div class="panes">
<div class="pane">
<h3>Tab 0</h3>Content
</div>
<div class="pane">
<h3>Tab 1</h3>Content
</div>
<div class="pane">
<h3>Tab 2</h3>Content
</div>
</div>
Finally, the shortcode I am using looks like this:
[tabgroup]
[tab title="tab1"]Content[/tab]
[tab title="tab2"]Content[/tab]
[tab title="tab3"]Content[/tab]
[/tabgroup]
My question is, how do I need to change my php code to make the outputted HTML look like the HTML needed to make the tabs work?
OK this is what you have to do.
if( is_array( $GLOBALS['tabs'] ) ){
foreach( $GLOBALS['tabs'] as $k=>$tab ){
$tabs[] = '<li>'.$tab['title'].'</li>';
$panes[] = '<div id="tab-'.$k.'"><p>'.$tab['content'].'</p></div>';
}
$return = "\n".'<!-- the tabs --><div class="tabs"><ul>'.implode( "\n", $tabs ).'</ul>'."\n".'<!-- tab "panes" -->'.implode( "\n", $panes ).'</div>'."\n";
}
You can also add shortcode like this:-
// Tab
function tab_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array(
'title' => ''
), $atts ) );
return '<div id="tabs-'. sanitize_title( $title ) .'">'. do_shortcode( $content ) .'</div>';
}
add_shortcode( 'tab', 'tab_shortcode' );
// Tabs Container
function tabs_container_shortcode( $atts, $content = null ) {
preg_match_all( '/tab title="([^\"]+)"/i', $content, $matche, PREG_OFFSET_CAPTURE );
$tab_title = array();
if( isset($matche[1]) ) {
$tab_title = $matche[1];
}
$output = '';
if( count($tab_title) ) {
$output .= '<div id="tabs">';
$output .= '<ul class="nav clearfix">';
foreach( $tab_title as $tab ){
$output .= '<li>' . $tab[0] . '</li>';
}
$output .= '</ul>' . do_shortcode( $content ) . '</div>';
} else {
$output .= do_shortcode( $content );
}
return $output;
}
add_shortcode( 'tabs_container', 'tabs_container_shortcode' );
This will also work for tabs. Hope this will help you bit.

Wordpress menu design

I require the following output for my site menus for my theme, which I generated using Weebly.
<!--Top level-->
<div class="nav-container">
<ul class='wsite-menu-default'>
<li id='active'>
<a href='/'>Home</a></li>
<li id='pg460790036268222251'>
<a href='/about.html'>About US</a>
<!--Second Level-->
<div class='wsite-menu-wrap' style='display:none'>
<ul class='wsite-menu'>
<li id='wsite-nav-887406923503161985'>
<a href='/our-community.html' >
<span class='wsite-menu-title'>Our Community</span>
</a></li>
<li id='wsite-nav-920389812461078079'>
<a href='/our-approach.html' >
<span class='wsite-menu-title'>Our Approach</span>
<span class='wsite-menu-arrow'>></span></a>
<!--Third level-->
<div class='wsite-menu-wrap' style='display:none'>
<ul class='wsite-menu'>
<li id='wsite-nav-351232458403900697'>
<a href='/research.html' >
<span class='wsite-menu-title'>Research</span></a>
</li>
<li id='wsite-nav-824453669863261400'>
<a href='/advocacy.html' >
<span class='wsite-menu-title'>Advocacy</span></a>
</li>
<li id='wsite-nav-802033955677651213'>
<a href='/publications.html' >
<span class='wsite-menu-title'>Publications</span></a>
</li>
</ul>
</div>
<!--Repeat above block as required -->
........................
<!--End Third-->
</li>
<li id='wsite-nav-307985062116431544'>
<a href='/our-constitution.html' >
<span class='wsite-menu-title'>Our Constitution</span></a>
</li>
<li id='wsite-nav-347537193311521165'>
<a href='/job-opportunities.html' >
<span class='wsite-menu-title'>Job Opportunities</span></a>
</li>
</ul>
</div>
<!--Repeat above block as required -->
..............
<!--End Second-->
</li>
<!--Repeat above block as required -->
..............
<!--End Top-->
</ul></div>
Now I need to port this over to a Wordpress system. I am familiar with menu basics, but need to know how to configure a Walker or custom function for this task for my theme. All jQuery, CSS scripts are in place, all I need to do is nest the following output as it is with the respective ids, classes and specified inline styling. I have created the menu hierarchy at Wordpress admin like I did in Weebly. I have been searching for examples but cant seem to find one that fits my requirement. Below is my Walker function.
class my_menu_walker extends Walker_Nav_Menu
{
function start_lvl(&$output, $depth)
{
$indent = str_repeat("\t", $depth);
if($depth === 0)
{
$output .= "\n$indent<ul class='wsite-menu-default'>\n";
}
else
{
$output .= "\n$indent<ul class='wsite-menu-default' style='display:none'>\n";
}
}
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ($depth)? str_repeat("\t", $depth): '';
$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 ) .'"' : '';
//if second level menus
/*$unit_output = $args->before;*/
$unit_output = "<a".$attributes.">";
$unit_output .= /*$args->link_before.*/ "<span>".apply_filters( 'the_title', $item->title, $item->ID )."</span>";
/*$unit_output .= $args->link_after;*/
$unit_output .= "</a>";
/*$unit_output .= $args->after;*/
//for top menu page navigation
if($item->current)
{
$output .= $indent."<li id='active'>";
}
else
{
$output .= $indent."<li id='pg".$item->ID."'>";
}
$output .= apply_filters( 'walker_nav_menu_start_el', $unit_output, $item, $depth, $args );
}
}
At the top of functions.php
add_theme_support( 'menus' );
And in header.php
<?php wp_nav_menu(array(
'theme_location' => 'header-menu',
'container' => 'div',
'container_class' => 'wsite-menu-default',
'menu_class' => 'nav-container',
'echo' => true,
'walker'=> new my_menu_walker()
)); ?>
My $depth is not receiving the correct data, please advise
function start_lvl(&$output, $depth)
{
$indent = str_repeat("\t", $depth);
if($depth > 1)
{
$output .= "\n$indent<ul class='wsite-menu'>\n";
}
else
{
$output .= "\n$indent<ul class='wsite-menu-default'>\n";
}
}

adding a class current in the navigation category with php

I Have page containe posts i can filter those posts with php passing a value in url for included page , I want to add current class on a tag for the current category replacing another class with php . this is my code for the moment :
<div class="top_p_r" style="width:825px; float:left;">
<span class="cat">Category</span>
<ul class="ul_cat_blog">
<?php
echo'<li class="li_cat_blog"><a class="current" href="/">All</a></li>';
foreach(Categorie::getAllCategorie()as $categorie ){
$categorie= new Categorie($categorie->ID_CAT);
echo '<li class="li_cat_blog"><a class="rotated_link" href="?cat='.$categorie->getIDCategorie().'">'.$categorie->getNomCategorie().'</a></li>';}?>
</ul>
</div>
I want to use the value witch i pass in url in order to change the class="rotated_link" to class="current" , i try like this but it doesn't work with me it show always the class='rotated_link' :
<div class="top_p_r" style="width:825px; float:left;">
<span class="cat">Category</span>
<ul class="ul_cat_blog">
<?php
$currentPath = $_SERVER['REQUEST_URI'];
$currentClass = ' class="current"';
$rotatesdClass = ' class="rotated_link"'; ?>
<?php
echo'<li class="li_cat_blog"><a class="current" href="/">All</a></li>';
foreach(Categorie::getAllCategorie()as $categorie ){
$categorie= new Categorie($categorie->ID_CAT);
echo '<li class="li_cat_blog"><a'; if( $currentPath == $id_categorie ) { echo $currentClass; } else {echo $rotatesdClass;} echo' href="?cat='.$categorie->getIDCategorie().'">'.$categorie->getNomCategorie().'</a></li>';
}?>
</ul>
</div>
thanks
The problem is that you are overwriting your own variable:
foreach(Categorie::getAllCategorie()as $categorie ){
$categorie = new Categorie($categorie->ID_CAT);
Here, you are over-riding the value of $categorie.

Categories