Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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:

enter image description here

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>';
}
share|improve this question
    
Works for me... –  Fabricator 20 hours ago
    
i think this is inside a loop, since each var_dump($format_string) produces a single string string(8) "Standard". instead of looping right away, use the array_replace() first to make replacements to the base, then loop the necessary replaced values after –  kevinabelita 20 hours ago
1  
i thought so, as the error says, the first argument must be an array not a string, manual, why not build the first array first, then make the replacement, then loop the results –  kevinabelita 20 hours ago
add comment

1 Answer

up vote 0 down vote accepted

Your format_string is like it says, a string. Vardump is showing the whole array because you're var dumping in a loop. If you var dump one iteration of the loop, it should show you 1 string.

Create and replace the array outside the look before echoing the LI one by one.

share|improve this answer
    
Never fails, it's always some simple overlooked solution for me. Derp derp :p –  dcc 20 hours ago
    
I know that feel bro :) Also like meagar commented, a true array will var_dump something like array {[0] => string("item1") [1] => string("item2") } –  MrXenotype 20 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.