开发者

WordPress - Creating a Custom Loop with all categories displayed, but each category with seperate DIV, CLASS and ID

开发者 https://www.devze.com 2023-02-16 19:50 出处:网络
I am fairly experienced with creating custom WordPress loops and such, but I have a requirement now that I need assistance on.

I am fairly experienced with creating custom WordPress loops and such, but I have a requirement now that I need assistance on.

I need to create a WordPress loop that displays posts from all categories (that parts easy :-) ) however, each category needs to be encased in a DIV with a unique class and ID, with all of the posts for that category shown in that DIV. This needs to happen to all categories, no matter how many there are, and no matter if new ones are added or old ones are removed.

If anyone can help me with this or prov开发者_如何学Cide an example, that would be grand :-)

Thanks so much Zach


You can get the desired categories by doing:

<?php $categories = get_categories( $args ); ?>

Documentation here.

Then you can loop within $categories, using query_posts create your <div> and do whatever you want with it. (including fetching all the posts that belongs to that category)

Those two links I gave you should be enough. But here is some pseudo code of what you may do:

$categories = get_categories( $args );
while($c in $categories):
    $posts = query_posts($c); 
    while($p in $posts):    
        <div></div>
    endwhile;
endwhile;
0

精彩评论

暂无评论...
验证码 换一张
取 消