Order Wordpress-attributes by global/backend order (orderby) - php

I'm trying to create a function, where all available colors (attributes/terms) gets attached to each product - on the category-page (product-listing). The code below works, however, the frontend-order is not identical with the backend-order. I really can't figure out, how I'm suppose to get the attributes sorted accordingly. Have tried changing menu_order to term_order aswell. This dosen't work either.
function ea_display_color_swatches_on_category() {
$html_swatch = '<div class="swatches-wrapper">';
$terms = wp_get_object_terms( get_the_ID(), 'pa_farve', array( 'orderby' => 'menu_order' ) );
foreach ( $terms as $term ) {
$hex_color = get_woocommerce_term_meta( $term->term_id, 'pa_farve_swatches_id_color', true );
$html_swatch .= '<a class="swatch-color-category tooltips"
title="' . $term->name . '"
style="background:' . $hex_color . '" ></a>';
}
// end color-swatches-wrapper
$html_swatch .= '</div>';
echo $html_swatch;
}
add_action( 'woocommerce_before_shop_loop_item', 'ea_display_color_swatches_on_category' );
Source

Have you tried with term_id? WordPress use to sort by creation date.

Related

Display a ACF Category Taxonomy Field With a List of all Categories

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 );
?>

Displaying the Woocommerce subcategory image in a shortcode

So I had a shortcode working well which pulled in the subcategories of a product and displayed the image and text but realised it was at the top of the content because I had used echo . . . so moved the HTML output into a variable so I could return it but the images are coming out of the list items, so seem to be having an issue with the function: woocommerce_subcategory_thumbnail()
Not too sure why but I presume the function must have an echo? I guess I want to just get the image url and put it in a container? Honestly have no idea what the best method is but this is where I'm at
add_shortcode( 'show_products_categories_os', 'categories_of_the_product_os' );
function categories_of_the_product_os() {
$term_id = get_term_by( 'slug', 'os', 'product_cat' );
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms ) {
$output_html .= '<ul class="product-cats osp">';
foreach ( $terms as $term ) {
if($term->parent === $term_id->term_id){
$output_html .= '<li class="category os">';
$output_html .= '' . woocommerce_subcategory_thumbnail( $term ) . '';
$output_html .= '<h2>' . $term->name . '</h2>';
$output_html .= '</li>';
}
}
$output_html .= '</ul>';
}
return $output_html;
}
Is there another function I can't find that can give me jst the url for the image? Or a way of stripping it from that other function?
You need to get the thumbnail_id from the term's metadata and pass that as a parameter to wp_get_attachment_url
add_shortcode( 'show_products_categories_os', 'categories_of_the_product_os' );
function categories_of_the_product_os() {
$term_id = get_term_by( 'slug', 'os', 'product_cat' );
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms ) {
$output_html .= '<ul class="product-cats osp">';
foreach ( $terms as $term ) {
$thumbnail_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
if($term->parent === $term_id->term_id){
$output_html .= '<li class="category os">';
$output_html .= '' . $image_url . '';
$output_html .= '<h2>' . $term->name . '</h2>';
$output_html .= '</li>';
}
}
$output_html .= '</ul>';
}
return $output_html;
}
Source: https://stackoverflow.com/a/51465602/14481105
To Note
You now need to add that $image_url to the src attribute of an img tag for an image to be output, as currently just the URL would appear.

Wordpress - Single taxonomy term to link to category of term

Im currently displaying the custom taxonomy term for my post on a single-resources.php page. However I need it to link to the taxonomy category page and not the link of the page.
This is what I currently have:
<?php
$term_list = wp_get_post_terms($post->ID, 'resourcecategory', array("fields" => "all"));
foreach($term_list as $term_single) {
echo '<a class="icon-hv-link" href="' . esc_url( $term_link ) . '"><i class="icon-left-open-big"></i><span>' . $term_single->name . '</span></a>';
}
?>
I was previously doing this which does work however its displaying every taxonomy term rather than the one specific to the post, so it doesn't work :(
<?php $terms = get_terms( 'resourcecategory' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
echo '<a class="icon-hv-link" href="' . esc_url( $term_link ) . '"><i class="icon-left-open-big"></i><span>' . $term->name . '</span></a>';
}
}?>
Does anyone have any idea on someway to combine the two?
For anyone else having an issue with this I managed to achieve what I was after with the following code:
<?php
$terms = get_the_terms( $post->ID, 'resourcecategory');
foreach($terms as $term) {
echo '<a class="icon-hv-link" href="' . get_term_link($term) . '"><i class="icon-left-open-big"></i><span>' . $term->name . '</span></a>';
}
?>
You need to use get_the_terms instead of get_terms. As mentioned in the comments, dont use wp_get_post_terms as this causes unnecessary calls to the database

Display a list of taxonomies, only if they are hierarchical

I am trying to show a list of the taxonomies a custom post type is in, excluding those which aren't hierarchical.
The code below currently works but shows all taxonomies, however I can't get it to check if the taxonomy is hierarchical before running it through the loop.
I know there is a WordPress function that runs this check, but as I usually work front-end, I can't seem figure out where to put it in order for it to take effect:
is_taxonomy_hierarchical( $taxonomy )
The following is the function I am using to output a list of the taxonomies:
// get taxonomies terms links
function custom_taxonomies_terms_links(){
// get post by post id
$post = get_post( $post->ID );
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
echo '<a href="';
echo '/';
echo '">';
echo 'Home';
echo "</a> / ";
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if (!empty( $terms )) {
foreach ( $terms as $term ) {
$out[] =
'<a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a> / ";
}
$out[] = " ";
}
}
return implode('', $out );
}
If I understand your correctly, couldn't you just test the taxonomy like the following:
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy->hierarchical) {
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if (!empty( $terms )) {
foreach ( $terms as $term ) {
$out[] =
'<a href="'
. get_term_link( $term->slug, $taxonomy_slug ) .'">'
. $term->name
. "</a> / ";
}
$out[] = " ";
}
}
}
When you use 'objects' as the second parameter in:
get_object_taxonomies( $post_type, 'objects' );
what you get back is an array of taxonomy objects, as opposed to just taxonomy names (the other option). The taxonomy object has a property "hierarchical" that indicates whether that taxonomy is hierarchical or not. You can test this to choose the types of taxonomies (hierarchical or not) that you want.

wordpress get_the_terms not work

this is my query to show my work in the portfolio
<?php
// The Query
$the_query = new WP_Query( array( 'post_type'=> 'portfolio' ) );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$medium = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$url_thumb = $thumb['0'];
$url_medium = $medium['0'];
$option = '<li>';
$option .= '<a data-value="' . get_the_terms($post->ID, 'portfolio' ) . '" data-largesrc="' . $url_medium .'" data-title="' . get_the_title() .'" data-description="' . get_the_content() .'">';
$option .= '<img src="' . $url_thumb . '" alt="img01" />';
$option .= '</a>';
$option .= '</li>';
echo $option;
}
} else {
}
/* Restore original Post Data */
wp_reset_postdata();
?>
The problem is here, in the data-value I need to extract the category of work
data-value="' . get_the_terms( 'portfolio', $post->ID ) . '"
I think the code that I use is wrong because if I put online I truncates the code and I do not show anything
Your code suggests that portfolio is a custom post type, not a custom taxonomy, but you are passing it as the taxonomy parameter for get_the_terms(). These are not the same things - post types are types of content (e.g. posts, pages) and taxonomies are ways to organize and group things (e.g. tags, categories).
You need to pass the slug of the custom taxonomy as the $taxonomy parameter, not the slug of the custom post type portfolio. I don't know what taxonomy you are querying but it is probably something like portfolio_categories or similar. For example if you were using the default category taxonomy with the post you would want get_the_terms($post->ID, 'category');
You are using wrong syntax, the right syntax is
<?php get_the_terms( $id, $taxonomy ); ?>

Categories