I'm modifying Wordpress post formats and displaying them in a custom tab bar horizontally across the top of the Wordpress Post Editor page.
I'm trying to rename the labels in the array before they get printed.
Var_dump($format_string) outputs:
Using array_replace I try to change the labels:
$replacements = array(1 => "Code", 2 => "External", 3 => "Swap", 4 => "Horizontal", 5 => "Single" );
$post_types = array_replace($format_string, $replacements);
echo '<li>' . esc_html($post_types) . '</li>';
}
but the error is:
Warning: array_replace(): Argument #1 is not an array in ... on line 19
Full code:
foreach ($post_formats as $format) {
$class = ($format == $current_post_format || (empty($current_post_format) && $format == 'standard') ? 'current' : '');
if ($format == 'standard') {
$format_string = __('Standard', 'post-format');
$format_hash = 'post-format-0';
} else {
$format_string = get_post_format_string($format);
$format_hash = 'post-format-'.$format;
}
var_dump($format_string);
$replacements = array(1 => "Code", 2 => "External", 3 => "Swap", 4 => "Horizontal", 5 => "Single" );
$post_types = array_replace($format_string, $replacements);
echo '<li><a '.(!empty($class) ? 'class="'.esc_attr($class).'"' : '').' href="#'.esc_attr($format_hash).'">'.esc_html($post_types).'</a></li>';
}
var_dump($format_string)
produces a single stringstring(8) "Standard"
. instead of looping right away, use thearray_replace()
first to make replacements to the base, then loop the necessary replaced values after – kevinabelita 20 hours ago