我把我所有的页面都称为一个部分。这是我的代码:
<?php $args = array(
\'post_type\' => \'page\',
\'orderby\' => \'ASC\',
\'posts_per_page\' => 50,
\'meta_query\' => array(
array(
\'key\' => \'page_as_section\', // Calling only those page that set to page as section
\'value\' => \'Yes\'
),
),
);
$onepage = new WP_Query($args);
?>
<?php if ( $onepage->have_posts() ) : while ( $onepage->have_posts() ) : $onepage->the_post(); ?>
<?php $sectyp = array(
\'meta_query\' => array(
array(
\'key\' => \'section_type\', // selecting the section type "Normal" or "Parallax"
\'value\' => \'Parallax\'
),
),
); ?>
<?php if($sectyp) { ?>
<?php if( get_field(\'select_parallax\') ){ ?>
<div class="section-dark" style="background-image: url(\'<?php the_field(\'select_parallax\'); ?>\');" data-stellar-background-ratio="0.7">
<?php } ?>
<?php get_template_part( \'content\', \'page\' ); ?>
</div>
<?php }
else{ ?>
<div class="section-light">
<?php get_template_part( \'content\', \'page\' ); ?>
</div>
<?php } ?>
<?php endwhile; wp_reset_postdata(); endif; // end of the loop. ?>
我面临的问题是,只有当条件起作用并将页面包装为(div class=“section dark”)中的一个节时,else条件才起作用,而它没有包装为(div class=“section light”)。我犯了个错误。。请帮助。。谢谢
最合适的回答,由SO网友:czerspalace 整理而成
尝试更换以下部件:
<?php $sectyp = array(
\'meta_query\' => array(
array(
\'key\' => \'section_type\', // selecting the section type "Normal" or "Parallax"
\'value\' => \'Parallax\'
),
),
); ?>
使用
<?php $sectyp = get_post_meta(get_the_ID(), \'section_type\', true); ?>
和替换
<?php if($sectyp) { ?>
使用
<?php if($sectyp == "Parallax") { ?>