Wordpress get category link - php

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

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.

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

List only child categories a post is in, of a specific parent category

Either this is harder than it needs to be or I am just not understanding WordPress/PHP very well :( All I want to do is show the child/sub categories of a specific parent category...but only if the post is in those subcategories. Specific example:
I am building a wine reviews website and these are the categories:
BrandSubcategory 1Subcategory 2etc.
RegionSubcategory 1Subcategory 2etc.
GrapeSubcategory 1Subcategory 2etc.
The parent categories will never change, and every post will have at least 1 subcategory selected under each parent, so in the LOOP I can just list the parents by name. But I am needing to dynamically output the subcategories, something like this:
Brand: <?php list_post_subcategories('brand'); ?>
Region: <?php list_post_subcategories('region'); ?>
Grape: <?php list_post_subcategories('grape'); ?>
Is there any easy way like this? It seems like this should be a basic function in Wordpress? I've looked at the functions 'get_categories' and 'in_category' but they don't seem to be able to do this.
<?php $post_child_cat = array();
foreach((get_the_category()) as $cats) {
$args = array( 'child_of' => $cats->cat_ID );
$categories = get_categories( $args );
if( $categories ) foreach( $categories as $category ) {
echo $category->cat_name; }
} ?>
try this
I posted to Wordpress Answers to get more help and #Milo gave a great code solution:
// get top level terms
$parents = get_terms( 'category', array( 'parent' => 0 ) );
// get post categories
$categories = get_the_terms( $post->ID, 'category' );
// output top level cats and their children
foreach( $parents as $parent ):
// output parent name and link
echo '' . $parent->name . ': ';
// initialize array to hold child links
$links = array();
foreach( $categories as $category ):
if( $parent->term_id == $category->parent ):
// put link in array
$links[] = '' . $category->name . '';
endif;
endforeach;
// join and output links with separator
echo join( ', ', $links );
endforeach;
You can use array_map so you can return only the categories that you want. For example:
array_map( function( $cat ) {
if ( $cat->parent != 0 ) {
return $cat;
}
},
get_the_category()
);

how to get parent category id in wordpress

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

wordpress - display subcategory siblings when viewing a single post

Thank you all for a great site. I'm learning a lot. I'm trying to get my subcategory siblings to show when a single post is clicked on. I have set up my parent categories using the wordpress menu. I am using a php widget to call for the children (subcategories) in a separate menu (and then style with CSS). The code I'm using is showing the specific (relevant) children when each category is clicked on; however I am unable to figure out how to make them appear when viewing a post.
<?php
if(is_category()) {
$breakpoint = 0;
$thiscat = get_term( get_query_var('cat') , 'category' );
$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
if(empty($subcategories) && $thiscat->parent != 0) {
$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
}
$items='';
if(!empty($subcategories)) {
foreach($subcategories as $subcat) {
if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
$items .= '
<li class="cat-item cat-item-'.$subcat->term_id.$current.'">
'.$subcat->name.'
</li>';
}
echo "<ul>$items</ul>";
}
unset($subcategories,$subcat,$thiscat,$items);
}
?>
I'm attempting to mimic the behavior of this menu at pioneer woman.com
Any help or a better solution would greatly be appreciated.
Thanks,
This lists all child categories of the current post's category:
<?php
echo '<ul>';
$post_child_cat = array();
foreach((get_the_category()) as $cat) {
$args = array( 'child_of' => $cat->cat_ID );
$categories = get_categories( $args );
if( $categories ) foreach( $categories as $category ) {
echo '<li class="cat-item cat-item-'.$category->term_id.'">'.
'<a title="'.$category->description.'" href="';
echo bloginfo('url');
echo '/category/'.$cat->slug.'/'.$category->slug.'">'.
$category->name.'</a></li>';
}
}
echo '</ul>';
?>

Categories