If my permalink structure is set to /blog/%postname%/
, how can I retrieve a URL for my site which retrieves domain.com/blog/
?
With the permalink structure mentioned above, when using site_url();
, it displays domain.com/
when I’m looking to easily get the site’s URL including the front base.
Maybe I’m unclear about what the “front base” actually is.
I’m under the impression that the front base is your “Permalink Structure”. (Stored in the option permalink_structure
)
I’ve tried home_url();
and site_url();
… Both output just domain.com/
I’m thinking about just using site_url();
then retrieving the option permalink_structure
, parsing the contents of the option myself, then appending it to the site_url();
function.
$permalink_structure = get_option('permalink_structure');
$front_base = preg_replace('#(\%)(.*)(\%/)#si', '', $permalink_structure);
echo site_url($front_base, 'http').PHP_EOL;
Is there an easier way to get the site’s URL including the front base?
3 s
You can get the value of front
in the global $wp_rewrite
:
global $wp_rewrite;
echo $wp_rewrite->front;
// or
echo home_url( $wp_rewrite->front );
Though that is probably of limited use, as the front base isn’t necessarily an existing page, and may 404 in many cases. If you’re using that value to prepend to other URLs, you’re likely doing it wrong.
If you want the page designated as the posts page, that value is stored in the page_for_posts
option:
echo get_permalink( get_option( 'page_for_posts' ) );