在下面的代码中,我正在检查如果我在网站的主页上,那么我将列向右移动 2 的偏移量,对于所有其他页面,我不应该有任何偏移量。我正在尝试实现这一点,但出现错误。如果你能给我一个提示,那就太好了。谢谢!
<?php if (is_front_page() ) {
echo '<div class="col-md-8 la-content-inside col-md-offset-2">';
while ( have_posts() ) { the_post(); }
} else {
echo '<div class="col-md-8 la-content-inside">';
while ( have_posts() ) { the_post(); }
}
?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
在你的代码中,你打开while
块if
else
块,并在条件块后关闭endwhile
。
尝试使用
<?php
if (is_front_page() ) {
echo '<div class="col-md-8 la-content-inside col-md-offset-2">';
while (have_posts()) : the_post();
get_template_part( 'content', 'page' );
endwhile; // end of the loop.
}else {
echo '<div class="col-md-8 la-content-inside">';
while (have_posts()) : the_post();
get_template_part( 'content', 'page' );
endwhile; // end of the loop.
}
?>
让我们从清理代码开始。我正在几行上编写代码,因此提出问题的人可以理解。(是的,你可以写得更有效率)
<?php
echo '<div class="col-md-8 la-content-inside';
// frontpage exception
if(is_front_page()) {
echo ' col-md-offset-2';
}
echo '>';
while(have_posts()) {
the_post();
get_template_part( 'content', 'page' );
}
echo '</div>';
?>
如果这会生成错误。请将错误复制粘贴为此答案中的注释。
请尝试下面的短代码..将为您..
<div class="col-md-8 la-content-inside <?php echo (is_home() ? 'col-md-offset-2' : '' );?> ">
<?php while (have_posts()) : the_post();
get_template_part( 'content', 'page' );
endwhile; // end of the loop.
?>
</div>
您需要用户is_home()
功能..为首页。