blog.knym.net | I'm searching something to change my life.

WordPressで各カテゴリの最新の記事をとって、それを最新順に並べ替えて表示

もっと賢い方法がある気がするけど、とりあえず以下の感じで対応。


<?php 
                    $count = 0;
                    function getCategoryLatestPosts($ids){
                        //$bloggerCatIDs = '46,50,52,54,13,45,14,11,12';
                        $idsArr = split(",",$ids);
                        $posts = array();
                        //カテゴリ別に最新のpostを取得
                        foreach($idsArr as $catId){
                            $p =query_posts("showposts=1&cat={$catId}");
                            array_push($posts,$p[0]);
                        }
                        //配列の空要素を削除
                        function delNull($var){
                          if ($var == '') return FALSE;
                          return TRUE;
                        }
                        $posts = array_filter($posts, 'delNull');
                        //IDで最新順に並べ替え
                        function compareID( $a , $b){
                          $cmp = strcmp($b->ID, $a->ID );
                          return $cmp;
                        }</p>

<pre><code>                    usort( $posts , &amp;quot;compareID&amp;quot; );
                    return $posts;
                }

            ?&amp;gt;
            &amp;lt;?php

            global $bloggerCatIDs;
            foreach(getCategoryLatestPosts($bloggerCatIDs) as $post):
                setup_postdata($post);
            ?&amp;gt;
            &amp;lt;?php 
                $class = ($count%2==0)? 'alpha' : 'omega';
                $count++;
            ?&amp;gt;

                    &amp;lt;div class=&amp;quot;item grid_2 &amp;lt;?=$class?&amp;gt;&amp;quot;&amp;gt;
                    &amp;lt;div class=&amp;quot;img&amp;quot;&amp;gt;&amp;lt;a href=&amp;quot;&amp;lt;?php the_permalink();?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php userphoto_the_author_thumbnail();?&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;
                    &amp;lt;div class=&amp;quot;text&amp;quot;&amp;gt;&amp;lt;h4&amp;gt;&amp;lt;a href=&amp;quot;&amp;lt;?php the_permalink();?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php the_substr_title()?&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/h4&amp;gt;
                        &amp;lt;p class=&amp;quot;subinfo&amp;quot;&amp;gt;&amp;lt;span class=&amp;quot;flag&amp;quot;&amp;gt;&amp;lt;?php the_flag();?&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;?php the_author();?&amp;gt;&amp;lt;/p&amp;gt;
                    &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;?php 
                if($count%2==0) print '&amp;lt;div class=&amp;quot;clear&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;';
            ?&amp;gt;
            &amp;lt;?php
                //wp_reset_query();
            endforeach;
            ?&amp;gt;

Related Posts

  • No related posts

Write a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Essentials

Meta

Pages

Categories