Hello,
i have a sidebar widget with a list of categories and posts.
I want to set every category title clickable to the category itself.
Where I'm supposed to insert this (or similar) code?
get_term_link($category,'portfolio_category');
In this:
// Output of the widget
function widget( $args, $instance ) {
global $theme_option;
$title = apply_filters( 'widget_title', $instance['title'] );
$category = $instance['category'];
$num_fetch = $instance['num_fetch'];
// Opening of widget
echo $args['before_widget'];
// Open of title tag
if( !empty($title) ){
echo $args['before_title'] . $title . $args['after_title'];
}
// Widget Content
$current_post = array(get_the_ID());
$query_args = array('post_type' => 'portfolio', 'suppress_filters' => false);
$query_args['posts_per_page'] = $num_fetch;
$query_args['orderby'] = 'post_date';
$query_args['order'] = 'desc';
$query_args['paged'] = 1;
$query_args['portfolio_category'] = $category;
$query_args['post__not_in'] = array(get_the_ID());
$query = new WP_Query( $query_args );
if($query->have_posts()){
echo '<div class="gdlr-recent-port2-widget">';
while($query->have_posts()){ $query->the_post();
$thumbnail = gdlr_get_image(get_post_thumbnail_id(), 'thumbnail');
if( !empty($thumbnail) ){
echo '<div class="recent-port-widget-thumbnail"><a href="' . get_permalink() . '" >' . $thumbnail . '</a></div>';
}
}
echo '<div class="clear"></div>';
echo '</div>';
}
wp_reset_postdata();
// Closing of widget
echo $args['after_widget'];
}
Andrea P answers:
this bit here:
// Open of title tag
if( !empty($title) ){
echo $args['before_title'] . $title . $args['after_title'];
}
I guess it would be changed to:
if( !empty($title) ){
echo $args['before_title'] .'<a href="'. get_term_link($category,'portfolio_category') .'"> '. $title .'</a> '. $args['after_title'];
}
in any case, I strongly suggest you to don't edit the core code of a plugin widget.
you could use this plugin here instead (which happens to be mine..):
https://wordpress.org/plugins/wp-list-pages-by-custom-taxonomy/
then instead of putting a Title for the widget, you manually place an heading with the name and link to the category which you are displaying. you can do so, by adding a bit of html into the field "introduction html/text" within the widget options.
Andrea P comments:
here is a thread where I explain to another person how to do with my plugin exactly what you are asking.. ;)
https://wordpress.org/support/topic/option-for-heading-to-link-to-taxonomyterm
noronha comments:
Hi! Thank you very much for your answer.
I tried to set the code you provided, it works but at the same time it makes the footer wrong (without colour, etc etc). I have seen your plugin and I would use it but in a different site, because in this site I already have many plugins and I would limit its use. It's not really possible to find a different code for the $title
of the category in order to set it clickable without another plugins?
Andrea P comments:
mmm..
if it mess up the styles or the html of the elements below the widget (i.e. the footer), means that there is a typo and the html of the widget output is broken.
the string I provided seems correct to me.. are you sure you pasted it correctly? can I see the page to check the code from my browser inspector?
Andrea P comments:
I mean the page where the widget is displayed..
Arnav Joy answers:
do you also want to show title of the category ?
noronha comments:
Hi, yes. Category title clickable to the category page itself (the page with the list of the posts that have that category)
Arnav Joy comments:
so you want to show posts like this :-
Category1
post title1
post title2
Category2
post title1
post title2
...
...