douou0977
2010-06-13 11:03
浏览 424
已采纳

在WordPress中获取当前页面的URL和标题?

How to get current page URL and title in WordPress?

图片转代码服务由CSDN问答提供 功能建议

如何在WordPress中获取当前页面的URL和标题?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dousou2897 2010-06-13 11:26
    已采纳

    Here are few super global variables for getting url info:

    $_SERVER['PHP_SELF'];
    $_SERVER['REQUEST_URI'];
    $_SERVER['SCRIPT_NAME']
    

    $_SERVER['SCRIPT_NAME']

    $_SERVER['SCRIPT_NAME'] will be same - /index.php. It is irrespective of the actual URI ($_SERVER['REQUEST_URI']) used to access the site.

    As it returns the actual script name, it fails provide additional path information that may be present. So if the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then too the $_SERVER['SCRIPT_NAME'] will be same - /index.php.

    $_SERVER['SCRIPT_NAME'] is supported on all platforms

    $_SERVER['PHP_SELF']

    This is the filename of the currently executing script, relative to the document root. However, unlike $_SERVER['SCRIPT_NAME'], it provides additional path information like $_SERVER['REQUEST_URI'] when the actual php file is present in the path. So when the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then $_SERVER['PHP_SELF'] will be /index.php/big/directory/.

    However if all the URI's under http://www.example.com/ is mapped to http://www.example.com/index.php, then, for example, http://www.example.com/abc/def will return /index.php like $_SERVER['SCRIPT_NAME']. Note that $_SERVER['REQUEST_URI'] data is ignored for this request.

    $_SERVER['REQUEST_URI']

    It will give you entire url including query string vars.

    Title of the page:

    There is no built-in functionality to get title of the page, however, you can use the HTML Simple DOM to read the contents of <h2.

    Example:

    $title = str_get_html('<title></title>');
    

    Now, you can use $title however, you like. Visit the their site for more info about it.

    已采纳该答案
    打赏 评论
  • douguanya6399 2010-06-13 11:13

    you can get the URL by

    'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']
    

    or

    $_SERVER['SCRIPT_URI']
    

    might work on some platforms.

    the title is something that the PHP program gives back to the browser via HTML:

    <html><head><title>title is here</title> ...
    

    so it is not something that you "get" unless you get something from the DB and send back to the browser.

    Update... do you mean the title of page that leads to your php file? (the user clicks on that page to get to your php file.) in that case, you can use $_SERVER['HTTP_REFERER'] but it is not guaranteed to contain any data (but most of the time it will).

    打赏 评论
  • dongrong7267 2013-06-01 16:31

    If you are using wordpress, and you want to print the post title you can use,

    <?php echo get_the_title(); ?>
    

    documentation here: http://codex.wordpress.org/get_the_title

    打赏 评论

相关推荐 更多相似问题