【发布时间】:2016-03-09 07:06:07
【问题描述】:
我刚刚开始学习 PHP,并试图修改现有代码以匹配我正在尝试做的事情。我有多个变量,我试图在部分使用计数器的同时定义所有变量。我的问题是将计数附加到我单独定义的变量中。另外,我确信有一种更简洁的方法可以做到这一点(比如循环或嵌套数组),我只是看不出怎么做。
<?php
$pageId0 = opt('first_tab_page');
$post0 = get_post($pageId0);
$content0 = apply_filters('the_content', $post0->post_content);
$icon0 = wp_get_attachment_image_src( get_post_thumbnail_id( $post0->ID ), 'full' );
$pageId1 = opt('second_tab_page');
$post1 = get_post($pageId1);
$content1 = apply_filters('the_content', $post1->post_content);
$icon1 = wp_get_attachment_image_src( get_post_thumbnail_id( $post1->ID ), 'full' );
$pageId2 = opt('third_tab_page');
$post2 = get_post($pageId2);
$content2 = apply_filters('the_content', $post2->post_content);
$icon2 = wp_get_attachment_image_src( get_post_thumbnail_id( $post2->ID ), 'full' );
?>
<div>
<?php echo $icon0[0]; ?>
</div>
<div>
<?php echo $icon1[0]; ?>
</div>
<div>
<?php echo $icon2[0]; ?>
</div>
<?php
$tab_position = opt('tab_position');
if ($tab_position == '' || count($tab_position) != 3) {
$tab_position = array(0, 1, 2);
}
for($i=0; $i < count($tab_position); $i++)
{
$selected = '';
if (opt('default_selected_tab') == $tab_position[$i]){
$selected = 'class="selected"';
}
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $content//should be 0 to start; ?>
</a>
<?php } ?>
【问题讨论】:
-
有点不清楚,但我认为你应该使用array
-
我能做些什么来清理一下吗?我对如何将数组与 opt('first_tab_page') 片段一起使用感到困惑
-
你期望有多少页