0

I have the following switch statement:

switch($post->post_type) {

    default:

        echo 'default';

<?php }

which in essence is about as basic as you can get. The post->post_type is simply a variable that will return a string depending on the post type of an item (WordPress).

What I'd like to be able to do is plug into the switch statement very similarly as someone would an array - like so:

function add_extra_fruits($fruits) {

$extra_fruits = array(
    'plums',
    'kiwis',
    'tangerines',
    'pepino melons'
);

// combine the two arrays
$fruits = array_merge($extra_fruits, $fruits);

return $fruits;
}
add_filter('add_fruits', 'add_extra_fruits');

using the array_merge() function.

I was given the following example to try:

switch ( true /* we're comparing a boolean, not the post type! */ ) {
    case has_action( "basey_loop_single_$post->post_type" ) :
        do_action( "basey_loop_single_$post->post_type" );
        break;

    default :
        // default post layout
}

but I'm not sure if this accomplishes what I'm after as passing an example action, like so:

function basey_loop_single_page_output() {
    echo 'test';
}
add_action('basey_loop_single_page', 'basey_loop_single_page_output');

does not return anything when the $post->post_type condition is met as it is specified. Anyone have an idea of how this could be approached? At its simplest, this is a core PHP question (about plugging into a switch statement), but obviously some of the lingo here is more geared towards WordPress.

Thanks!

4

0 回答 0