Using WordPress ‘get_post_format_strings()’ PHP function

The get_post_format_strings() WordPress PHP function returns an array of post format slugs to their translated and pretty display versions.

Usage

$formats = get_post_format_strings();
print_r($formats);

Output:

Array (
    [aside] => Aside
    [gallery] => Gallery
    [link] => Link
    [image] => Image
    [quote] => Quote
    [status] => Status
    [video] => Video
    [audio] => Audio
    [chat] => Chat
)

Parameters

  • None

More information

See WordPress Developer Resources: get_post_format_strings()

Examples

Display a dropdown of post formats

This code creates a dropdown menu with all available post formats.

$formats = get_post_format_strings();

echo '<select name="post_format">';
foreach ($formats as $slug => $name) {
    echo '<option value="' . $slug . '">' . $name . '</option>';
}
echo '</select>';

Display a list of supported post formats

This code displays a list of post formats supported by the current theme.

$supported_formats = get_theme_support('post-formats');
$formats = get_post_format_strings();

if ($supported_formats) {
    echo '<ul>';
    foreach ($supported_formats[0] as $format) {
        echo '<li>' . $formats[$format] . '</li>';
    }
    echo '</ul>';
}

Add custom CSS classes based on post format

This code adds custom CSS classes to post elements based on their format.

$post_format = get_post_format();
$formats = get_post_format_strings();
$format_name = isset($formats[$post_format]) ? $formats[$post_format] : '';

echo '<div class="post ' . $format_name . '">';
// Your post content here
echo '</div>';

Display posts grouped by format

This code groups and displays posts by their post format.

$formats = get_post_format_strings();

foreach ($formats as $slug => $name) {
    $query = new WP_Query(array('post_type' => 'post', 'tax_query' => array(array('taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-' . $slug))));

    if ($query->have_posts()) {
        echo '<h2>' . $name . '</h2>';
        echo '<ul>';
        while ($query->have_posts()) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
    }
    wp_reset_postdata();
}

Add post format icon

This code adds an icon to each post based on its post format.

$post_format = get_post_format();
$formats = get_post_format_strings();
$format_icon = '';

switch ($post_format) {
    case 'video':
        $format_icon = '📹';
        break;
    case 'audio':
        $format_icon = '🎧';
        break;
    case 'image':
        $format_icon = '📷';
        break;
    // Add more cases for other formats
}

if ($format_icon) {
    echo '<div class="post-format-icon">' . $format_icon . '</div>';
}
// Your post content here