开发者

How to paginate wordpress page + need help debuging + need help with POST/GET requests

This is the p开发者_如何学运维age, its a wordpress powered site:

http://bit.ly/9oJXWV

You select some value, it makes POST to same page and based on value you selected it makes a list pages.

Now before you jump into my code i just want to say that im a newbie and that my main problem here were database queries so i didnt focus on other small stuff(like bunch if's at start inline css and stuff like that).

So this is my template:

http://pastebin.com/HQvMq3Db

This is a function from functions.php which im using in template:

http://pastebin.com/fWKqqzQv

This page works the way i want it and i just finnished putting all the code together but have one issue. Once i get that sorted out i will make the code a lot nicer... :)

So the issue is that if you look at pages that are listed once you make a selection and submit, on a lot of them some values are missing even thought those values are there(open any page from that list which is missing some value and you can pretty much see the same stuff but now it display's all the data).

So that is the part i need help with debugging. I really have no idea how to tackle this.

Second part of this question is simple: how do i paginate this page? Any link, tip, tutorial would be good.

Also one more thing, how can i have links for example like this:

.../hostels/?grad=Beograd

and when user opens up that page he does not have to click to select the town, it would already list all pages from "Beograd"? I guess that is GET request right? Can i do something like that with POST? O_o Not sure what to do here, like i said im newbie.

Thanks for reading, looking forward to answers and comments.

Cheers!


You can set up your functions to enable pagination in WP without have to do any custom logic.

See: http://codex.wordpress.org/Template_Tags/query_posts#Pagination_Parameters


and when user opens up that page he does not have to click to select the town, it would already list all pages from "Beograd"? I guess that is GET request right? Can i do something like that with POST?

yes. yes. no.

GET requests retrieve the variables from the url. so you just run a link with GET variables, the php would suscessfuly display your info. but if you are using POST, the variables are retrieved from "the background", passed by the previous page. so you cannot just run a link, the page must be called from a previous page (trough a form) or the page won't have access to the variables.


1) I fixed pagination simply by implementing &paged='.get_query_var('paged') to my query. Now it looks like this:

$hostels = new WP_Query('post_type=page&meta_key=Grad&meta_value='.$grad.'&posts_per_page=60&orderby=title&order=ASC&paged='.get_query_var('paged'));

@js1568 i gave him +1 for his answer, but he didnt answer my entire question.

Now i can go through pages like so:

/acommodation/hostels/?city=beograd - this is page 1
/acommodation/hostels/page/2/?city=beograd - this is page 2
/acommodation/hostels/page/3/?city=beograd - this is page 3
etc...

2) The issue with missing info from some pages is fixed by putting this below the end of inner loop:

wp_reset_query();

and also i created some custom function which will get all meta values for given post id:

function custom_get_meta_values($id){

$first_array = get_post_custom_keys($id);

foreach ($first_array as $key => $value) :
       $second_array[$value] =  get_post_meta($id, $value, FALSE);
        foreach($second_array as $second_key => $second_value) :
            $result[$second_key] = $second_value[0];
        endforeach;
 endforeach;

return $result;
}

In my inner loop i call that function like this:

$result = custom_get_meta_values($post->ID);

Then i just echo what i need like so:

echo $result['Mail'];

Just put the name of meta field in that $result array and echo it.

3) I replaced POST with GET request so now i can have links like this:

/acommodation/hostels/?city=beograd

which when opened will show every hostel from 'beograd'. I only have 4 possible values for cities so if value of 'city' that i capture from GET request is not one of those 4 values, i do nothing, just show that form. If it is i take that value and show the list from that city.

As per Will instructions, i will mark this answer as accepted.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜