Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a widget area on my posts category pages and am wondering if there is a way to dynamically add a link to the first post as well as the current post of the category. I think the first post can be hardcoded since it will never change, but the current one will. I wonder what sort of php madness that can do this?

I know I know, you are asking why not just use the post navigation that already comes right... Well clients being what they are.

Any help is greatly appreciate and will very much aid in my not walking of a cliff.

share|improve this question
    
If you're displaying a list of posts, aren't all the posts technically current, how do you differentiate between whatever post you're referring to and any others currently listed on the page? –  t31os Feb 17 '11 at 18:15
    
not a list of posts, but when you are in an actual post/detail page. They client wants to be able to always have a link to the current and first post of that category, IN ADDITION to the next and previous, so for some crazy reason if you are say in the 7 post say of 10 you can automatically go to the first or current post in that category. This apparantly is a matter of life and death. –  Driftwood Feb 17 '11 at 18:28
    
So you want to query posts in the current category, fetch the first(however you want to determine first) and additionally determine next and previous, aswell as current, and link to each of them? Am i on track? –  t31os Feb 17 '11 at 18:42
    
yes, but only first and current. I already have the next and previous running and set up from the base install. :) –  Driftwood Feb 17 '11 at 23:10
add comment

1 Answer 1

up vote 1 down vote accepted

you can use wp-pagenavi and set the "number of pages to show" to 3 so you will get

[first] [6] [7] [8] [last]

now if you just need the names of the post then you could do something like this:

global $wp_query;
    //curent post
    $thePostID = $wp_query->post->ID;
    $my_query = new WP_Query(array('cat' => get_query_var('cat')));
    $count = 0;
    $curent_count = 0;
    if ($my_query->have_posts()){
        while  ($my_query->have_posts()){
            $no_repeat = array();
            $my_query->the_post();
            $count = $count + 1;
            if ($count = 1 ){// the first post in the category
                if (!in_array($post->ID,$no_repeat){
                    echo '<a href="'.the_permalink().'">'.the_title().'</a> ';
                    $no_repeat[] = $post->ID;
                }
            }
            if ($count = ($thePostID -1) ){//previous post
                if (!in_array($post->ID,$no_repeat){
                    echo '<a href="'.the_permalink().'">'.the_title().'</a> ';
                    $no_repeat[] = $post->ID;
                }
            }
            if ($count = $thePostID){//Current post
                if (!in_array($post->ID,$no_repeat){
                    echo '<a href="'.the_permalink().'">'.the_title().'</a> ';
                    $no_repeat[] = $post->ID;
                }
            }
            if ($count = ($thePostID + 1)){//Current post
                if (!in_array($post->ID,$no_repeat){
                    echo '<a href="'.the_permalink().'">'.the_title().'</a> ';
                    $no_repeat[] = $post->ID;
                }
            }
        }
    }

Hope this helps.

share|improve this answer
    
I'd suggest giving this a shot. +1 :) –  t31os Feb 17 '11 at 20:38
    
haha, yeah that looks like some solid coding!!! thanks! I was thinking if there was a way for it to get only the first and the last and be used in a widget? –  Driftwood Feb 17 '11 at 23:12
    
yea, this code will get you the first and to get the last{ wtf? } you can get the query count and when you hit the last then output the link to it. –  Bainternet Feb 17 '11 at 23:38
    
Might want to start with building a widget, then see how you go adding the above provided code(but start with building a widget first, you can always come back if you have problems working the provided code in), see: codex.wordpress.org/Widgets_API#Developing_Widgets –  t31os Feb 18 '11 at 0:27
    
allright thanks for the tip, I will try it out. –  Driftwood Feb 18 '11 at 21:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.