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>';
<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,
http://localhost/viewpost.php?id=8
when running locally and for example http://www.yourwebsite.com/viewpost.php?id=8
when online ...viewpost.php?id=8
, for example $row['postID']
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>';
.
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. $_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. $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 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.