Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sumber: https://dbarnwal.wordpress.com/2014/07/23/display-category-name-in-post-page/
- Many times we need to display category name in post page . I have mentioned little code to display category name in post page, blog page.
- Display the first category name only:
- <?php
- $category = get_the_category();
- echo $category[0]->cat_name;
- ?>
- For more then one category name:
- <?php
- foreach((get_the_category()) as $category) {
- echo $category->cat_name . ' ';
- }
- ?>
- Make the first category link to the category page
- <?php
- $category = get_the_category();
- if($category[0]){
- echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
- }
- ?>
- get the post categories from outside the loop:
- <?php
- global $post;
- $categories = get_the_category($post->ID);
- var_dump($categories);
- ?>
- Display all categories as Links:
- <?php
- $categories = get_the_category();
- $separator = ' ';
- $output = '';
- if($categories){
- foreach($categories as $category) {
- $output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
- }
- echo trim($output, $separator);
- }
- ?>
- Display category images:
- <?php
- foreach((get_the_category()) as $category) {
- echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement