【问题标题】:Class isn't being added to current page? (want to highlight the active page)类没有被添加到当前页面? (想要突出显示活动页面)
【发布时间】:2016-01-20 10:12:06
【问题描述】:

我正在尝试向用户在 Wordpress 中所在的当前页面添加一个类..

到目前为止,我有下面的代码,但它不工作有人知道为什么吗?

  <?php while ( have_posts() ) : the_post(); ?>
  <a href="<?php echo get_the_permalink(); ?>" title="<?php echo the_title(); ?>"> 
  <div id="grid-icon" class="icon-news<?php if (is_page($post->ID)) { 
  echo "page-active "; } else {} ?> col-xs-12 col-md-4">
     <p><?php echo get_post_meta($post->ID, 'wpcf-short-title', true); ?></p>
  </div> </a>
  <?php endwhile;?>

【问题讨论】:

标签: php wordpress loops


【解决方案1】:

看这一行:

<div id="grid-icon" class="icon-news<?php if (is_page($post->ID)) { 
    echo "page-active "; } else {} ?> col-xs-12 col-md-4">

icon-newspage-active 类之间没有空格。尝试将echo 命令更改为:

echo " page-active "; //note the space between `"` and `page-active`

这也可以简写为:

<div id="grid-icon"
    class="icon-news<?php echo is_page($post->ID) ? " page-active " : null ?> col-xs-12 col-md-4">

【讨论】:

    【解决方案2】:

    当你使用 php 代码添加类时,你必须在类之前添加一个空格。

    echo "page-active "; } else {} ?> col-xs-12 col-md-4">
    

    行将是

    echo " page-active "; } else {} ?> col-xs-12 col-md-4">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      相关资源
      最近更新 更多