简体   繁体   中英

Make a link redirect to the same page

I've created a page with an article. On top of that there's a title. If people try to click this title I want them to be redirected to the same page. Like this: https://gyazo.com/74350b4fe91c670c4101449ee1c928a4 If I click on the article it just refreshes the page.

I can't do this manually for every article because I'm using a script. The code I've written looks like this:

 echo '<a href="/"> <h1 class="entry-title">'.$row['postTitle'].'</h1></a>'; 
As you can see I wrote

 <a href="/"> 

and that's will not redirect me / refresh the page i'm viewing.

This is how it looks for me: https://gyazo.com/8a15ae274d8a7240b07100395460568d as you can see it does not redirect me to the same page when i click the title.

How can I do this?

To make your custom PHP blog post template be able to display a title link that points back to the page, one way is to make use of your $row[... . variable.

Provided that,

  • your URLS will look like your screenshots, such as http://localhost/viewpost.php?id=8 when running locally and for example http://www.yourwebsite.com/viewpost.php?id=8 when online
  • you know how to refer to the post's id that is used in the ...viewpost.php?id=8 , for example $row['postID']
  • you don't yet have any variable or means to refer to your current domain http://localhost when local or http://www.yourwebsite.com when online

Then, I recommend a two-part approach:

Somewhere at the top of your code, or perhaps in an include you might use for such code-reuse purposes, define for example $host :

$host='http://' . $_SERVER['SERVER_NAME'];

Then, for your actual title link::

echo '<a href="' . $host . '/viewpost.php?id=' . $row['postID'] . '"> <h1 class="entry-title">' . $row['postTitle'] . '</h1></a>';

Explanation

  • Separated HTML from the concatenation dots . with spaces, to be easier to read, as well as to support any helper programs such as fmt that you might use for wrapping long lines, so they have spaces to use for wrapping lines.
  • Uses PHP's predefined $_SERVER variable's SERVER_NAME , which, combined with the http:// , the $host will be http://localhost when local and http://www.yourwebsite.com when online.
  • Define $host as a variable once at the top of the page, because it is clearer that way and likely you will have a use for it elsewhere on the page, so this helps avoid having to repeat yourself writing 'http://'.$_SERVER['SERVER_NAME'] everywhere else you may need to start forming the absolute URL
  • $host is then combined with the pattern for the rest of the URL, to assemble the absolute URL
  • Absolute URL is helpful so that if a user saves your article to their computer, then later clicks the title link on their locally saved article, the user can still correctly reach the original online page
  • As the article author, setting a link this way also means it can serve as a permalink , which helps with search engine optimization (SEO)

Please use # in href attribute! that will redirect you on the same page.

Problem solved. I used

 echo '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>'; 

Thank you everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM