I want to use the URL of the Wordpress post page in the fixed page where I transitioned.

Asked 2 years ago, Updated 2 years ago, 66 views

Nice to meet you.

I would like to put the URL of the Wordpress post page in the variable and use the URL of the transition source post page within the fixed page where I transitioned from the link in that post page, but it doesn't work.

Even if you define and insert variables in single.php on the post page, if you display them on a fixed page with the same variable name, NULL will appear.

[single.php]

$url=get_permalink();

[kotei_test.php]

<?php
var_dump($url);
?>

I feel that the scope of the variable is wrong, but I'm confused because I don't know where to set it.

Could you please let me know if there are any useful pages such as explanations on how to use Wordpress variables?

I'm sorry for asking such a clumsy question as I'm a beginner at customizing Wordpress.

php wordpress

2022-09-30 15:34

1 Answers

The variable is not taken over in the first place because it is a different session as PHP at the time of page transition.

As a PHP, it's easy to say that you can use $_SESSION, but using SESSION in WordPress is basically avoided.

However, I think that's enough for personal use.

single

session_start();
$_SESSION['last_url'] = get_permalink();

page

session_start();
if(!empty($_SESSION['last_url']) var_dump($_SESSION['last_url']);

@see
[PHP]Summary of request parameter sessions - Qiita #Session related things

P.S. However, be aware that if you need this dynamic behavior, using a cache, etc., can cause unintended behavior.


2022-09-30 15:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.