Multisite Conditional (if blog_id?) in a page template?

I have a WordPress network with multiple sites using the same theme. Now I need to edit part of a certain PageTemplate.php to display different things based on the blog id.

Example:

If blog ID 1, run this:

<a href="https://wordpress.stackexchange.com/questions/124104/<?php if(get_post_meta($post->ID,"videoembed_videopopup', true)): ?><?php echo get_post_meta($post->ID, 'videoembed_videopopup', true) ?>
<?php else: ?>
<?php if(get_post_meta($post->ID, 'links_link_custom', true)): ?><?php echo get_post_meta($post->ID, 'links_link_custom', true) ?>
<?php else: ?>
<?php the_permalink(); ?>
<?php endif; ?><?php endif; ?>" class="hover-gradient <?php if(get_post_meta($post->ID, 'videoembed_videopopup', true)): ?>video-pop-up<?php endif; ?>" target="_blank"https://wordpress.stackexchange.com/questions/124104/<?php if(get_post_meta($post->ID,"videoembed_videopopup', true)): ?>rel="prettyPhoto"<?php endif; ?>><?php the_post_thumbnail('progression-thumb-retina'); ?></a><?php endif; ?>

else, run this:

<?php if(get_post_meta($post->ID, 'links_link_custom', true)): ?>
<a href="https://wordpress.stackexchange.com/questions/124104/<?php echo get_post_meta($post->ID,"links_link_custom', true) ?>" class="hover-gradient <?php if(get_post_meta($post->ID, 'videoembed_videopopup', true)): ?>video-pop-up<?php endif; ?>">
<?php else: ?>
<?php if(get_post_meta($post->ID, 'videoembed_videopopup', true)): ?>
<a href="https://wordpress.stackexchange.com/questions/124104/<?php echo get_post_meta($post->ID,"videoembed_videopopup', true) ?>" class="hover-gradient <?php if(get_post_meta($post->ID, 'videoembed_videopopup', true)): ?>video-pop-up<?php endif; ?>">
<?php else: ?>
<?php endif; ?><?php endif; ?>
<?php the_post_thumbnail('progression-thumb-retina'); ?></a>
<?php endif; ?>

I found this conditional which I believe is the way to accomplish my objective:

<?php
global $blog_id;

if ($blog_id == 1) {
code here;
} 

else {
code here;
}
?>

How can I put the two blocks of codes inside the blog id conditional, is it possible? If not, is there another way to do this?

2 Answers
2

You can do it like this:

<?php
   $blog_id = get_current_blog_id();
   if ( 1 == $blog_id ) {
?>

<!-- Your FIRST code block here -->

<?php } else { ?>

<!-- Your SECOND code block here -->

<?php } ?>

Here’s the whole prettified version of the code you can use (totally UNTESTED, but should work unless there are any silly mistakes that I didn’t notice):

<?php
    $blog_id = get_current_blog_id();
    $drabello_video_embed_popup = get_post_meta( $post->ID, 'videoembed_videopopup', true );
    $drabello_custom_link = get_post_meta( $post->ID, 'links_link_custom', true );

    if ( '' !== get_the_post_thumbnail( 'progression-thumb-retina' ) ) {

        if ( 1 == $blog_id ) {

            if( $drabello_video_embed_popup ) {

                echo '<a href="'. $drabello_video_embed_popup .'" class="hover-gradient video-pop-up" target="_blank" rel="prettyPhoto">'. the_post_thumbnail( 'progression-thumb-retina' ) .'</a>';

            } elseif ( $drabello_custom_link ) {

                echo '<a href="'. $drabello_custom_link .'" class="hover-gradient" target="_blank">'. the_post_thumbnail( 'progression-thumb-retina' ) .'</a>';

            } else {

                echo '<a href="'. the_permalink() .'" class="hover-gradient" target="_blank">'. the_post_thumbnail( 'progression-thumb-retina' ) .'</a>';

            }

        } else {

            if ( $drabello_custom_link ) {

                echo '<a href="'. $drabello_custom_link .'" class="hover-gradient">'. the_post_thumbnail( 'progression-thumb-retina' ) .'</a>';

            } elseif( $drabello_video_embed_popup ) {

                echo '<a href="'. $drabello_video_embed_popup .'" class="hover-gradient video-pop-up">'. the_post_thumbnail( 'progression-thumb-retina' ) .'</a>';

            }

        }

    }
?>

Leave a Comment