how to get parent category id in wordpress - php

how to get only parent category ids. not children category ids
i tried this code before which is showing me all ids of categories
<?php $category_ids = get_all_category_ids();
foreach($category_ids as $cat_id) {
$cat_name = get_cat_name($cat_id);
//echo '<span class="png_bg category_icon"></span>' . $cat_name ;
?>
<option><?php echo '<span class="png_bg category_icon"></span>' . $cat_name ; ?></option>
<?php
}
?>
</select>

There can be done in a number of ways. One of them is
$categories = get_categories();
foreach ($categories as $cat)
{
// if it is a topmost category , it has no parents, ie parent=0
if($cat->parent < 1)
{
echo $cat->category_nicename
echo $cat->cat_name ;
}
}

Here is the function:
<?php get_category_parents($cat); ?>
This function accepts several arguments, you can find the full reference on wordpress codex

Related

Only display Woo Commerce sub-categories based on selected Parent Category in a wordpress widget sidebar

I have a WordPress sidebar that I want to only display the children of the parent category. My structure is as follows
All Products > Parent Category > Children Categories
How do I get the sidebar to only display the Parent Category and Children Categories.
I read a solution to implement this code but I'm not sure where to put it inside the files.
<?php
if (is_category()) {
$cat = get_query_var('cat');
$this_category = get_category($cat);
$this_category = wp_list_categories('hide_empty=0&hierarchical=true&orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if($this_category !='<li>No categories</li>')
{
echo '<h3>Products</h3>';
echo '<ul>'.$this_category.'</ul>';
}
}
?>
These are screenshots from what my shop looks like and what I want.
Create a shortcode in your theme's functions.php file as follows:
add_shortcode('child_category_list', 'get_child_category_list');
function get_child_category_list(){
ob_start();
// Only on product parent category pages
if( is_product_category() ) {
$parent = get_queried_object();
$categories = get_term_children( $parent->term_id, 'product_cat' );
if ( $categories && ! is_wp_error( $categories ) ) :
echo '<ul>';
echo '<li>';
echo ''.$parent->name.'';
echo '<ul>';
foreach($categories as $category) :
$term = get_term( $category, 'product_cat' );
echo '<li>';
echo '<a href="'.get_term_link($term).'" >';
echo $term->name;
echo '</a>';
echo '</li>';
endforeach;
echo '</ul>';
echo '<li>';
echo '</ul>';
endif;
}
return ob_get_clean();
}
Then, go to Appearance > Widgets, grab either shortcode or text widget into your sidebar and paste following shortecode:
[child_category_list]
Tested on localhost & its working fine.

How to show only Parent Category name in single.php? (wordpress)

How to print only Parent Category name in single.php? (wordpress)
My structure:
Category A
-SubCategory X
The post (article) is posted only to Subcategory X.
I want to print only the name of "Category A".(without html tag).
I think you try this code:
for me this is working............
<?php $parentscategory ="";
foreach((get_the_category()) as $category) {
if ($category->category_parent == 0) {
$parentscategory .= ' ' . $category->name . ', ';
}
}
echo substr($parentscategory,0,-2); ?>
<?php
$get_parent_cats = array(
'parent' => '0' //get top level categories only
);
$all_cats = get_categories( $get_parent_cats );
foreach( $all_categories as $single_category ){
$catID = $single_category->cat_ID;
}
?>
echo '<li>' . $single_category->name . ''

Wordpress: How to add commas in this get_the_category function

PHP
<?php
$categories = get_the_category();
foreach ($categories as $category){
echo $category->cat_name;}
?>
Currently this will display categories as "cat1cat2cat3"
What I want it to be is "cat1, cat2, cat3"
I tried this echo $category->cat_name . ', '; but this just adds a comma after every category. Even if the post just has one category: Ex. "cat1, " And it also adds commas to the last category in the list: Ex. "cat1, cat2, cat3, "
So how can I get the commmas in but absent if just one category and absent on the last category if its a list?
You can also use implode() for that:
$categories = get_the_category();
$category_names = array();
foreach ($categories as $category)
{
$category_names[] = $category->cat_name;
}
echo implode(', ', $category_names);
Try this: (As of PHP 5.3)
$categories = array_map(function($category) {
return $category->cat_name;
}, get_the_category());
echo implode(', ', $categories);
you can do something like:
<?php
$categories = get_the_category();
$cat = '';
foreach ($categories as $category){
$cat .= $category->cat_name . ', ';
}
$cat = substr($cat,0,-2);
echo $cat;
?>
best regards,

in Wordpress display all categories for post in loop

I'm looking for a better way to output a list of categories for a post in the loop. This is what I have:
<?php $category = get_the_category(); ?>
<?php echo $category[0]->cat_name; ?>,
<?php echo $category[1]->cat_name;?>,
<?php echo $category[2]->cat_name;?>
Obviously thats not great because if there's not three categories I'll get redundant commas. What is the best way to loop through and output categories, with commas in between?
Thanks very much
This code should work:
<?php
$separator = ',';
$output = '';
$categories = get_the_category();
if ($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>
More info and examples about get_the_category() function on wordpress codex
you can use
<?php echo get_the_category_list(); ?>
which will output everything in this format(as a list):
<ul class="post-categories">
<li>
Business
</li>
</ul>
you can read more about that here
or alternatively if you use
<?php wp_get_post_categories( $post_id, $args ); ?>
it will output the category ids as an array
so something like
$post_categories = wp_get_post_categories( $post->ID );
foreach($post_categories as $c){
$cat = get_category( $c );
$link = get_category_link( $c );
echo "<a href='{$link}'>{$cat->name}</a>";
}
might work for you better
you can read more about this function here

Wordpress get category link

I want to ask on how to get the link of each category that have related post on it. I only got some code that will only display parent category. Any help would be much appreciated. Thanks!
<?php
$categories = get_categories();
foreach ($categories as $cat){
if($cat->parent < 1){
echo $cat->cat_name ;
}
}
?>
Sounds like you may want get_category_link - something like:
$categories = get_categories();
foreach ($categories as $cat) {
$category_link = get_category_link($cat->cat_ID);
echo '' . esc_html($cat->name) . '';
}
should print out the links to the categories for you.
According to Function Reference/get category link
<?php get_category_link( $category_id ); ?>
Example:
<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Category Name' );
// Get the URL of this category
$category_link = get_category_link( $category_id );
?>
<!-- Print a link to this category -->
Category Name

Categories