简体   繁体   中英

How do you get or display the path of an absolute link using PHP?

I know that this is a simple question but I am trying to link to a php page from within a php template. My template is located at: wp-content/themes/maton-theme/main.php

And the file that I am linking to is in the same directory I tried a few things but my code is wrong.

<a href="<?php $url = includes_url('post-2.php'); ?>">Link</a>

I also tried linking by using a simple:

<a href="post-2.php">Link</a>

But it didn't work since I am using a theme, the link goes to http://local.test.com/main/post-2.php and I get a Page not found error.

How can I link to another php file within the same directory? Also, Is there a way to show the whole URL path?

I wouldn't recommend doing what you are doing. It would be better to just create a post in WordPress with a custom template and link to that. However I could missing the reason why you would need to do this. So what you need to do is get the template directory URL using get_template_directory_uri() .

<a href="<?php echo get_template_directory_uri(); ?>/post-2.php">Link</a>

Reference: https://developer.wordpress.org/reference/functions/get_template_directory_uri/

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