3

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="<?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" <?php if(get_post_meta($post->ID, 'videoembed_videopopup', true)): ?>rel="prettyPhoto[gallery]"<?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="<?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="<?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?

8

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[gallery]">'. 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>';

            }

        }

    }
?>
|improve this answer|||||
3

If you're trying to distinguish between your network's "main" site and any others, you can use the conditional tag is_main_site():

if( is_main_site() ) {
    do_something();
} else {
    do_something_else();
}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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