动态数字PHP递增div id

时间:2014-01-20 20:35:23

标签: php jquery wordpress jquery-ui

我正在使用这个脚本:

http://jqueryui.com/tabs/

我用它来创建动态WordPress服装后期类型类别显示。我设法动态地呈现了类别,但我需要为每个编辑的类别创建一种动态数字ID 另外,我需要动态创建包含类别内容的DIV(我将在那里添加帖子列表)。

这是一些代码:

  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#tabs" ).tabs();
  });
  </script>
<?php

$taxonomy = 'portfolio_categories';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>


<div id="tabs">
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="#tabs-<?php here i need the dynamic numeric ID ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<div id="tabs-1">
i need this to be created dynamicly like the foreach
  </div>
  <div id="tabs-2">
i need this to be created dynamicly like the foreach

  </div>
  <div id="tabs-3">
so on and so on..
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

只需在循环中添加递增计数器:

<div id="tabs">
    <ul>
        <?php $counter = 0; ?>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="#tabs-<?php echo $counter; ?>"><?php echo $term->name; ?></a></li>
            <?php $counter++; ?>
        <?php } ?>
    </ul>
    <?php $counter = 0; ?>
    <?php foreach ( $terms as $term ) { ?>
        <div id="tabs-<?php echo $counter; ?>">
            i need this to be created dynamicly like the foreach
        </div>
        <?php $counter++; ?>
    <?php } ?>
</div>