wordpress recent categories widget - wordpress

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

Related

Page with category list with image (ACF)

I try to create list of child category with images thumbs.
I add new field to taxonomy (ACF plugin) “image_category” and now I try to add this image to the list:
<?php
$args = array('parent' => 3);
$categories = get_categories( $args );
$term = get_queried_object();
$image_category = get_field('image_category', $term);
foreach($categories as $category) {
echo '<div class="col-md-3 col-sm-12 col-xs-12 text-center pb-3"><img src="' . $image_category . '"><h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
echo '<p> <strong>Number post:</strong> '. $category->count . '</p></div>';
}
?>
I see category list, name, number post. Only image dont work.
Image link dont show. I have on consol only “img src=(unknown)”. What am I doing wrong?

WP_Error in a shortcode to show terms

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>';

How to display subcategories on click categories page wordpress categories

How to display sub categories on category page if category has subcategories else show posts of that categories wordpress category page
$id = $wp_query->get_queried_object_id(); // now you have the ID of the category
Now check if something is returned and do whatever from there:
$args = array('child_of' => $id);
$subcats = get_terms( $args );
if(!empty($subcats)){
foreach($subcats as $subcat) {
echo get_term_link( $subcat->slug, $subcat->taxonomy ); // for example
}
} else {
// do the usual stuff
}
You can try get_categories() with args child_of function like below.
$args = array('child_of' => 'category_id');
$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>';
}
Also, $category->count ===0 then category has no post. we can use it to check that category's post.

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 url structure

Is it possible in wordpress when I call example.com/category to have listed all available categories. If I request the url like in my example I'm getting an 404 page
I'm not sure if there is a default link to list all the categories.
But it doesn't mean you can't do it yourself. Create a new template file in your theme, name it for example category_list.php and add this code:
You might want to tweak it a bit to display it how you want.
<?php
/**
* Template Name: Category listing
* Description: list all the categories
*/
get_header(); ?>
<div class="container">
<?php
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$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>';
echo '<hr />';
endforeach;
?>
</div>
<?php get_footer(); ?>
Then go to Pages -> add new. Name the Page as "Category", so that the url will be example.com/category.
And in the template list select the template you just created. It will be named "Category listing" as you can see in the code above in the comments at the top.

Resources