wordpress get_the_terms not work - php

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

Related

Shortcode - Taxonomy terms list with ACF term icon

I'm trying to create a [shortcode] that will display taxonomy terms of a post in a list with an icon.
I am using ACF for the icon with meta book_icon, then trying to call it into the shortcode following the ACF forum post here.
This is my code so far:
add_shortcode( 'show-books', 'books_output' );
function books_output($atts){
ob_start();
echo '<ul class="books-terms">';
global $post;
$taxonomy = 'books';
$terms = get_the_terms( $post, $taxonomy );
$image = get_field('book_icon');
if ( !empty( $terms ) ) {
foreach ($terms as $term) {
echo '<li><img src="' . $image['url'] . '" class="book-css-class">' . $term->name . '</li>';
}
}
echo '</ul>';
$output = ob_get_clean();
return $output;
}
I'm getting the taxonomy terms in a list, but not the image icon.
Where am I going wrong?

Get image inside a post using wp_get_recent_posts

I am trying to create a a page in wordpress where I show 5 recent posts.
I was able to successfully do that.
However, I am not able to retrieve images that are present inside that post.
I am using this code to retrieve post url and post image. (Post URL works fine!)
$args = array('numberposts'=>'5');
$recentposts = wp_get_recent_posts($args);
foreach($recentposts as $post){
$v = $post['ID'];
$postlink = get_permalink($v);
$postimg = get_the_post_thumbnail($v);
}
try below code
$posts = get_posts( array( 'posts_per_page' => 5 ) );
foreach ( $posts as $_post ) {
if ( has_post_thumbnail( $_post->ID ) ) {
echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
echo '</a>';
}
}

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

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.

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.

Categories