Using WordPress ‘get_post_format_link()’ PHP function

The get_post_format_link() WordPress PHP function returns a link to a post format index.

Usage

get_post_format_link( $format );

Example:

$post_format_link = get_post_format_link( 'gallery' );
echo $post_format_link; // Output: https://example.com/type/gallery/

Parameters

  • $format (string) – Required. The post format slug.

More information

See WordPress Developer Resources: get_post_format_link()

Examples

Display Post Format Link

Display the link to the ‘quote’ post format index.

Explanation:

This code snippet retrieves the link for the ‘quote’ post format and echoes it on the page.

$quote_link = get_post_format_link( 'quote' );
echo $quote_link; // Output: https://example.com/type/quote/

Create a Post Format Navigation Menu

Create a simple navigation menu with links to various post format indexes.

Explanation:

This code snippet creates an unordered list with links to different post format indexes like ‘image’, ‘video’, ‘audio’, and ‘aside’.

$formats = array( 'image', 'video', 'audio', 'aside' );
echo '<ul>';
foreach ( $formats as $format ) {
    $link = get_post_format_link( $format );
    echo "<li><a href='{$link}'>{$format}</a></li>";
}
echo '</ul>';

Display Post Format Links in a Dropdown

Create a dropdown menu with post format links.

Explanation:

This code snippet creates a dropdown menu with links to different post format indexes like ‘gallery’, ‘link’, ‘status’, and ‘chat’.

$formats = array( 'gallery', 'link', 'status', 'chat' );
echo '<select onchange="location = this.value;">';
echo '<option value="">Select Post Format</option>';
foreach ( $formats as $format ) {
    $link = get_post_format_link( $format );
    echo "<option value='{$link}'>{$format}</option>";
}
echo '</select>';

Generate Post Format Links with Custom Labels

Display post format links with custom labels.

Explanation:

This code snippet displays post format links with custom labels for the ‘quote’ and ‘image’ post formats.

$formats_with_labels = array(
    'quote' => 'Quotes',
    'image' => 'Images',
);

foreach ( $formats_with_labels as $format => $label ) {
    $link = get_post_format_link( $format );
    echo "<a href='{$link}'>{$label}</a><br>";
}

Display Post Format Links Only if They Have Posts

Display post format links only if they have at least one post.

Explanation:

This code snippet checks if a specific post format has at least one post before displaying its link.

$formats = array( 'image', 'video', 'audio', 'aside' );

foreach ( $formats as $format ) {
    $post_format_term = get_term_by( 'slug', 'post-format-' . $format, 'post_format' );
    if ( $post_format_term->count > 0 ) {
        $link = get_post_format_link( $format );
        echo "<a href='{$link}'>{$format}</a><br>";
    }
}