Go to main content

Page url



<txp:page_url />

The page_url tag is a single tag that is used to return a particular component of the URL from the current page being displayed.


Tag will accept the following attributes (case-sensitive) as well as the global attributes :

context="list" v4.7.2+
Specifies which Textpattern URL parameters must be retained. For example, <txp:page_url /> without context will produce something like index.php?s=somesec&c=somecat&pg=2 on the corresponding page, while <txp:page_url context="c, s" /> will result in index.php?s=somesec&c=somecat.
Default: unset (retain everything).
default="value" v4.7.0+
The default value if no component of the current page’s URL matches the type attribute.
Default: unset.
type="type" v4.7.0+
Specifies which component of the current page’s URL will be returned.
admin_root: current site’s admin URL.
author: current page’s author on article lists filtered by author.
c: current page’s category.
css: current style sheet name.
id: current article’s id on a single article page.
images_root: images directory URL.
month: current page’s month on time based article lists.
page: current page template name.
pg: current page number in article list mode.
q: search query string.
request_uri: current article’s URL-title including any query string.
s: current page’s section.
status: HTTP error response (200, 404).
theme: current theme name.
theme_path: current theme directory URL.
themes_root: themes directory URL.
Default: request_uri.

Any other type value will return the matching URL parameter or the default value that will always be sanitized. v4.7.0+


Example 1: Show the current article’s ID, HTTP status and section

    Article ID:
    <txp:page_url type="id" />,
    From section:
    <txp:page_url type="s" />
    (Result: <txp:page_url type="status" />)

This would result in the following, for example:

    Article ID:
    From section:
    Tasty pea recipes
    (Result: 200)

Example 2: Show the current theme’s name and path

    Using theme '<txp:page_url type="theme" />' located in <code><txp:page_url type="theme_path" /></code>

This would result in the following, for example:

    Using theme 'four-point-seven' located in <code>https://example.com/themes/four-point-seven</code>

Example 3: Show the website’s image and themes directories

    Site images are located in <code><txp:page_url type="images_root" /></code> and themes are located in <code><txp:page_url type="themes_root" /></code>.

This would result in the following, for example:

    Site images are located in <code>https://example.com/images</code> and themes are located in <code>https://example.com/themes/</code>.
<link rel="stylesheet" href="<txp:page_url type="theme_path" />/styles/default.css">
<script async src="<txp:page_url type="theme_path" />/scripts/main.js"></script>

Example 5: Return the sanitized value of some URL query parameter

<txp:page_url type="lang" default="en" />

Example 6: Link to the latest article in a section

<txp:variable name="is_latest"><txp:page_url type="latest" /></txp:variable>
<txp:if_variable name="is_latest" value="1">
    <txp:article limit="1" />
    <txp:article />

Using the above code in a page template would permit you to use example.org/articles?latest=1 as a URL and have Textpattern capture the ‘latest’ parameter and take action to display just the most recent article in that section.

Other tags used: article, else, if_variable, variable.

As an alternative, in the Advanced options of the Preferences panel, create a custom form type ‘html’ of type mediatype="text/html". Then in the Forms panel, create a new form called ‘latest’ of this new ‘html’ type and make the following its contents:

<txp:article limit="1">
    <txp:header name="Location" value='<txp:permlink />' />

Now each hit of /any-section/?f=latest will 302-redirect the visitor to the latest article in that section.


Version 4.7.2

context attribute added.

Version 4.7.0

type attribute behaviour changes (value will always be sanitized).
images_root, theme, theme_path and themes_root values allowed for type attribute.
default attribute added.

If you notice any kind of problem with this page's construction or content (outdated information, typos, broken links, or whatever), open an issue to have it sorted. Or have a go at it yourself. :)