Adding Class current_page_item - php

I am working on a Wordpress-Design and i want to creat a Custom Menu.
$items = wp_get_nav_menu_items('Menu', array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false));
echo '<pre>'; print_r($items); echo '</pre>';
foreach($items as $item){
echo '<div class="menu_entry">'.$item->title.'</div>';
}
The problem is, i need the "current-page"-Class, which is WordPress creating - in the Standard Menu.
Any Ideas how to add this class?

Solution time:
WordPress's function that adds these classes is _wp_menu_item_classes_by_context(). This is called already when you use wp_nav_menu but not wp_get_nav_menu_items. Fortunately, the latter provides a filter so we can do it ourselves:
add_filter( 'wp_get_nav_menu_items', 'prefix_nav_menu_classes', 10, 3 );
function prefix_nav_menu_classes($items, $menu, $args) {
_wp_menu_item_classes_by_context($items);
return $items;
}

You can do a compare on the current page / cat etc ID against the menu items object_id which is the ID of the page / category etc its linked to.
Something like (untested);
global $post;
$thePostID = $post->ID;
foreach($items as $item){
if($thePostID === $item->object_id) {
echo '<div class="menu_entry">'.$item->title.'</div>';
}else{
echo '<div class="menu_entry">'.$item->title.'</div>';
}
}

Using the function get_queried_object_id(). This works fine in all the pages, including the Blog page.
See an example:
if ( $menu_items = wp_get_nav_menu_items( 'menu' ) ) {
foreach ( $menu_items as $menu_item ) {
$current = ( $menu_item->object_id == get_queried_object_id() ) ? 'current' : '';
echo '<li class="' . $current . '">' . $menu_item->title . '</li>';
}
}

Related

Disable rendering of subcategory text after navigating to that subcategory - woocommerce

StackOverflow community. I would like to ask for your help.
My problem is that I want to display all subcategories text in the main category card-box.
Using this code,
add_action('woocommerce_after_subcategory_title', 'woocommerce_subcats_from_parentcat_by_ID', 30);
function woocommerce_subcats_from_parentcat_by_ID($category)
{
$parent_category_ID = $category->term_id;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0, // Set to 0 to show empty categories and 1 to hide them
'parent' => $parent_category_ID,
'taxonomy' => 'product_cat'
);
$subcategories = get_categories($args);
echo '<div class="custom_subcategory_main">';
echo '<span class="custom_subcategory_klasy">Dostępne klasy:</span><br>';
foreach ($subcategories as $subcategory) {
echo '<span> '.$subcategory->name.'</span>';
}
echo '<br>';
echo '<span class="custom_subcategory_klasy">Poziom:</span><br>';
echo '<span>Szkoła podstawowa</span>';
echo '</div>';
}
the subcategories text is displaying correctly in the main category card-box [screen below].
The problem is that after navigating to the product subcategory it is still visible and I would like to hide it. More precisely, I would like to hide this part of the code [screen below].
echo '<span class="custom_subcategory_klasy">Dostępne klasy:</span><br>';
If I understand properly, once you're in a subcategory, there are no further children. So you could put a check inside your function to see if children exists, and if not, exit:
add_action('woocommerce_after_subcategory_title', 'woocommerce_subcats_from_parentcat_by_ID', 30);
function woocommerce_subcats_from_parentcat_by_ID($category)
{
$parent_category_ID = $category->term_id;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0, // Set to 0 to show empty categories and 1 to hide them
'parent' => $parent_category_ID,
'taxonomy' => 'product_cat'
);
$subcategories = get_categories( $args );
if ( empty ( $subcategories ) ) return; // exit
echo '<div class="custom_subcategory_main">';
echo '<span class="custom_subcategory_klasy">Dostępne klasy:</span><br>';
foreach ($subcategories as $subcategory) {
echo '<span> '.$subcategory->name.'</span>';
}
echo '<br>';
echo '<span class="custom_subcategory_klasy">Poziom:</span><br>';
echo '<span>Szkoła podstawowa</span>';
echo '</div>';
}

WordPress custom post type array not working as expcted

I'm using this code...
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = '';
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return .= "'" . $the_excluded . "', ";
}
}
return $return;
}
... to return a list of custom post types that I have selected from an options page. This works fine, and if I do this...
echo included_post_types();
...I see this...
'clothes', 'shoes', 'jackets',
...which is what I excpected.
But the problem is when I try to use included_post_types() in a loop to only show posts with those post types, like this:
$sitemap_post_args = array(
'post_type' => array(included_post_types()),
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);
$loop = new WP_Query($sitemap_post_args);
global $post_type;
global $post;
echo '<ul>';
$last_post_type = '';
while($loop->have_posts()): $loop->the_post();
$current_post_type = $post->post_type;
$current_post_type_object = get_post_type_object( $current_post_type );
if($current_post_type != $last_post_type) echo "<br /><li><strong>" . $current_post_type_object->labels->name . "</strong></li>";?>
<li><?php echo get_the_title(); ?></li>
<?php echo "\n";
$last_post_type = $current_post_type;
endwhile;
wp_reset_query();
echo '</ul>';
It simply doesn't display anything on my page, but it doesn't throw an error either.
I'm almost certain the problem is this line:
'post_type' => array(included_post_types()),
I even tried it like this...
'post_type' => included_post_types(),
...but it did not work.
If I try this...
'post_type' => array('clothes', 'shoes', 'jackets', ),
...it works, but I need to be able to use the function name.
Any suggestions would be helpful.
please replace below code with yours. It should be help.
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = array();
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return[] = $the_excluded;
}
}
return $return;
}
and also replace below code that show posts
$post_type_list = include_post_types();
$sitemap_post_args = array(
'post_type' => $post_type_list,
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);

Foreach - recursion?

I have this code that is getting all categories and subcategories from a woocommerce based website in a hierarchical order, and it works but each and every time when I want to add a new level of depth
category
->sub-cat
->sub-sub-cat
->sub-sub-sub-cat
->etc
I have to add one more foreach loop inside the last loop (I'm not even sure if you can understand me to cause honestly I don't even know how to explain this - sorry, please be patient, it is my second month in PHPs 'fields')
So, I'm trying to get rid of all those foreach loops (to make it more dynamic) and I heard that there is a way to do that by using something called recursion, the kind of stuff that I can use to make this happen. I don't really understand how that works and I've read like the whole day about it so if there is somebody who can help me understand how to get this done I'll be happy?
Here is my code, part of it is taken from StackOverflow.
<ul>
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$hierarchical = 1;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
foreach ($cat as $c) {
if($c->category_parent == 0) {
$catID = $c->term_id;
echo '<li>'. $c->name .'';
$args = array(
'taxonomy' => $taxonomy,
'parent' => $catID,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
if($cat) {
echo '<ul>';
foreach($cat as $c) {
$catID = $c->term_id;
echo '<li>'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'';
$args = array(
'taxonomy' => $taxonomy,
'parent' => $catID,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
if($cat){
echo '<ul>';
foreach ($cat as $c) {
$catID = $c->term_id;
echo '<li>'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'';
$args = array(
'taxonomy' => $taxonomy,
'parent' => $catID,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
if($cat){
echo '<ul>';
foreach ($cat as $c) {
$catID = $c->term_id;
echo '<li>'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
}
?>
It works perfectly, though I'm aware that it is not too smart, so please don't be too harsh on me.
Here is an example of recursion:
You create a function that looks at the item of the array if it's an array you call the function again and pass the item to it (the subarray).
If it's not an array you echo it.
$arr = [1,2,3,[4,5,[6]]];
print_items($arr);
Function print_items($arr){
Foreach($arr as $item){
If(is_array($item)){
print_items($item);
}Else{
Echo $item;
}
}
}
https://3v4l.org/vbjaO
A WordPress-specific printing function might look like this:
<?php
function display_child_categories_of($category_id) {
$subcategories = get_categories(array('child_of' => category_id));
foreach ($subcategories as $subcategory) {
echo '<li>';
echo $subcategory->name;
echo '<ul class="children">';
display_child_categories_of($subcategory);
echo '</ul>';
echo '</li>';
}
}
?>
<ul>
<?php
display_child_categories_of(0); // gets all top-level categories
?>
</ul>
This is a barebones version; you'll want to add in the additional parameters you're using to the get_categories() arguments and then tweak the HTML as needed.
However, as #jaswrks mentioned in the comments, you should use the built-in walk_category_tree() WordPress function instead if it works for your situation.

add shortcode heading showing multiple time

Hi my code is below for adding shortcode in posts. when i am adding shortcode two times it shows me heading two times that i added in code "Recent Posts" is there is way to show this heading only top means one time?
/*shortcode start*/
add_shortcode( 'recent-posts', 'PL_recent_posts' );
function PL_recent_posts( $atts ) {
extract( shortcode_atts( array(
'numbers' => '5',
'order' => 'ASC',
), $atts ) );
$rposts = new WP_Query( array( 'posts_per_page' => $numbers, 'orderby' => 'date' , 'colorss' => $color ) );
if ( $rposts->have_posts() ) {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
while( $rposts->have_posts() ) {
$rposts->the_post();
$html .= sprintf(
'<li>%s</li>',
get_permalink($rposts->post->ID),
get_the_title(),
get_the_title()
);
}
$html .= '</ul>';
}
wp_reset_query();
return $html;
}
Define a global variable to detect whether title is already added.
function PL_recent_posts( $atts ) {
global $title_added;
...
if ( $rposts->have_posts() ) {
if ( $title_added ) {
$html = '<ul class="recent-posts">';
} else {
$html = '<h3>Recent Posts</h3><ul class="recent-posts">';
$title_added = true;
}
Hope that helps..!

inclue link list category as css class

I'd like to render some extra css classes in my wordpress links list, specifically id like to render the link category as a css class, so for example:
link : http://www.foobar.com/
gategories : friends, colleagues
name : Foo Bar
Currentyly renders as:
Foo Bar
But I want it to render as:
Foo Bar
I know that you use the following function to build the links list but i cant work out how to modify it to do what I need:
function wp_list_bookmarks($args = '') {
$defaults = array(
'orderby' => 'name', 'order' => 'ASC',
'limit' => -1, 'category' => '', 'exclude_category' => '',
'category_name' => '', 'hide_invisible' => 1,
'show_updated' => 0, 'echo' => 1,
'categorize' => 1, 'title_li' => __('Bookmarks'),
'title_before' => '<h2>', 'title_after' => '</h2>',
'category_orderby' => 'name', 'category_order' => 'ASC',
'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
'category_after' => '</li>'
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$output = '';
if(1) {
//output one single list using title_li for the title
$bookmarks = get_bookmarks($r);
if ( !empty($bookmarks) ) {
if ( !empty( $title_li ) ){
$output .= str_replace(array('%id', '%class'), array("linkcat-$category", $class), $category_before);
$output .= "$title_before$title_li$title_after\n\t<ul class=\"xoxo blogroll $category\">\n";
$output .= _walk_bookmarks($bookmarks, $r);
$output .= "\n\t</ul>\n$category_after\n";
} else {
$output .= _walk_bookmarks($bookmarks, $r);
}
}
}
$output = apply_filters( 'wp_list_bookmarks', $output );
if ( !$echo )
return $output;
echo $output;
}
?>
Thanks!
Firstly, you should never modify the Wordpress Core functions. In the case of Bookmarks, however, I don't really blame you for wanting to. They're kind of a pain.
I would just build it from scratch using get_bookmarks(). Here's a working example:
foreach(get_bookmarks() as $bm)
{
$terms = get_the_terms($bm->link_id, 'link_category');
$classes = array('wp_link');
if($terms)
foreach($terms as $term)
$classes[] = $term->slug;
echo '<a class="'.implode(' ', $classes).'" href="'.$bm->link_url.'"'.($bm->link_target ? 'target="'.$bm->link_target.'"' : '').'>'.$bm->link_name.'</a><br/>';
}
Just place this wherever it is in your template that you want to generate your bookmarks. Or you can wrap it up in a custom function call, and call that function from your template.

Categories