Part 2 Learning Objectives
- Master author-related template tags
- Work with categories and tags
- Handle media and attachments
- Use custom fields and post metadata
- Implement conditional tags for post data
👤 Author Tags
the_author() / get_the_author()
the_author()
Displays the author's display name.
<?php
printf('By %s', get_the_author());
?>
the_author_meta()
the_author_meta( $field, $user_id )
Displays specific author metadata.
<?php
the_author_meta('description');
the_author_meta('user_email');
the_author_meta('user_url');
?>
the_author_posts_link()
the_author_posts_link()
Displays a link to the author's archive.
<?php the_author_posts_link(); ?>
// Output: <a href="/author/john/">John Doe</a>
get_avatar()
get_avatar( $id_or_email, $size, $default, $alt )
Retrieves author's avatar image.
<?php
echo get_avatar(
get_the_author_meta('ID'),
96
);
?>
🏷️ Category and Tag Functions
the_category()
the_category( $separator, $parents, $post_id )
Displays post categories as links.
<?php the_category(', '); ?>
// Output: News, Technology, Featured
get_the_category()
get_the_category( $post_id )
Returns array of category objects.
<?php
$categories = get_the_category();
foreach($categories as $cat) {
echo $cat->name . ' ';
}
?>
the_tags()
the_tags( $before, $sep, $after )
Displays post tags with custom formatting.
<?php
the_tags('Tags: ', ' | ', '');
?>
get_the_term_list()
get_the_term_list( $post_id, $taxonomy, $before, $sep, $after )
Gets terms from any taxonomy.
<?php
echo get_the_term_list(
get_the_ID(),
'custom_taxonomy',
'Terms: ',
', '
);
?>
🖼️ Media and Attachment Tags
the_post_thumbnail()
the_post_thumbnail( $size, $attr )
Displays the featured image.
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('large', array(
'class' => 'featured-image',
'alt' => get_the_title()
));
}
?>
get_the_post_thumbnail_url()
get_the_post_thumbnail_url( $post, $size )
Gets URL of featured image.
<?php
$img_url = get_the_post_thumbnail_url(
get_the_ID(),
'full'
);
?>
<div style="background-image: url(<?php echo esc_url($img_url); ?>)"></div>
wp_get_attachment_image()
wp_get_attachment_image( $attachment_id, $size, $icon, $attr )
Gets HTML for an attachment image.
<?php
echo wp_get_attachment_image(
123,
'medium',
false,
array('class' => 'custom-image')
);
?>
get_attached_media()
get_attached_media( $type, $post )
Gets all attached media of specific type.
<?php
$images = get_attached_media('image');
foreach($images as $image) {
echo wp_get_attachment_image($image->ID);
}
?>
Continue Learning