2015-02-11 46 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。 :)乾杯!