Bootstrap Templates Bundle

Tuesday, September 3, 2013

How to show category name by category id in wordpress

Here is will show you some way to get category name and link from category ID

/// Category link  from category Name

<?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 -->
<a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>


WordPress – Get Category Link By Category Id

<?php
echo get_category_link( "category_id_here" );
?>

WordPress – Get Category name By Category Id

<?php echo get_the_category_by_id(10); ?>

Yellow color color code some shows a error "Catchable fatal error: Object of class stdClass could not be converted to string in ..."

if showing Catchable fatal error:  then use bellow for category name 

<?php
$cat=1;
$yourcat = get_category($cat);
if ($yourcat) {
echo '<h2>' . $yourcat->name . '</h2>';
}
?>




No comments:

Post a Comment

Wokiee React eCommerce Template