Show specific categories in Wordpress - wordpress

I have more than 800 categories, because 1 post may have more more than 10. I want to display only X categories on front page. How I can do this?

$id = get_the_ID();
$cats = wp_get_post_categories($id);
$i=0;
foreach ( $cats as $cat ){
if($i <10){
//Dosomething with $cat
}
$i++;
}
By setting a counter you could show desired categories

Related

Display the category names for the current post, but exclude one ID

Im trying to display the current post category name for a custom post types' taxonomy, but exclude one of the categories. I see different solutions in StackOverflow, but I can't seem to get any of them to work for me. Below is the code I am using, which works great, but I don't know how to exclude one ID using it.
<?php $terms = get_the_terms( $post->ID , 'press_category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'press_category' );
if( is_wp_error( $term_link ) )
continue;
echo $term->name ;
}
?>
Try this
<?php $terms = get_the_terms($post->ID, 'press_category');
foreach ($terms as $term)
{
$term_link = get_term_link($term, 'press_category');
if (is_wp_error($term_link)) continue;
if ($term->term_id != 222)
{
echo $term->name;
}
}
?>
Change 222 in this line to your taxonomy id for exclude
$term->term_id != 222

How to remove duplicated results from a foreach loop

I'm trying to add some sidebar content to my WordPress search results page where the top categories and tags, related to the current search query, are displayed. I have been able to generate the content, but there are a massive amount of duplicates also displaying. Click for my example page.
I've tried two ways of doing this, both with no success. Both were pulled from previous threads with similar questions. I would prefer avoiding 3rd party plugins if possible. Any ideas would be appreciated. Thanks
Method #1:
function list_search_cats() {
// Start the Loop
$uniqueCats = array();
while ( have_posts() ) : the_post();
$cats = get_the_category();
if (! in_array($cats, $uniqueCats)) :
$uniqueCats[] = $cats;
foreach($cats as $cat) :
echo '<li><a class="tag" href="'.get_category_link($cat->cat_ID).'">' . $cat->cat_name . '</a></li>';
endforeach;
endif;
endwhile;
}
Method #2:
function list_search_cats() {
// Start the Loop
$uniqueCats = array();
while ( have_posts() ) : the_post();
$cats = get_the_category();
$cats = array_unique($cats, SORT_REGULAR);
foreach($cats as $cat) :
echo '<li><a class="tag" href="'.get_category_link($cat->cat_ID).'">' . $cat->cat_name . '</a></li>';
endforeach;
endwhile;
}
It looks to me like you're just missing the actual check of each category. You may need an extra foreach loop to check for dupes, then use that unique array in another foreach to display the categories. Try this:
function list_search_cats() {
// Array to put unique cats in
$uniqueCats = array();
while ( have_posts() ) : the_post();
$cats[] = get_the_category();
// first foreach to check each cat and put in to new array
foreach($cats as $cat) {
if(!in_array($cat, $uniqueCats)) {
$uniqueCats[] = $cat;
}
}
// second foreach to display the list
foreach($uniqueCats as $cat_li) {
$term = get_term_by('name', $cat_li, 'category');
echo '<li><a class="tag" href="'.get_category_link($term->term_id)).'">' . $cat_li . '</a></li>';
}
endwhile;
}

wordpress get taxonomy of a displayed item

I`m writing a wordpress theme. I need to display all parents of the current item that belongs to some taxonomy. The first thing I need to do is to get the taxonomy of the item that is displayed. Here what I tried to do:
$id = get_the_ID();
$taxonomy = get_term_by('id', $id)['taxonomy'];
echo 'Current taxonomy is ' . $taxonomy;
$terms = get_the_terms( $id, $taxonomy);
for($i = count( $terms ) - 1; $i >= 0; $i--){
echo '>' . $terms[$i]->name . '';
}
The first problem I`ve faced is $taxonomy = "". Please help me.
$id = get_the_ID();
$taxonomy = get_term_by('id', $id)['taxonomy'];
Tou are getting empty $taxonomy because you are trying to get term details using the id of a post and Not the term id. To get the terms associated with the post you have to do this:
$terms = wp_get_post_terms( $post_id, $taxonomy, $args );
you can get your terms by wp_get_post_terms for more informationclick here

Show Product category list in Woocommerce Wordpress Plugin without thumbnails

I am using the following short code to list the product categories in woocommerce Wordpress plugin
<?php echo do_shortcode('[product_categories number=""]'); ?>
The problem is the category thumbnail that I need to get rid of. I want to hide it and doing it with CSS seems to be a big hassle. Is there anyway I can list the categories without the thumbnails appearing?
You could use the following:
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);
if (count($terms) > 0) {
echo '<p class="my_term-archive">';
foreach ($terms as $term) {
echo '' . $term->name . '';
}
echo '</p>';
}
Heres what I used for making a list of all categories by letter excluding empty letters and with category featured images!
All you need to do is style it the way you want ;)
<?php
/* Define which category // to list child categories array('parent'=>42) 42 is category ID */
$designers = get_terms('product_cat', array('parent'=>42));
/* If categ not empty for each designer take first letter */
if ( !empty( $designers ) && !is_wp_error( $designers ) ){
$term_list = [];
foreach ( $designers as $designer ){
$letter = strtoupper($designer->name[0]);
$designer_list[$letter][] = $designer;
}
unset($designer);
foreach ( $designer_list as $key=>$value ) {
/* Wrap around each designer */
echo '<div><span>' . $key . '</span><ul>';
foreach ( $value as $designer ) {
// Set thumbnail
$thumbnail_id = get_woocommerce_term_meta( $designer->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
/* List designers */
echo '<li>' . $designer->name . '<img src=" ' .$image.' "" alt="" /></li>';
}
/* end Wrap around designer */
echo '</ul></div>';
}?>
}

Portfiolio Category filter Wordpress

I am building a Wordpress Portfolio with Quicksand filtering functionality.
The part I need help with is that I have about 20 categories and would like to split them into 3 rows, each with their own title - Sound, Design and Video.
Here is the code that I am using:
<?php
$terms = get_terms("tagportfolio");
$count = count($terms);
echo '<ul id="portfolio-filter">';
echo '<li>All</li>';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$termname = strtolower($term->name);
$termname = str_replace(' ', '-', $termname);
echo '<li>'.$term->name.'</li>';
}
}
echo "</ul>";
?>
If I understand correctly, "tagportfolio" is a custom, chierarchical taxonomy?
Than use:
<?php get_term_children( $ID_of_subcategory, 'tagportfolio') ?>
to get elements from $ID_of_subcategory.

Resources