CyberShot Posted January 11, 2018 Share Posted January 11, 2018 I am passing an array of wordpress page IDs through an anchor link but it looks rather ugly. I am serializing my array like this $var = htmlspecialchars(serialize($pages), ENT_QUOTES); which gives me this string(45) "a:4:{i:0;i:944;i:1;i:17;i:2;i:19;i:3;i:1310;}" passing it through an anchor tag like so $link = tbpb_get_archives_link(); // returns the permalink of a WordPress page <a href="<?php echo $link . '?var='. $var ?>" class="work-all"><span><i class="fa fa-times"></i> All works</span></a> now my anchor tag looks like this in the navigation bar http://mysite.com/all-projects/?var=a:4:{i:0;i:944;i:1;i:17;i:2;i:19;i:3;i:1310;} is there a way to clean that up and make it look prettier? or another method of passing the variables from one page to another? Link to comment Share on other sites More sharing options...
kicken Posted January 11, 2018 Share Posted January 11, 2018 I'd just implode the array with a separator, such as a comma or dash or whatever else. Then explode it on the next page to get your array back. You could also form a URL with multiple parameters using PHP's parameter array syntax so your URL would look like: http://mysite.com/all-projects/?var[]=944&var[]=17&var[]=19&var[]=1310 There is a function for this built into PHP. Link to comment Share on other sites More sharing options...
CyberShot Posted January 12, 2018 Author Share Posted January 12, 2018 My only thing with this is that I don't see this kind of thing often. I would think there would be a better way. I think your idea has promise and I might actually look into that. I did try the http_build_query but I don't think it ended up working. This is the first time I have tried passing variables through a link Link to comment Share on other sites More sharing options...
CyberShot Posted January 12, 2018 Author Share Posted January 12, 2018 I'd just implode the array with a separator, such as a comma or dash or whatever else. Then explode it on the next page to get your array back. You could also form a URL with multiple parameters using PHP's parameter array syntax so your URL would look like: http://mysite.com/all-projects/?var[]=944&var[]=17&var[]=19&var[]=1310 There is a function for this built into PHP. I re-wrote it with the implode, explode. It's shorter and cleaner. It also makes the address at least look like something WordPress would do. I had hoped to find a way to hide the variable all together but I can accept this method. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.