add category name to post in archive page

I have this custom plugin below:

if ( function_exists(‘add_image_size’) )
{
$sizes = get_option(‘jlao_cat_post_thumb_sizes’);
if ( $sizes )
{
foreach ( $sizes as $id=>$size )
add_image_size( ‘cat_post_thumb_size’ . $id, $size[0], $size[1], true );
}
}
class CategoryPosts extends WP_Widget {
function CategoryPosts() {
parent::WP_Widget(false, $name=’Category Posts’);
}
function widget($args, $instance) {
global $post;
$post_old = $post; // Save the post object.

extract( $args );

$sizes = get_option(‘jlao_cat_post_thumb_sizes’);

// Get array of post info.
$cat_posts = new WP_Query(
“&cat=” . $instance[“cat”] .
“&orderby=” . $sort_by .
“&order=” . $sort_order
);
// Excerpt length filter
$new_excerpt_length = create_function(‘$length’, “return ” . $instance[“excerpt_length”] . “;”);
if ( $instance[“excerpt_length”] > 0 )
add_filter(‘excerpt_length’, $new_excerpt_length);

echo $before_widget;

// Widget title

echo $before_title;
echo $instance[“title”];
echo $after_title;
// Post list

echo ‘<div class=”row”>’;

echo ‘<ul class=”large-offset-1 inner-content grid-x grid-margin-x grid-padding-x large-centered medium-centered small-centered”>’;
// echo ‘<div class=”large-10″>’;

while ( $cat_posts->have_posts() )
{
$cat_posts->the_post();
?>

<li id=”<?php echo $instance[” cat “]; ?>” class=”small-3 small-centered medium-3 medium-centered large-3 large-centered colum columns cat-post-item <?php echo $instance[”
cat “]; ?>
<?php the_field(‘format’); ?>” data-permanlink=”<?php the_permalink() ?>”>

<?php echo get_the_category( $id )[0]->name; ?>

<?php

function_exists(‘the_post_thumbnail’) &&
current_theme_supports(“post-thumbnails”) &&
$instance[“thumb”] &&
has_post_thumbnail()

?>
<a href=”<?php the_permalink(); ?>” title=”<?php the_title_attribute(); ?>”>
<?php the_post_thumbnail( ‘cat_post_thumb_size’.$this->id ); ?>
</a>

<a class=”post-title” href=”<?php the_permalink(); ?>” rel=”bookmark” title=”Permanent link to <?php the_title_attribute(); ?>”>
<?php the_title(); ?>
</a>

<?php the_excerpt(); ?>

</li>
<?php
}

echo “</ul>n”;
// echo “</div>n”;

echo $after_widget;
remove_filter(‘excerpt_length’, $new_excerpt_length);

$post = $post_old; // Restore the post object.
}
/**
* Form processing… Dead simple.
*/
function update($new_instance, $old_instance) {
/**
* Save the thumbnail dimensions outside so we can
* register the sizes easily. We have to do this
* because the sizes must registered beforehand
* in order for WP to hard crop images (this in
* turn is because WP only hard crops on upload).
* The code inside the widget is executed only when
* the widget is shown so we register the sizes
* outside of the widget class.
*/
if ( function_exists(‘the_post_thumbnail’) )
{
$sizes = get_option(‘jlao_cat_post_thumb_sizes’);
if ( !$sizes ) $sizes = array();
$sizes[$this->id] = array($new_instance[‘thumb_w’], $new_instance[‘thumb_h’]);
update_option(‘jlao_cat_post_thumb_sizes’, $sizes);
}

return $new_instance;
}
/**
* The configuration form.
*/
function form($instance) {
?>
<p>
<label for=”<?php echo $this->get_field_id(” title “); ?>”>
<?php _e( ‘Title’ ); ?>:
<input class=”widefat” id=”<?php echo $this->get_field_id(” title “); ?>” name=”<?php echo $this->get_field_name(” title
“); ?>” type=”text” value=”<?php echo esc_attr($instance[” title “]); ?>” />
</label>
</p>

<p>
<label>
<?php _e( ‘Category’ ); ?>:
<?php wp_dropdown_categories( array( ‘name’ => $this->get_field_name(“cat”), ‘selected’ => $instance[“cat”] ) ); ?>
</label>
</p>

<?php
}
}
add_action( ‘widgets_init’, create_function(”, ‘return .
register_widget(“CategoryPosts”);’) );
?>

I am calling the category id by in my post

the loop being:

echo ‘<ul class=”large-offset-1 inner-content grid-x grid-margin-x grid-padding-x large-centered medium-centered small-centered”>’;
// echo ‘<div class=”large-10″>’;

while ( $cat_posts->have_posts() )
{
$cat_posts->the_post();
?>

<li id=”<?php echo $instance[” cat “]; ?>” class=”small-3 small-centered medium-3 medium-centered large-3 large-centered colum columns cat-post-item <?php echo $instance[”
cat “]; ?>
<?php the_field(‘format’); ?>” data-permanlink=”<?php the_permalink() ?>”>

<?php echo get_the_category( $id )[0]->name; ?>

<?php

function_exists(‘the_post_thumbnail’) &&
current_theme_supports(“post-thumbnails”) &&
$instance[“thumb”] &&
has_post_thumbnail()

?>
<a href=”<?php the_permalink(); ?>” title=”<?php the_title_attribute(); ?>”>
<?php the_post_thumbnail( ‘cat_post_thumb_size’.$this->id ); ?>
</a>

<a class=”post-title” href=”<?php the_permalink(); ?>” rel=”bookmark” title=”Permanent link to <?php the_title_attribute(); ?>”>
<?php the_title(); ?>
</a>

<?php the_excerpt(); ?>

</li>
<?php
}

echo “</ul>n”;

but instead I woudl like to call the name of the category instead of the category ID. How is this possible in that particular example ?

Thanks four for all

Read more here:: add category name to post in archive page

Leave a Reply

Your email address will not be published. Required fields are marked *