You need to pass the category ID into get_category_link()
<a href="https://wordpress.stackexchange.com/questions/244678/<?php echo esc_url( get_category_link( $category[0]->term_id ) ); ?>"><?php echo $category[0]->cat_name; ?></a>
Here’s the whole thing put together
<?php
$query = new WP_Query( array(
'posts_per_page' => 2,
'categories_per_page' => 1,
) );
while ( $query->have_posts() ) : $query->the_post(); ?>
<li><a href="https://wordpress.stackexchange.com/questions/244678/<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
$category = get_the_category();
if ( ! empty( $category ) ) {
?>
<a href="https://wordpress.stackexchange.com/questions/244678/<?php echo esc_url( get_category_link( $category[0]->term_id ) ); ?>"><?php echo $category[0]->cat_name; ?></a>
<?php
}
endwhile;
?>
Related Posts:
- What’s the difference between WordPress random_int() and PHP built-in function random_int()?
- How to use the do_action () with parameter
- Issue with wp_get_attachment_image() and SVG file type
- deregister scripts on certain page
- Notify users whenever a new post is published based on their preference
- Changing a function in function.php to a shortcode – for listing categories of only a certain post type
- Formatting post content to exclude gallery
- Why do I get undefined function export_wp()?
- How to remove the embed_footer?
- Hide some items from Screen options in dashboard for products
- How to set global variables in template page?
- Setting featured image with function, 1 part not working
- Function to show only first instance of shortcode
- Comments to only be seen by their author
- Using the child theme functions.php to change the customizer.php on parent theme
- Can’t Update function.php after writing short code
- How to get last updated row or ID in wordpress
- Filtering posts by category name based on page’s slug
- adding custom classes for tables
- Load parent theme files before child theme functions.php
- 2 Loops, Only Displaying 1 Loop in Both Loops
- edit role display name and label name without plugins
- Two functions.php files in one theme
- wp_dropdown_categories and custom attribute
- Converting html to wordpress images not showing
- How can I add text to all posts/pages/categories/homepage etc
- Make parent page only link to first subpage
- Pulling Twitter RSS feed not working as expected (fatal error)
- Related post using post title
- How to validate this deprecated function
- Move Post to different category if post_meta field is 0 or is 2 days old?
- How can I modify all existing tags while keeping the urls themselves?
- Scripts not appending to element in AJAX call – why?
- Trigger a custom function when option are saved in admin area
- Find a Javascript ID when trying to deregister?
- Functions For Calling Specific Elements
- How would I add “edit profile” and “blog post” functions to front-end?
- How can I add a filter for specific categories on functions.php?
- Custom editor color palette colors are not appearing in the editor
- Set Taxonomy based on post status
- Is There Any Built In WP Functionality For Combining Form Fields To Return Specific Data
- How would I go about replacing this function in my child theme located in inc/template-tags.php
- Condition OR for current user ID
- wp_create_category deprecated?
- If user is logged-in display/hide something
- Visual Composer creating own shortcodes with vc_map() to return simple Image
- Using multiple line variable inside a function?
- Wp-query Order By problem
- WordPress Excerpt – How to remove the first link using functions.php
- Sortable column (by numbers) in admin users
- How can I add a class to a nav li depending on URL?
- Access category within rss2_head hook?
- Compare meta key to current date in pre get post
- Is there a built-in function to duplicate existing posts?
- load CSS only for administrators on backend
- Modifying the_posts_pagination from within functions.php instead of template files
- Woocommerce image sizes missing from Appearance › Customize but not declared by theme
- Prevent WordPress Automatic Logout
- Insert Address fields into function
- functions php file [closed]
- How to remove admin footer text from inside child theme?
- Problem with images URL after filter applying
- How add body_class from wp_termmeta
- Rewrite Question Mark in Post Template with URL Parameter for clean URLs
- Modify function to only return values for the user that’s logged in
- WordPress performance – send all metaboxes to function or to call it again
- Custom attributes to javascript tags
- How to add php within jquery
- Is it possible to put a function somewhere other than the functions.php file?
- wp_enqueue_script() not working
- Display a list of random terms from custom taxonomy with shortcode
- Overwrite text in a complicated filter hook
- WP_Query for Attachments not working as expected
- Text replace for the whole body tag
- Link product attribute value to a URL – woocommerce
- Deleting guests profile users after 1.5 hours
- wp enqueue style on about us page
- CF7 select value get to function and reciept value from function
- How set a while with a function
- Trying to get the path to my child-theme via my functions.php
- Remove action hook from Class, understanding OOP
- Create custom admin bar menu items
- Adding a class to the body of an inactive site using multisite
- Form output outside of container
- Redirect logged user depending on user role if he tries to access home page?
- Placing CSS/ JS into the header or footer (inline) – trying to apply to several templates
- How to automatically add custom classes to headings in content area
- Remove Featured Images from Posts Older Than a Certain Date
- How to list out all values from the get_body_class array
- Mail function not working with user accounts
- Function not recognizing page id?
- Conditionally write open graph meta property in header
- Pull random comment from specific post, display on homepage with shortcode
- Using array page name together with page id to deregister script
- Modifying Page-Links format
- Give a function a unique ID
- how to handle multiple forloop?
- Different ads on every website on multisite environment
- WordPress Custom wp mail template return full template
- How to get variable from other function inside class function using add_action for Ajax call