我有一种叫做“产品”的定制邮件类型。在每一个产品页,我想有一个边栏与所有产品列出。侧栏中的当前产品应加下划线。
例如,在访问产品3时,应突出显示产品3:
侧栏产品1 产品2 产品3 产品4
我认为解决方案可以是包含所有产品的wp菜单,然后使用..current菜单项类来实现高亮显示。但是当创建新产品时,侧边栏必须自动更新。
下面是我列出所有产品的方法。有没有一种方法来集成..current菜单项功能?
<?php $loop = new WP_Query( array( 'post_type' => 'product' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>通过添加这样的类?
<a class="current-product" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>发布于 2014-09-07 14:53:51
是的,有一个简单的方法可以实现这一点。
下面是完成您的任务的代码的修改版本:
<?php
// get the current product ID
$current_post_id = get_the_ID();
$loop = new WP_Query( array( 'post_type' => 'product' ) );
while ( $loop->have_posts() ) {
$loop->the_post();
?>
<a href="<?php the_permalink(); ?>" <?php echo get_the_ID() == $current_post_id ? 'class="current-product"' : ''; ?>><?php the_title(); ?></a>
<?php
}
?>在这里,我们基本上将当前产品的ID保存在一个变量中,然后对于列表中的每个产品,我们检查环产品ID是否等于当前产品ID。如果是,我们将显示class="current-product"属性。
https://stackoverflow.com/questions/25710984
复制相似问题