Print tag or category name

Post Reply
Gohh
Ssr. Bludit
Posts: 11
Joined: Fri Aug 23, 2019 11:32 pm

Hello, I print a name depending on the category it is or the tag. The name of the category is printed, the name of the tag is not :(

Code: Select all

<h2>
<?php if ($WHERE_AM_I == 'category') : ?>
<?php $categoryKey = $url->slug();
    $category = new Category($categoryKey);
    echo $category->name(); ?>
<?php elseif ($WHERE_AM_I == 'tags') : ?>
<?php $tagKey = $Url->slug();
    $tag = new Tag($tagKey);
    echo $tag->name(); ?>
<?php endif ?>
</h2>
User avatar
Jay
Master Bludit
Posts: 133
Joined: Mon Feb 11, 2019 8:41 pm

Code: Select all

($WHERE_AM_I == 'tags')
There is no "tags" page, use "tag" ;)

edit: without elseif
You can be either on a category page, or on a tag page.
Gohh
Ssr. Bludit
Posts: 11
Joined: Fri Aug 23, 2019 11:32 pm

Edited like this:

Code: Select all

<?php if ($WHERE_AM_I == 'category') : ?>
<?php $categoryKey = $url->slug();
    $category = new Category($categoryKey);
    echo $category->name(); ?>
<?php endif ?>
<?php if ($WHERE_AM_I == 'tag') : ?>
<?php $tagKey = $Url->slug();
    $tag = new Tag($tagKey);
    echo $tag->name(); ?>
<?php endif ?>
The fact is that when I insert "tag" instead of "tags" the tag page is not loaded. Can you somehow turn on the error show, what's the problem?
User avatar
Jay
Master Bludit
Posts: 133
Joined: Mon Feb 11, 2019 8:41 pm

Ohh I just realized you are trying to use a snippet from bludit docs.
The mentioned exception to show code on a tag page works as supposed, as I had to make some small changes on my own bloggie couple days ago.
Right after I ended fixes I found this code in docs, and just of sheer curiosity I tried it out.

There are 2 typos in the snippet you use. First one I already mentioned, second one is in $tagKey = $Url->slug(); there should be a $url not $Url


ps. to enable php error reporting change define('DEBUG_MODE', FALSE); to TRUE in /kernel/boot/init.php
Gohh
Ssr. Bludit
Posts: 11
Joined: Fri Aug 23, 2019 11:32 pm

Jay wrote: Wed Apr 08, 2020 2:14 pm Ohh I just realized you are trying to use a snippet from bludit docs.
The mentioned exception to show code on a tag page works as supposed, as I had to make some small changes on my own bloggie couple days ago.
Right after I ended fixes I found this code in docs, and just of sheer curiosity I tried it out.

There are 2 typos in the snippet you use. First one I already mentioned, second one is in $tagKey = $Url->slug(); there should be a $url not $Url


ps. to enable php error reporting change define('DEBUG_MODE', FALSE); to TRUE in /kernel/boot/init.php
Thank you, now works!

Code: Select all

<?php if ($WHERE_AM_I == 'tag') : ?>
<?php $tagKey = $url->slug();
    $tag = new Tag($tagKey);
    echo $tag->name(); ?>
<?php endif ?>
User avatar
Jay
Master Bludit
Posts: 133
Joined: Mon Feb 11, 2019 8:41 pm

Gohh wrote: Wed Apr 08, 2020 2:36 pm
Thank you, now works!
Your'e welcome :)


I hope you wont eat your own teeth on this piece of software like I did ;)
Gohh
Ssr. Bludit
Posts: 11
Joined: Fri Aug 23, 2019 11:32 pm

Jay wrote: Thu Apr 09, 2020 1:10 am
Gohh wrote: Wed Apr 08, 2020 2:36 pm
Thank you, now works!
Your'e welcome :)


I hope you wont eat your own teeth on this piece of software like I did ;)
I didn't quite understand the point, but I'll be careful :D
GracefulForm
Jr. Bludit
Posts: 3
Joined: Tue Aug 15, 2023 12:39 am
Location: Texas
Has thanked: 3 times

I came across this and it was very helpful, thank you. To fill the space when a category or tag is not being shown, I added the option to use the word "Recent."

Code: Select all

<?php
	// Check if the user is browsing a category
   if ($WHERE_AM_I=='category') {
        // Get the category key from the URL
        $categoryKey = $url->slug();
        // Create the Category-Object
        $category = new Category($categoryKey);
        // Print the category name
        echo $category->name();
        // Print the category description
        echo $category->description();   
	}
	// Check if the user is browsing a tag
   elseif ($WHERE_AM_I=='tag') {
        // Get the tag key from the URL
        $tagKey = $url->slug();
        // Create the Tag-Object
        $tag = new Tag($tagKey);
        // Print the tag name
        echo $tag->name();
   	 }
	// Show this title if none of the other are being used
   else {
		echo "Recent";
	}
?>	
Post Reply