$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - A to Z List of Custom Post Type in three columns|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

loop - A to Z List of Custom Post Type in three columns

matteradmin9PV0评论

I want to display custom post types A to Z Index in three columns, but it just lists the first post under each alphabet in three columns, I think there is some issue with the loop but can not figure out how can I fix it

The code is listed below:

<div class="container">
    <div class="row">
        <div class="serviceproviders-inner-content haslayout">
<h2>A to Z Medication Guide </h2>
<?php

$query = new WP_Query(array(
                        'post_status'   => 'publish',
                        'post_type'     => 'medicines',
                        'orderby'       => 'title',
                        'order'         => 'ASC',
                        'posts_per_page'    => -1
                        ));

$post_count = $query->post_count;
$posts_per_column = ceil($post_count / 4);      

$rows = array();                                            
$count = 0;

while ($query->have_posts())

{ 
    $query->the_post(); 

    if($rows[$count] == "" ) { $rows[$count] = '<div class="row">'; }




        If ($letter != strtoupper(get_the_title()[0]))
        {
                // echo ($letter != '') ? '</ul></div>' : '';
                 $letter = strtoupper(get_the_title()[0]);  

        $rows[$count] = $rows[$count] .  '<div class="col-sm-3">' .  '<h4>'.strtoupper(get_the_title()[0]).'</h4>' .'<div class="post-title">'. '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>' . '</div></div>';  
        $count++;                           

        }   

        if ($count == $posts_per_column ) { $count = 0; } 


}
foreach ($rows as $row) { echo $row . '</div>'; }
?>
</div>
        </div>
    </div> 

I want to display custom post types A to Z Index in three columns, but it just lists the first post under each alphabet in three columns, I think there is some issue with the loop but can not figure out how can I fix it

The code is listed below:

<div class="container">
    <div class="row">
        <div class="serviceproviders-inner-content haslayout">
<h2>A to Z Medication Guide </h2>
<?php

$query = new WP_Query(array(
                        'post_status'   => 'publish',
                        'post_type'     => 'medicines',
                        'orderby'       => 'title',
                        'order'         => 'ASC',
                        'posts_per_page'    => -1
                        ));

$post_count = $query->post_count;
$posts_per_column = ceil($post_count / 4);      

$rows = array();                                            
$count = 0;

while ($query->have_posts())

{ 
    $query->the_post(); 

    if($rows[$count] == "" ) { $rows[$count] = '<div class="row">'; }




        If ($letter != strtoupper(get_the_title()[0]))
        {
                // echo ($letter != '') ? '</ul></div>' : '';
                 $letter = strtoupper(get_the_title()[0]);  

        $rows[$count] = $rows[$count] .  '<div class="col-sm-3">' .  '<h4>'.strtoupper(get_the_title()[0]).'</h4>' .'<div class="post-title">'. '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>' . '</div></div>';  
        $count++;                           

        }   

        if ($count == $posts_per_column ) { $count = 0; } 


}
foreach ($rows as $row) { echo $row . '</div>'; }
?>
</div>
        </div>
    </div> 
Share Improve this question asked Feb 14, 2019 at 10:41 Emma BEmma B 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The problem lies in this line:

$rows[$count] = $rows[$count] .  '<div class="col-sm-3">' .  '<h4>'.strtoupper(get_the_title()[0]).'</h4>' .'<div class="post-title">'. '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>' . '</div></div>';

As I understand your code correctly, it should append another column item to given row.

But it doesn't do it - it overwrites the content of given row with current column.

So it should look like this (.= instead of =):

$rows[$count] .= $rows[$count] .  '<div class="col-sm-3">' .  '<h4>'.strtoupper(get_the_title()[0]).'</h4>' .'<div class="post-title">'. '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>' . '</div></div>';

PS. You wrote that you want 3 columns, but in your code you divide by 4 - but maybe that's just a typo.

Post a comment

comment list (0)

  1. No comments so far