I need to extract a url path number to a shortcode... Here is an example below...
I need the number "1" to be shown with a shortcode...
[url_path_number]
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI need to extract a url path number to a shortcode... Here is an example below...
I need the number "1" to be shown with a shortcode...
[url_path_number]
You can find that with somthing like the following
add_shortcode( 'url_path_number', function ( $atts ) {
// use an attribute or the current URL
$a = shortcode_atts( array(
'url' => get_permalink(),
), $atts );
// get the path from the URL
$path = parse_url($a['url'],PHP_URL_PATH);
$parts = array_filter(explode('/', $path),function($v) { return $v !== ''; });
// get the last dir of path
return end($parts);
} );
Using the shortcode will return current URL
[url_path_number]
or you can specify the URL
[url_path_number url="https://example.com/page/c/1"]
to go just after the path, and not use get_permalink()
add_shortcode( 'url_path_number', function ( $atts ) {
$a = shortcode_atts( array(
'path' => $_SERVER['REQUEST_URI'],
), $atts );
$parts = array_filter(explode('/',$a['path']),function($v) { return $v !== ''; });
return end($parts);
} );
get_permalink()
Dec 19, 2017 at 18:46
$_REQUEST
or$_GET
. However I think this value integer is the ID of a post, page or similar in your WP install, so that you can use the WP core function likeget_the_ID
.