WP_Error in a shortcode to show terms - wordpress

I'm using Anywhere Elementor to design a taxonomy archive template. I use this shortcode to show all the term
// Add Shortcode
function categorias_shortcode() {
$taxonomyName = "families";
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false, 'suppress_filters' => false));
echo '<ul style="list-style: none; margin: 0; color: white !important;">';
foreach ($parent_terms as $pterm) {
$terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
echo '<li><a style="color: white !important;" href="' . get_term_link( $pterm->name, $taxonomyName ) . '">' . $pterm->name . '</a></li>';
foreach ($terms as $term) {
echo '<li style="margin-left: 20px;"><a style="color: white !important;" href="' . get_term_link( $term->term_id, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
}
echo '</ul>';
}
add_shortcode( 'categorias', 'categorias_shortcode' );
And it worked perfectly until I tried to translate this page. WordPress give me this error:
Recoverable fatal error: Object of class WP_Error could not be converted to string in /usr/home/lloretensejove.cat/web/wp-content/themes/generatepress-child/functions.php on line 129
This is line 129:
echo '<li><a style="color: white !important;" href="' . get_term_link( $pterm->name, $taxonomyName ) . '">' . $pterm->name . '</a></li>';
I don't know how to fix this error.

I solved this error just changing this $pterm->name to this $pterm->term_id on the line:
echo '<li><a style="color: white !important;" href="' . get_term_link( $pterm->term_id, $taxonomyName ) . '">' . $pterm->name . '</a></li>';

Related

Custom type - display all taxonomy name and image

I need to display on my single page, each taxonomy name and image.
i have 10 different image for taxonomy : 'brique'
It's ok for the name but i can't display image
For the image
<?php
$tax = 'brique';
$terms = get_terms( $tax, $args = array(
'hide_empty' => false, // do not hide empty terms
));
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('visuel' . $term_id );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
Try the below code. first, you need to pass taxonomy Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array: you can check here get_terms()
in get_field you have pass second parameter as term_id or $term object
<?php
$tax = 'brique';
$terms = get_terms( array(
'taxonomy' => $tax,
'hide_empty' => false,
) );
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field( 'visuel', $term );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>

Taxonomy Term Image 'post_per_page' doesn't work

This code working with image and title but post_per_page doesn’t work and trying
number' =>1
is work but if any taxonomy Less than one Or equal to one Info will not be showing.
<?php
$tax = 'studio';
$types = get_terms( array(
'post_per_page' => 1,
)
);
foreach($types as $type) { ?>
<?php
$term_link = get_term_link( $type );
$image = get_field('studio_avatar', 'studio_' . $type->term_id . '' );
if ( has_term( $type->term_id, 'studio')) {
echo '<a class="author author-avt inline" href="' . esc_url( $term_link ) . '">';
echo '<img class="si-user" src="' . $image['url'] . '" /> ';
echo '<span>'. $type->name . '</span>';
echo '</a>';
echo '<span class="posted-on inline">';
echo '<i class="si-clock"></i><time>'. $post_date = get_the_date("j F Y") .'</time>';
echo '</span>';
}
?>
<?php } ?>

Show star ratings in comments

I followed the steps in this article: cssigniter.com/add-rating-wordpress-comment-system to add a star rating to the comments system... but when I list the comments with the code below the stars are not showing up I have tried what seems like a million things and I can not seem to figure out why the stars are not showing up.
Here is the code I am using to pull the comments
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'user_id' => $user_id );
$comments = get_comments($args);
foreach($comments as $comment) : echo '<p>';
$post_id = $comment->comment_post_ID;
$member_name = get_post( $comment->comment_post_ID );
echo ( ' <div style="color: #00205a;"> ' . mysql2date(get_option('date_format'), $comment->comment_date) . ' - </div>' . '<a style="color:#a27747;" href="' . get_permalink( $comment->comment_post_ID ) . '">' . $member_name->post_title . '</a><br />' . '(stars go here)' . '<br />' . $comment->comment_content ). '<br /><br />';
echo '</p>';
endforeach;
}
if ( is_user_logged_in() ) {
$user_id = get_current_user_id(); $args = array( 'status' => 'approve', 'user_id' => $user_id );
$comments = get_comments($args);
foreach($comments as $comment) : echo '<p>';
$member_name = get_post( $comment->comment_post_ID );
if ( $rating = get_comment_meta( $comment->comment_ID, 'rating', true ) ) {
$stars = '<p class="stars">';
for ( $i = 1; $i <= $rating; $i++ ) {
$stars .= '<span class="dashicons dashicons-star-filled"></span>';
}
$stars .= '</p>';
}
echo ( ' <div style="color: #00205a;"> ' . mysql2date(get_option('date_format'), $comment->comment_date) . ' - </div>' . '<a style="color:#a27747;" href="' . get_permalink( $comment->comment_post_ID ) . '">' . $member_name->post_title . '</a><br />'. $stars . '<br />' . $comment->comment_content ). '<br /><br />';
echo '</p>';
endforeach;
}

Get the_category outside of loop

I'm working on a site that has custom author pages and on the author pages it has a recent author posts widget which displays other posts by that author. This site has multiple authors so it is different for every post and I haven't found a plugin that does this. I'm trying to display the post thumbnail, title, and category name.
I'm using a function that is displaying the title and the thumbnail, but it doesn't have the category . I tried to add the category in with: the_category(' ','multiple',$authors_post->ID) unfortunately it displays all of the categories in the first li. Instead of for each post.
Here is what I'm working with:
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,
'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' )
. '<p>' . the_category(' ','multiple',$authors_post->ID)
. '</p> <a href="' . get_permalink( $authors_post->ID )
. '">' . apply_filters( 'the_title', $authors_post->post_title,
$authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';
return $output;
}
Any help would be greatly appreciated,
Thanks!
Please try this code,
<?php
$cat=1;
$yourcat = get_category($cat);
if ($yourcat)
{
echo '<h2>' . $yourcat->name . '</h2>';
}
?>
To get category name,
try some thing like
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$category = get_the_category($authors_post->ID);
foreach($category as $c){
$cat=$c->cat_name.' '.$cat;
}
$output .= '<li>' . get_the_post_thumbnail( $authors_post->ID, 'xsmall-thumb' ) . '<p>' . $cat . '</p> ' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</li>';
}
$output .= '</ul>';
return $output;
}
hope this work for you..

wordpress recent categories widget

I have used the following code to display the recent categories <?php wp_list_categories( 'title_li=<h3>' . __('Recent Categories') . '</h3>' ); ?>. I need to exclude some categories from display. How can i do that?
You might try using 'get_categories' instead. It's a bit more complicated, but a lot more flexible. You can exclude specific cats with this function by including a comma-separated list of categories in the args. See below:
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => '1,4,9' <--- Add your cats to exclude here
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
Read more about get_categories at http://codex.wordpress.org/Function_Reference/get_categories
?>

Resources