PHP呈现两列,但我需要三列

时间:2014-07-25 17:18:47

标签: php html wordpress

我有一些php可以创建两列网站内容类别,其中包含该类别的子文章。我已经尝试过插入列出后两个帖子的代码,但它不起作用(因为我不知道我在做什么)。目前的php呈现如下:

<div class="row">
<div class="column col-half">...</div>
<div class="column col-half">...</div>
</div>

我希望将其渲染为:

<div class="row">
<div class="column col-third">...</div>
<div class="column col-third">...</div>
<div class="column col-third">...</div>
</div>

以下是呈现HTML的主题代码:

$st_categories = get_categories($st_hp_cat_args); 
$st_categories = wp_list_filter($st_categories,array('parent'=>0));
if ($st_categories) {
    foreach($st_categories as $st_category) { 
        $st_cat_counter++;

        if ((!is_int($st_cat_counter / 2)) && $st_cat_counter != 1) { 
            echo '</div><div class="row">';
        } elseif ($st_cat_counter == 1) {
            echo '<div class="row">';
        }

        echo '<div class="column col-half '. $st_cat_counter.'">';
        echo '<h3> <a href="' . get_category_link( $st_category->term_id ) . '" title="' . sprintf( __( 'View all posts in %s', 'framework' ), $st_category->name ) . '" ' . '>' .     $st_category->name.'</a>';

        if (of_get_option('st_hp_cat_counts') == '1') {
            echo '<span class="cat-count">(' . $st_category->count.')</span>';  
        }

        echo '</h3>';

提前致谢。

1 个答案:

答案 0 :(得分:1)

你需要改变一些事情(我在行和缩进方面改进了代码风格):

$st_categories = get_categories($st_hp_cat_args); 
$st_categories = wp_list_filter($st_categories,array('parent'=>0));
if ($st_categories) {
    foreach($st_categories as $st_category) { 
        $st_cat_counter++;

        if (1 === $st_cat_counter % 3 && $st_cat_counter !== 1) { // change 2 -> 3 and use mod operator %
            echo '</div><div class="row">';
        } elseif ($st_cat_counter == 1) {
            echo '<div class="row">';
        }
        echo '<div class="column col-third '. $st_cat_counter.'">'; // half -> third
        echo '<h3> <a href="' . get_category_link( $st_category->term_id ) . '" title="' .     sprintf( __( 'View all posts in %s', 'framework' ), $st_category->name ) . '" ' . '>' .     $st_category->name.'</a>';
        if (of_get_option('st_hp_cat_counts') == '1') {
            echo '<span class="cat-count">(' . $st_category->count.')</span>';  
        }
        echo '</h3>';