好的,首先设置一个数组,该数组包含对应于框1、2、3、4(按顺序)的页面ID。我使用了框的位置作为键,这些作为一个类添加到<div>
元素。
将当前页的ID与每个页的ID进行比较,并有条件地附加类“current”。
//The pages as $location => $page_id
$pages = array(
\'product-left\' => 22,
\'product-center-left\' => 25,
\'product-center-right\' => 17,
\'product-right\' => 42);
//Current page ID
$current_id = (is_page() ? get_queried_object_id() : 0);
?>
<div class="product-wrapper">
<?php $count=0; ?>
<?php foreach ($pages as $position => $page): ?>
<?php $class = ( $page == $current_id ? $position.\' current\' : $location); ?>
<a class="products-box" href="<?php echo get_page_link($page);?>">
<div class="<?php echo esc_attr($class);?>">
<?php get_sidebar($count.\'-products\'); ?>
</div>
</a>
<?php count++; ?>
<?php endforeach; ?>
<div class="clear"></div>
</div>