这是我用来创建子菜单的代码,该子菜单列出了我在wordpress中创建的所有自定义帖子:
<ul class="submenu">
<img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
<!-- List post types -->
<?php
$the_query = new WP_Query( 'post_type=artworks_post' );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li id="submenu_link" class="submenu_item_link">';
echo '<a href="' .get_permalink(). '" > ';
the_title();
echo '</a>';
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
<img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
</ul>
例如我当前的帖子是artwork_post / project-coke,但它没有将当前/活动类添加到子菜单(Project Coke title)中的标题。
如何在子菜单中为当前帖子的标题添加活动/当前类?
子菜单的构建方式是获取自定义帖子...
答案 0 :(得分:1)
试试我的代码? :
将此代码放入 functions.php
function if_current($s) {
global $wp_query,$post;
$current = $wp_query->get_queried_object_id();
$post_id = $post->ID;
if($current==$post_id){echo $s;}
}
所以编辑你的代码如:
<ul class="submenu">
<img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
<!-- List post types -->
<?php
$the_query = new WP_Query( 'post_type=artworks_post' );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li id="submenu_link" class="submenu_item_link ';
if_current('current');
echo '">';
echo '<a href="' .get_permalink(). '" > ';
the_title();
echo '</a>';
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
<img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
</ul>
结果:
<li id="submenu_link" class="submenu_item_link current">
<a href="http://webkunst.comeze.com/test/artworks_post/project-coke/"> Project Coke</a>
</li>
PS:您可以在主题中添加循环中的if_current('text')
,这将在$post->ID == $wp_query->get_queried_object_id()
(如果是当前)
因此,您可以在CSS中使用当前类
答案 1 :(得分:0)
您需要检查当前所在的页面(或类别),并将其他类添加到处于活动状态的子菜单项。您可以使用get_the_ID()
检索WP循环中当前元素的ID。
所以在此之前:
echo '<li id="submenu_link" class="submenu_item_link">';
如果当前帖子ID与帖子ID匹配,您需要添加一个检查,并在li
元素中添加一个类,以显示该值。
答案 2 :(得分:0)