Page 1 of 1

How to get blog slug name?

Posted: Wed Sep 13, 2023 2:20 am
by kocritz
Hello,

I'm developing a theme, and I want to automatically add blog menu/link (if home is a static page) to the navigation bar.

So far what I got is $site->uriFilters('blog') will get the blog URL.

But I still can't find the way to get the slug name.

For example, the slug being inputted is /stories/ or /articles/ or anything else.

How to get this slug name?

Thank you.

Re: How to get blog slug name?

Posted: Mon Sep 18, 2023 3:51 pm
by CCDzine
Try getting the field value.

Code: Select all

$site->getField( 'uriBlog' );
.

This is what I do to use the slug as the menu entry...

Code: Select all

$blog_uri = $site->getField( 'uriBlog' );

if ( ! empty( $blog_uri ) ) {

	// Use sprintf() if you need to echo later.
	printf(
		'<li><a href="%s">%s</a></li>',
		$site->url() . str_replace( '/', '', $blog_uri ) . '/',
		ucwords( str_replace( [ '/', '-', '_' ], ' ', $blog_uri ) )
	);
}

Re: How to get blog slug name?

Posted: Mon Sep 18, 2023 10:23 pm
by kocritz
Splendid. Thank you very much. Never thought can be achieved this way. So much better.

Re: How to get blog slug name?

Posted: Tue Sep 19, 2023 10:25 pm
by Edi
Thank you for the workaround! This is an older problem that has never been fixed. ;)

Re: How to get blog slug name?

Posted: Mon Nov 27, 2023 6:50 pm
by Romeo21
Thanks, never knew how to solve it!