elithoma
elithoma

Reputation: 1

Back one directory in php

I am building a wordpress site with galleries in it and want to add a 'Back to Gallery' link in my single-attachment.php that will send the user back one directory.

For example, if the image is at www.site.com/galleries/gallery1/image1/ I would want the link to send it back to www.site.com/galleries/gallery1/.

Any suggestions would be appreciated.

Upvotes: 0

Views: 85

Answers (1)

ForTW
ForTW

Reputation: 176

You can either explode current URL,and remove last term from the URL and give it as link for Back to Gallery.

<?php $url = explode('/', 'www.site.com/galleries/gallery1/image1/');
array_pop($url);
$back=implode('/', $url); 
?>
<a href="<?php echo $back; ?>" >Back to Gallery</a>

Or you can use $_SERVER['HTTP_REFERER'] for getting the previous URL.

<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>" >Back to Gallery</a>

also by using javascript you can implement this in easy way :

<a href="javascript:history.go(-1)" >Back to Gallery</a>

Upvotes: 1

Related Questions