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
Related
I would like to modify the get_author_posts_url function to get a custom taxonomy guest author slug that I have created.
Is there a way to change the following function to fetch a custom taxonomy link? I have used a filter to register the guest author as the post author by using a custom taxonomy, but I couldn't find a way to connect it to this get_author_posts_url function:
function get_author_posts_url( $author_id, $author_nicename = '' ) {
global $wp_rewrite;
$author_id = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty( $link ) ) {
$file = home_url( '/' );
$link = $file . '?author=' . $author_id;
} else {
if ( '' === $author_nicename ) {
$user = get_userdata( $author_id );
if ( ! empty( $user->user_nicename ) ) {
$author_nicename = $user->user_nicename;
}
}
$link = str_replace( '%author%', $author_nicename, $link );
$link = home_url( user_trailingslashit( $link ) );
}
Besides using the following filter, I also added the custom taxonomy (autoria) as the base slug instead of the /author slug. It's all working in the way that it is registering the custom author as the post author and also creating a custom taxonomy link in which I can access all posts by a certain custom author, but whenever I try to fetch the Author_URL it still shows the user that created the post (admin).
The filter I'm using is:
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = wp_get_post_terms( $post->ID, 'autoria', array( 'fields' => 'names' ) );
$author = implode( ', ', $author );
if ( $author )
$name = $author;
return $name;
}
Is there a way to connect this guest_author_name function to register the URL as well?
Thanks a lot, any help is appreciated
I added a Text field to the Category taxonomy using ACF so that I can add a font awesome icon code to be displayed in a list of all categories.
The end result I am looking for is a list of all categories names linked to their respective category pages (if they have posts or not) with the icon from the custom field before each category name.
Reviewing the ACF documents and looking around for a solution I have only been able to display one icon at a time or just create errors.
Is there a more straight forward way I can loop through all of the categories and include the name, link and icon from my ACF custom field in the way I would do so for posts that have a custom field added on to it?
Here is the code I have so far but it only shows one at a time even though I am trying to use a foreach to loop through them all:
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
foreach( $categories as $category ) {
$terms = get_the_terms( get_the_ID(), 'category');
if($terms) {
$term = array_pop($terms);
$icon = get_field('cat_icon', $term );
}
echo $icon;
$output .= '' . esc_html( $category->name ) . '' . $separator;
}
echo trim( $output, $separator ); ?>
Any help is appreciated, thank you!
As requested here is a screenshot, you can see it is displaying the icon from my custom field but only one category out of the 3 I currently have added.
You can use get_terms. try the below code.
<?php
$categories = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false
) );
$separator = ' ';
$output = '';
foreach( $categories as $category ) {
if($category) {
$icon = get_field('cat_icon', $category );
}
echo $icon;
$output .= '' . esc_html( $category->name ) . '' . $separator;
}
echo trim( $output, $separator );
?>
I keep searching for a way to do this, but I can't find anything unfortunately.
I an trying to display all the product's attributes and values, separated by a pipe, in a custom place on the single-product (so for that I was thinking to create a shortcode, so I can place it anywhere I want). the output would be something like this:
BRAND: RENAULT | MODEL: 12 | YEAR: 1973
The code on the Woocommerce template product-attributes.php lists the attributes of the current product on single-product page, but it will list it with some styles I don't want in a place I don't want.
I want to create a shortcode with that code, which is:
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<?php echo wp_kses_post( $product_attribute['label'] ); ?>: <?php echo wp_kses_post( $product_attribute['value'] ); ?> |
<?php endforeach; ?>
How can I create a shortcode with it? I know the general code for a shortcode, but I don't know how to actually integrate the above one in it:
function custom_attributes_product_page() {
// integrate the required code
// Output needs to be return
return
}
// register shortcode
add_shortcode('custom-attributes', 'custom_attributes_product_page');
Would be great if this shortcode would list the attributes and their values separated by a column, like I said above (how to do that?)
Any help is highly appreciated.
Try the following shortcode that will display all product attribute(s) set for a product and their value(s), handling custom attributes too:
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
USAGE: [display-attributes] or with a defined product Id [display-attributes id="254"]
You will get a display like: BRAND: RENAULT | MODEL: 12 | YEAR: 1973
If you don't want the linked terms, replace:
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
by this:
$option_values[] = $term->name;
I need to show different product categories on several regular pages but can't find a suitable shortcode for that.
Which shortcode do I have to use to show only single product category (without showing any products from that category) on any page?
Any help is appreciated.
I think that what you are asking is to get the link from a product category to display the linked product category name. For that you can build a custom shortcode:
add_shortcode('linked_pcat', 'display_a_linked_product_category');
function display_a_linked_product_category( $atts ) {
$atts = shortcode_atts( array(
'term' => '',
), $atts, 'linked_pcat' );
$taxomomy = 'product_cat';
$value = $atts['term'];
if( is_numeric( $value ) || (string) (int) $value == $value ) {
$field = 'term_id';
$value = (int) $atts['term'];
} else {
$field = 'slug';
$value = sanitize_title( $atts['term'] );
}
if( term_exists( $value, $taxomomy ) ) {
$term = get_term_by( $field, $value, $taxomomy );
$term_link = get_term_link( $term, $taxomomy );
return '' . $term->name . '';
}
return false;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
USAGE:
Note: For the product category term can use a term name, a term slug or a term ID…
1) In the Wordpress Text Editor (with a term name for example):
[linked_pcat term='Clothing']
2) Inside php code (with a term slug for example):
echo do_shortcode( "[linked_pcat term='t-shirts']" );
3) In a php file between html code (with a term Id for example):
<?php echo do_shortcode( "[linked_pcat term='t-shirts']" ); ?>
I'm using WordPress Multisite and I'm trying to display all the categories in every site on one page. When I'm on my admin account, the following code works. However, when I switch to any other account, no categories are shown.
$t=get_current_blog_id();
foreach(function_that_gets_blogs() as $k=>$blog){
switch_to_blog($blog['blog_id']);
print_r(get_categories(array('hide_empty'=>true))); // prints "array()"
foreach(get_categories(array('hide_empty'=>true)) as $cat){
...
}
}
switch_to_blog($t);
Why aren't the categories showing?
Like b__ said you should check for:
Where are you using this code? (functions.php, plugin)
You should disable plugins 1 by 1, to check if some are interfering
and in the last case change theme
I've done something like you, and here is the code, in case you wanna try it:
// Current Site
$current = get_current_site();
// All Sites
$blogs = get_blog_list( 0, 'all' );
foreach ( $blogs as $blog ) {
// switch to the blog
switch_to_blog( $blog['blog_id'] );
// get_categories args
$args = array(
'hide_empty' => true
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$link = get_category_link( $category->term_id );
$name = $category->name;
printf( '%s ', $link, $name, $name );
}
}
// return to the current site
switch_to_blog( $current->id );
Update 2014/06/01
the function get_blog_list(); is deprecated since version 3.0, with that you should change that function within wp_get_sites();
// Current Site
$current = get_current_site();
// All Sites
$blogs = wp_get_sites();
foreach ( $blogs as $blog ) {
// switch to the blog
switch_to_blog( $blog['blog_id'] );
// get_categories args
$args = array(
'hide_empty' => true
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
$link = get_category_link( $category->term_id );
$name = $category->name;
printf( '%s ', $link, $name, $name );
}
}
// return to the current site
switch_to_blog( $current->id );
Simple as that...
Did you try:
<?php wp_list_categories("title_li=");?>