Using WordPress ‘get_post_format_string()’ PHP function

The get_post_format_string() WordPress PHP function returns a pretty, translated version of a post format slug.

Usage

$pretty_post_format = get_post_format_string( $slug );

Example: If $slug is ‘aside’, the function returns ‘Aside’.

Parameters

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

More information

See WordPress Developer Resources: get_post_format_string

Examples

Display the pretty format name of a post

This example gets the post format of a specific post and then displays its pretty name.

$post_id = 42; // Replace with your post ID
$slug = get_post_format( $post_id );
$pretty_format = get_post_format_string( $slug );
echo 'Post format: ' . $pretty_format;

Get and display all supported post formats

This example retrieves all supported post formats and displays their pretty names.

$post_formats = get_theme_support( 'post-formats' );
if ( $post_formats ) {
    foreach ( $post_formats[0] as $format ) {
        $pretty_format = get_post_format_string( $format );
        echo 'Supported post format: ' . $pretty_format . '<br>';
    }
}

Filter posts by a specific format

This example retrieves all posts with a specific format (e.g., ‘gallery’) and displays their titles and pretty format names.

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => 'post-format-gallery',
        ),
    ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<h2>' . get_the_title() . '</h2>';
        echo 'Post format: ' . get_post_format_string( get_post_format() ) . '<br>';
    }
}

Display post format in archive pages

This example displays the pretty post format name in archive pages.

if ( is_tax( 'post_format' ) ) {
    $term = get_queried_object();
    $pretty_format = get_post_format_string( $term->slug );
    echo 'Post format: ' . $pretty_format;
}

Add post format names to the post class

This example adds the pretty post format name to the post class attribute, allowing for CSS styling based on the post format.

function add_post_format_to_post_class( $classes ) {
    if ( get_post_format() ) {
        $classes[] = 'format-' . get_post_format_string( get_post_format() );
    }
    return $classes;
}
add_filter( 'post_class', 'add_post_format_to_post_class' );