我在 Wordpress 上使用定制器来设置不同的布局,并更改边栏的位置。
该脚本的工作方式如下:它在侧边栏左侧或右侧设置一个 float 。
但是当侧边栏设置在左边时,它会在内容下方(因为内容在 float:left;
上)
结构是这样的
<div class="home">
<div class="content" style="float:left;">
<?php get_template_part('loop'); ?>
</div>
<div class="sidebar">
<?php get_sidebar(); ?>
</div>
</div>
在我的标题中
<?php
$sidebar_position = get_theme_mod('sidebar_position');
?>
<style>
#sidebar-primary {float: <?php echo $sidebar_position; ?>;}
</style>
侧边栏位置的部分脚本:
$wp_customize->add_setting('sidebar_position', array());
$wp_customize->add_control('sidebar_position', array(
'label' => __('Sidebar position', 'Blog'),
'section' => 'layout',
'settings' => 'sidebar_position',
'type' => 'radio',
'choices' => array(
'left' => 'left',
'right' => 'right',
),
));
如何自动更改内容相对于侧边栏的位置? 简单的 CSS 还是我需要在脚本中添加一些东西来设置与内容相反的 float 位置?
最佳答案
如果你想通过css float left 将侧边栏位置设置到你的内容左侧,侧边栏必须放在内容之前。所以你可以这样检查设置:
<div class="home">
<?php if ($sidebar_position == 'left') { ?>
<div class="sidebar">
<?php get_sidebar(); ?>
</div>
<?php } ?>
<div class="content" style="float:left;">
<?php get_template_part('loop'); ?>
</div>
<?php if ($sidebar_position == 'right') { ?>
<div class="sidebar">
<?php get_sidebar(); ?>
</div>
<?php } ?>
</div>
关于php - 设置内容相对于侧边栏的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33856346/