多站点条件(如果博客_id?)在页面模板中?

时间:2013-11-22 作者:drabello

我有一个WordPress网络,有多个使用相同主题的网站。现在我需要编辑某个PageTemplate.php 根据blog id.

示例:

如果日志ID为1,请运行以下操作:

<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; ?>
否则,运行以下命令:

<?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; ?>
我发现了这个条件,我相信这是实现我目标的途径:

<?php
global $blog_id;

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

else {
code here;
}
?>
我如何将这两段代码放入blog id条件中,是否可能?如果没有,还有其他方法吗?

2 个回复
SO网友:its_me

您可以这样做:

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

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

<?php } else { ?>

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

<?php } ?>
以下是您可以使用的整个经过修饰的代码版本(完全未经测试,但应该可以使用,除非有任何我没有注意到的愚蠢错误):

<?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>\';

            }

        }

    }
?>

SO网友:Pat J

如果您试图区分网络的“主”站点和任何其他站点,可以使用条件标记is_main_site():

if( is_main_site() ) {
    do_something();
} else {
    do_something_else();
}

结束

相关推荐

Copy Root Multisite Site

我在WordPress中创建了一个多站点安装,位于example.com 网站位于example.com/site1, example.com/site2 等是否可以创建example.com 在example.com/site3?Blog Copier 和NS cloner 由于某种原因,不允许您创建根站点的副本。这背后的技术限制是什么?