add_action() in switch statement

I’m using the approach of having all of my single-views in one loop-single.php and thus have a simple switch statement that will render each single-view based on the post type:

switch($post->post_type) {

    do_action('basey_loop_single');

    default: ?>

        <?php echo apply_filters('basey_page_title_news', __('<h1>News</h1>','basey')); ?>
        <?php /* Start loop */ ?>
        <?php while (have_posts()) : the_post(); ?>
            <?php basey_post_before(); ?>
                <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
                    <?php basey_post_inside_before(); ?>
                        <header>
                            <h2 class="entry-title"><?php the_title(); ?></h2>
                            <?php basey_entry_meta(); ?>
                        </header>
                        <div class="entry-content">
                            <?php the_content(); ?>
                        </div>
                        <footer>
                            <?php wp_link_pages(array('before' => '<nav id="page-nav"><p>' . __('Pages:', 'basey'), 'after' => '</p></nav>' )); ?>
                            <div class="taxonomy">
                                <?php echo __('Posted in ','basey'); the_category(', '); ?>
                            </div>
                            <?php $tag = get_the_tags(); if (!$tag) { } else { ?><div class="tags"><?php the_tags(); ?></div><?php } ?>
                            <div class="commentLinks"><?php comments_popup_link( __( ' 0 Comments', 'blank' ), __( ' 1 Comment', 'blank' ), __( ' % Comments', 'blank' ), 'comments-link', __('Comments closed', 'blank')); ?> <?php if ( comments_open() ) : ?>| <a href="<?php the_permalink(); ?>#respond" title="<?php echo __('Add a Comment','basey'); ?>"><?php echo __('Add a Comment','basey'); ?></a><?php endif; ?>
                            </div>
                        </footer>
                    <?php basey_post_inside_after(); ?>
                    <?php comments_template(); ?>
                </article>
            <?php basey_post_after(); ?>
        <?php endwhile; /* End loop */ ?>

<?php }

You can see at the top that I’ve included a do_action() call for what I hope can be an extendable area (that can be plugged-into via other plugins or theme functions I write). While this is also part core PHP question –> is there a way to plug into a switch statement the same way you 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');

Related posts

Leave a Reply

1 comment

  1. Not quite sure what you’re after – something like this?

    <?php echo apply_filters( 'basey_page_title_news', __( '<h1>News</h1>', 'basey' ) ) ?>
    <?php
    
        while ( have_posts() ) : the_post();
            switch ( true ) :
                case has_action( "basey_loop_single_$post->post_type" ) :
                    do_action( "basey_loop_single_$post->post_type", $post );
                    break;
    
                default : ?>
    
        <!-- your fallback code -->
    
    <?php
    
            endswitch;
        endwhile;
    
    ?>
    

    If a plugin/theme created a hook for a particular post type, it’d replace the default output.