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


PHP rendering two columns, but I need three columns

我有一些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>';

您只需要更改几件事(我在行和缩进方面也改进了代码风格):

$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>';