开发者

Wordpress - Skip category page if only one post in category

I'm trying to figure out a way of being able to click a link in a sidebar and skip straight to a single page if there is only one post in a category.

This is the website I've built for the company I work for. For example. If you click on the "Kings Theatre" link in the sidebar (under "browse by client") it goes to a category page with one item on. I would like to have this link directed at the single page.

Is there a way o开发者_开发百科f linking straight to the single page if there is only one post in that category?

Here is the link:

http://www.oysterdesign.co.uk/category/work/


The solution is:

$category = get_the_category();
                $category = $category[0];
                $cat_ID = $category->cat_ID;
                $args = array(
                    'numberposts'     => 500,
                    'offset'          => 0,
                    'category'        => $category->cat_ID,
                    'orderby'         => 'menu_order',
                    'order'           => 'DESC',
                    'post_type'       => 'post',
                    'post_status'     => 'publish' );
                
                $all_posts = get_posts($args);
                $item_amount = count($all_posts);
                // If there is only one post available, go directly to the post
                if($item_amount == 1){
                    header ("Location: ".get_permalink($all_posts[0]->ID));
                }


In the category template check for the number of elements in the $posts array. If there's 1 then include the single page template or else display the normal category listing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜