AVADA Wordpress Custom Taxonomy Sidebar - php

I have a custom Wordpress Taxonomy called 'directory-category' with posts sitting in each relevant category within the taxonomy.
I'm using the Avada template with a hope to use their sidebar mechanism, however I'm unable to modify (successfully) this code to not list page ancestors, but rather, listing categories within the taxonomy and then the posts within the categories.
Here is the current sidebar helper function building a sidebar menu based on pages / page ancestors;
$html = '<ul class="side-nav">';
$post_ancestors = get_ancestors( $post_id, 'page' );
$post_parent = end( $post_ancestors );
$html .= '<li';
if( is_page( $post_parent ) ) {
$html .= ' class="current_page_item"';
}
if( $post_parent ) {
$html .= sprintf( '>%s</li>', get_permalink( $post_parent ), __( 'Back to Parent Page', 'Avada' ), get_the_title( $post_parent ) );
} else {
$html .= sprintf( '>%s</li>', get_permalink( $post_id ), __( 'Back to Parent Page', 'Avada' ), get_the_title( $post_id ) );
}
if( $post_parent ) {
$children = wp_list_pages( sprintf( 'title_li=&child_of=%s&echo=0', $post_parent ) );
} else {
$children = wp_list_pages( sprintf( 'title_li=&child_of=%s&echo=0', $post_id ) );
}
if ( $children ) {
$html .= $children;
}
$html .= '</ul>';
return $html;
I really would appreciate the help if someone is able to assist!

Related

Add post category to latest post block in WordPress

I am editing the latest-blog.php file for WordPress and I have two goals.
I would like the post category to be displayed and have a class of category assigned to it.
I would like to assign a class to the thumbnail, the title and the excerpt so I can style them.
The code below is where I believe the changes need to made
foreach ( $recent_posts as $post ) {
$post_id = $post['ID'];
$title = get_the_title( $post_id );
if ( ! $title ) {
$title = __( '(Untitled)' );
}
$post_url = get_permalink( $post_id );
$text = get_post( $post_id );
$text = $text->post_content;
$text = wp_trim_words( $text, 25, '... <a class="subtitle-red" style="display:block;" href="'.$post_url.'">read full article</a>' );
$excerpt = $text;
$list_items_markup .= sprintf(
'<li>
%1$s%3$s<p>%4$s</p>',
get_the_post_thumbnail( $post_id ),
esc_url( get_permalink( $post_id ) ),
esc_html( $title ),
$excerpt
);
if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] ) {
$list_items_markup .= sprintf(
'<time datetime="%1$s" class="wp-block-latest-posts__post-date">%2$s</time>',
esc_attr( get_the_date( 'c', $post_id ) ),
esc_html( get_the_date( '', $post_id ) )
);
}
$list_items_markup .= "</li>\n";
}
As this is a core WP file editing it really isn't the best idea so an extra question... could this be done via functions.php instead?
Thanks
TJ

Display current category first then child categories in WooCommerce

I'm trying to display in the sidebar the current page's category and it's subcategories. The title should be the current category's name and linked to the current category as well. An example of what I'm trying to achieve can be seen here in the sidebar: https://food52.com/shop/pantry
This is my current site as an example:https://farmtofrank.wpengine.com/product-category/prepared-foods/
This is the code I've created so far:
<?php
$terms = get_terms([
'taxonomy' => get_queried_object()->taxonomy,
'parent' => get_queried_object_id(),
]);
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
echo '<div>';
foreach ( $terms as $term) {
echo '<p class="filters">' . $term->name . '</p>';
}
echo '</div>';
?>
It works but it puts the parent link at the bottom of the list. How can I keep the parent link at the top above the subcategories?
I suppose that this is related to product category archive pages. In this case, you are very near, try:
$queried_object = get_queried_object();
if ( is_product_category() && is_a($queried_object, 'WP_Term') ) {
$taxonomy = $queried_object->taxonomy;
echo '<h2 class="shop__sidebar-heading">
' . $queried_object->name . '
</h2>';
$children_terms = get_terms(['taxonomy' => $taxonomy, 'parent' => $queried_object->term_id]);
if ( ! empty($children_terms) ) {
echo '<ul class="'.$queried_object->slug.' child-terms">';
// Loop through children terms
foreach ( $children_terms as $term) {
echo '<li class="filters">' . $term->name . '</li>';
}
echo '</ul>';
}
}
Tested and works. It will require some styling (CSS).

Showing Custom Taxonomy Post Counts in Wordpress

I have created custom posts with custom taxonomy categories in wordpress. I have also created html link and i would like to show the custom post count related to the html next to it.
I have defined the listing categories in my functions php as:
define( 'VA_LISTING_CATEGORY', 'listing_category' );
and have the listing category function as :
function the_listing_categories( $listing_id = 0 ) {
$listing_id = $listing_id ? $listing_id : get_the_ID();
$cats = get_the_listing_categories( $listing_id );
if ( !$cats ) return;
$_cats = array();
foreach($cats as $cat) {
$_cats[] = html_link( get_term_link( $cat ), $cat->name );
}
$cats_list = implode( ', ', $_cats);
printf( __( 'Listed in %s', APP_TD ), $cats_list );
}
Please help in regards to how I can show these?
Thanks

List pages and display subtitle

I've been trying to display the subtitle for each children page of the current parent page. At the moment i've got it working so that it shows all the title's of the children pages.
Basically I want it to show the subtitle underneath each title of the children pages.
// Get childern
$children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') : wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
// Set subtitle
$subtitle = get_the_title($post->the_subtitle);
echo $children;
echo $subtitle;
Any help will be much appreciated.
Cheers
You have two options for this:
1. The OOP approach
Extend the Walker_page class to create a custom iterator to use with wp_list_pages().
Add this to your functions.php:
class Subtitle_walker extends Walker_page {
function start_el(&$output, $page, $depth, $args, $current_page) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$css_class = array('page_item', 'page-item-'.$page->ID);
if ( !empty($current_page) ) {
$_current_page = get_page( $current_page );
_get_post_ancestors($_current_page);
if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
$css_class[] = 'current_page_ancestor';
if ( $page->ID == $current_page )
$css_class[] = 'current_page_item';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = 'current_page_parent';
} elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
//Added subtitle support
$output .= $indent . '<li class="' . $css_class . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . get_the_post_thumbnail($page->ID, array(72,72)) .''.' <span class="subtitle">'.get_the_subtitle($page->ID,'','',0).'</span>';
if ( !empty($show_date) ) {
if ( 'modified' == $show_date )
$time = $page->post_modified;
else
$time = $page->post_date;
$output .= " " . mysql2date($date_format, $time);
}
}
}
And call it in your template file:
$subtitle_menu = new Subtitle_walker();
$args = array(
'title_li' => '',
'child_of' => ($post->post_parent) ? $post->post_parent : $post->ID,
'echo' => 0,
'walker' => $subtitle_menu
);
$children = wp_list_pages($args);
echo $children;
Note: the previous code is based on this tutorial, you can read it to get an in-depth explanation of what it's doing
2. Using custom WP_Query loop
You can use get_page_children to query the child pages and then loop through them and build/echo your custom list items:
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children( $post->post_parent, $all_wp_pages ) : get_page_children( $post->ID, $all_wp_pages );
//Build custom items
foreach($children as $child){
echo '<li class="page-item">';
echo ''.get_the_title($child->ID).'<br/>';
echo '<span class="subtitle">'.get_the_subtitle($child->ID).'</span>';
echo '</li>';
}

WooCommerce - get category for product page

For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Here's the function I'm creating for this...
function my_add_woo_cat_class($classes) {
$wooCatIdForThisProduct = "?????"; //help!
// add 'class-name' to the $classes array
$classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
// return the $classes array
return $classes;
}
//If we're showing a WC product page
if (is_product()) {
// Add specific CSS class by filter
add_filter('body_class','my_add_woo_cat_class');
}
...but how do I get the WooCommerce cat ID?
A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
$product->get_categories() is deprecated since version 3.0! Use wc_get_product_category_list instead.
https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html
I literally striped out this line of code from content-single-popup.php located in woocommerce folder in my theme directory.
global $product;
echo $product->get_categories( ', ', ' ' . _n( ' ', ' ', $cat_count, 'woocommerce' ) . ' ', ' ' );
Since my theme that I am working on has integrated woocommerce in it, this was my solution.
Thanks Box. I'm using MyStile Theme and I needed to display the product category name in my search result page. I added this function to my child theme functions.php
Hope it helps others.
/* Post Meta */
if (!function_exists( 'woo_post_meta')) {
function woo_post_meta( ) {
global $woo_options;
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
?>
<aside class="post-meta">
<ul>
<li class="post-category">
<?php the_category( ', ', $post->ID) ?>
<?php echo $product_cat; ?>
</li>
<?php the_tags( '<li class="tags">', ', ', '</li>' ); ?>
<?php if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] == 'excerpt' ) { ?>
<li class="comments"><?php comments_popup_link( __( 'Leave a comment', 'woothemes' ), __( '1 Comment', 'woothemes' ), __( '% Comments', 'woothemes' ) ); ?></li>
<?php } ?>
<?php edit_post_link( __( 'Edit', 'woothemes' ), '<li class="edit">', '</li>' ); ?>
</ul>
</aside>
<?php
}
}
?>
<?php
$terms = get_the_terms($product->ID, 'product_cat');
foreach ($terms as $term) {
$product_cat = $term->name;
echo $product_cat;
break;
}
?>
To add custom classes to the body tag you can use the body_class hook.
To add one or more classes on the product page based on specific product categories you can use the Wordpress has_term function (to check if a product belongs to that specific product category).
For this I created the array $classes_to_add where:
key: It can be the product category id (or the slug or the name). Or an array of them. Read the documentation.
value: a string containing the classes to add to the body tag. If you want to add more than one class create a string with multiple values separated by a space (see my example below)
So:
// adds a class to the body element based on the product category
add_filter( 'body_class', 'add_body_class_based_on_the_product_category' );
function add_body_class_based_on_the_product_category( $classes ) {
// only on the product page
if ( ! is_product() ) {
return $classes;
}
// create an array with the ids (or slugs) of the product categories and the respective class (or classes) to add
$classes_to_add = array(
30 => 'class-1',
'cat_slug' => 'class-2 class-3',
32 => 'class-4 class-5',
);
// if the product belongs to one of the product categories in the array it adds the respective class (or classes)
foreach ( $classes_to_add as $product_cat => $new_classes ) {
if ( has_term( $product_cat, 'product_cat', get_the_ID() ) ) {
$classes[] = $new_classes;
}
}
return $classes;
}
The code has been tested and works. Add it to your active theme's functions.php.

Categories