Back one directory in php - 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.

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>

Related

Data from database in href link echos site url

Using a foreach loop in codeigniter, I am trying to display website links from database in href html tags, But it echos the siteurl instead of linking to the website link.
<?php foreach ($as as $a): {
<li>
<strong>Web</strong>
<a href="<?php echo $a['website']; ?>" target="_blank" rel="nowfollow"><?
php echo $a['website']; ?></a></div>
</li>
<?php }endforeach; ?>
for example www.instagram.com was in the database it will link similar to this
localhost/site/view/www.instagram.com/p/BgHH1TyDqvp/ instead of just linking too www.instagram.com
I've noticed that if the protocol (http or https) isn't at the front of an external link then it'll handle it as a relative path. If I know there's a chance of an external link in the database I'll check for the protocol and add it if needed.
Updated Code
<?php foreach($as as $a) :
// Check if we have the protocol
$pro = (substr($a['website'], 0, 4) != 'http' ? 'https://' : '');
?>
<li>
<strong>Web</strong>
<a href="<?= $pro . $a['website'] ?>" target="_blank" rel="nofollow">
<?= $a['website'] ?>
</a>
</li>
<?php endforeach; ?>
This is based off memory and haven't tested this specific code block.
I don't claim this to be the best way to handle this, but has worked for me in the past.

Change logo destination URL for only one page

How do you change the logo to link to the home URL for all pages except one? I want one page to link to another page when the logo is clicked.
Here is the PHP code for the logo:
<div class="section-boxed section-header">
<?php do_action('pexeto_before_header'); ?>
<div id="logo-container">
<?php
$logo_image = pexeto_option('retina_logo_image') ? pexeto_option('retina_logo_image') : pexeto_option('logo_image');
if(empty($logo_image)){
$logo_image=get_template_directory_uri().'/images/logo#2x.png';
}
?>
<img src="<?php echo $logo_image; ?>" alt="<?php esc_attr(bloginfo('name')); ?>" />
</div>
What about creating a second section of PHP - with a slightly different DIV ID that is called when that paticular page is loaded, rather than this one which is called on all the other pages,
Copy, paste, change DIV ID <div id="logo-container2">, change link address.
In HTML - on the single page that takes them elsewhere - call
<div id="logo-container2">
Would that work?
Try to use template_tag is_page as condition
<div class="section-boxed section-header">
<?php do_action('pexeto_before_header'); ?>
<div id="logo-container">
<?php
$logo_image = pexeto_option('retina_logo_image') ? pexeto_option('retina_logo_image') : pexeto_option('logo_image');
if(empty($logo_image)){
$logo_image=get_template_directory_uri().'/images/logo#2x.png';
}
// Default logo url to home
$logo_url = esc_url(home_url('/');
// if is page about or id 5 anything inside is_page()
if(is_page('about') $logo_url = esc_url(home_url('about');
?>
<img src="<?php echo $logo_image; ?>" alt="<?php esc_attr(bloginfo('name')); ?>" />
</div>
I believe you should be able to use the get_permalink method to check which page you are on, and use an if statement to tell it what the href should be.
<a href="<?= (get_permalink() == '/my-page') ? esc_url(home_url('/go-to-page')) : esc_url(home_url('/')); ?>">
Haven't tested this, but it should work.

A href With echo open in a new window

I want to open this link on a new window: is the following correct?
Check Your Adress
I Just Find The Way To Open The Link In A New Page The Code Is
<a target = '_blank' href="<?php echo "$link"; ?><?php echo $userRow['user_wallet']; ?>" >Check Your Adress</a>
Use window.open() of javascript to achieve this.
<a onclick="window.open(document.URL, '_blank', 'location=yes,height=700,width=550,scrollbars=yes,status=yes');">Open In New Window</a>
This should help you.

Facebook Share Button data-href Wordpress Permalink Displays: "facebook.com/v2.0..."

This is how I render my button:
function fb_share() {
$permalink = get_permalink();
echo '<div class="fb-share-button" data-href="'.$permalink.'" data-layout="button_count"></div>';
}
I include the scripts right after tag opens. However, what it does when you click share is:
Any thoughts?
Try this code directly in the PHP/HTML markup of your theme. It will create a link to the current page/URL in the "data-href" attribute.
<div class="fb-share-button" data-href="<?php the_permalink(); ?>" data-layout="button_count"></div>
The problem arises because I have been testing my theme using MAMP and having the string "localhost" in the browser URL. As #dustincaruso mentioned:
<div class="fb-share-button" data-href="<?php the_permalink(); ?>" data-layout="button_count"></div>
works perfectly. Thanks.

CodeIgniter - Correct way to link to another page in a view

I was wondering if someone could tell me the correct way to link to another page from within a view.
Is there a function for this or is it just the usual about
I assume you are meaning "internally" within your application.
you can create your own <a> tag and insert a URL in the href like this
Link
OR you can use the URL helper this way to generate an <a> tag
anchor(uri segments, text, attributes)
So... to use it...
<?php echo anchor('controller/function/uri', 'Link', 'class="link-class"') ?>
and that will generate
Link
For the additional commented question
I would use my first example
so...
<img src="<?php echo base_url() ?>img/path/file.jpg" />
for images (and other assets) I wouldn't put the file path within the PHP, I would just echo the base_url() and then add the path normally.
The best way is to use the following code:
Link
you can also use PHP short tag to make it shorter. here's an example
<a href="<?= site_url('controller/function'); ?>Contacts</a>
or use the built in anchor function of CI.
Best and easiest way is to use anchor tag in CodeIgniter like eg.
<?php
$this->load->helper('url');
echo anchor('name_of_controller_file/function_name_if_any', 'Sign Out', array('class' => '', 'id' => ''));
?>
Refer https://www.codeigniter.com/user_guide/helpers/url_helper.html for details
This will surely work.
you can also use this code
//test" class="btn btn-primary pull-right">
<a href="<?php echo site_url('controller/function'); ?>Compose</a>
<a href="<?php echo site_url('controller/function'); ?>Inbox</a>
<a href="<?php echo site_url('controller/function'); ?>Outbox</a>
<a href="<?php echo site_url('controller/function'); ?>logout</a>
<a href="<?php echo site_url('controller/function'); ?>logout</a>

Categories