2015-02-11 48 views
-3

我正在为一个单页网站使用WordPress安装。我正在使用'query_post'数组获取所有内容。对于每个页面部分,它会创建一个id为#content的div。使用PHP添加CSS-class

<?php query_posts('post_type=page&order=ASC'); ?> 

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<div id="content"> 
    <div id="inner-content" class="wrap cf"> 
      <div id="main" class="m-all"> 

       <h2><?php the_field('headline') ?></h2> 

       <div class="sidebar"><?php the_field('sidebar') ?></div> 

       <div class="main-content"><?php the_content(); ?></div> 

      </div> 
    </div> 
</div> 

<?php endwhile; endif; ?> 

我现在想要做的是根据顺序为每个页面部分添加一个特定的ID。类似于

<div id="content section-1">...</div> 
<div id="content section-2">...</div> 
<div id="content section-3">...</div> 

等等。我如何修改我的代码来实现这一目标?

+2

我认为你的代码有问题:id不是类;) – Masiorama 2015-02-11 08:30:04

+0

除了上面的内容,你应该使用'counter' – rnevius 2015-02-11 08:35:38

+1

哦,天啊!内容ID正在被复制。 :O:O – 2015-02-11 08:38:47

回答

-1
<?php query_posts('post_type=page&order=ASC'); ?> 

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<div id="content section-<?php the_ID(); ?>"> 
    <div id="inner-content" class="wrap cf"> 
      <div id="main" class="m-all"> 

       <h2><?php the_field('headline') ?></h2> 

       <div class="sidebar"><?php the_field('sidebar') ?></div> 

       <div class="main-content"><?php the_content(); ?></div> 

      </div> 
    </div> 
</div> 

<?php endwhile; endif; ?> 

这将是工作还

<?php query_posts('post_type=page&order=ASC'); ?> 
<div id="content section-<?php echo $i++; ?>"> 
    <div id="inner-content" class="wrap cf"> 
      <div id="main" class="m-all"> 

       <h2><?php the_field('headline') ?></h2> 

       <div class="sidebar"><?php the_field('sidebar') ?></div> 

       <div class="main-content"><?php the_content(); ?></div> 

      </div> 
    </div> 
</div> 

<?php endwhile; endif; ?> 
-1

首先在逻辑上一个元素应该只有一个ID。因为你重复整个ID =“内容”,我建议你更好地添加类,而你可以添加id =“content”的包装。并获得ID你可以简单地使用the_ID();这应该工作。所以代码应该看起来像

<div id="content"> 

    <?php query_posts('post_type=page&order=ASC'); ?> 

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

     <div id="inner-content<?php the_ID(); ?>" class="wrap cf"> 

      <div id="main" class="m-all"> 

        <h2><?php the_field('headline') ?></h2> 

        <div class="sidebar"><?php the_field('sidebar') ?></div> 

        <div class="main-content"><?php the_content(); ?></div> 

       </div> 
     </div> 

<?php endwhile; endif; ?> 
</div> 

所以现在所有他们将有唯一的ID。 :)干杯!