Help - Search - Members - Calendar
Full Version: Select default category
Movable Type Community Forum > Additional Resources > Tips and Tricks
shiden4
I'm using MTEntryCategory to generate a class for the h3 heading tags - depending on the category, a different style is selected. However, I'm sure that people will forget to append a category to a post all the time, I was wondering how I can get MT to append a default category, for example "default"

Here is the h3 code:
CODE
<h3 class="<$MTEntryCategory$>">The title for this post</h3>

Clueless as to the default category process mellow.gif
ndns
I do not think there is a way to define a default category from within MT. However, I can code a PHP solution that would allow you to automatically select a default category if one is not chosen.

It shouldn't take long and I will post back when I am done. If you can't use it, hopefully someone down the road can.
LisaJill
*raises hand* I'll use it in a heartbeat hehe. =)
shiden4
That's excellent, I'll be using it for sure. My blog is running on .php templates - so I'm sure implementing it won't be a problem.
ndns
On the page where you want the information to display, drop the following code inside the MTEntries container, somewhere BEFORE the line that has the entry title.
CODE
<?php

$entry_category = '<MTEntryCategory>';

if (empty($entry_category)) {
    $display_category = 'DEFAULT_CATEGORY_NAME';
    }
else {
    $display_category = '<MTEntryCategory>';
    }

?>

(Within that code, change DEFAULT_CATEGORY_NAME to the name of your default category.)

Then, use this PHP instead of the MTEntryCategory tag:
CODE
<? echo $display_category; ?>

Which makes the entire line look like this:
CODE
<h3 class="<? echo $display_category; ?>">The title for this post</h3>

Remember that this doesn't set a default category within MT...it just tells the page to use a certain category if one wasn't defined.

Advantage: if you later edit the post to include a category, the default category will no longer be used.

Disadvantage: you have to put this code on every page where you want to use it.

Hope that helps...let me know if you have any problems.
shiden4
QUOTE (ndns @ Mar 18 2004, 02:00 AM)
Disadvantage: you have to put this code on every page where you want to use it.

Just one template, the index - it's perfect, thanks a lot! I'm going to take it for a test drive now, I'll let you know the results.

Thanks again - this is a very handy script.
shiden4
Works perfectly.

I'm going to use this on another blog, and each category style has a different background image icon - I couldn't accept having a class="" empty, because there would be no icon unless I styled all h3's, in which case they would all be the same icon.
ndns
I'm glad it works! biggrin.gif
kadyellebee
One way you could do this with just the CSS would be to have a common prefix for all of your styles on this element.

like this:
CODE
.cat { background-color: green;}
.catred {background-color: red;}
.catblue {background-color: blue;}
.catyellow {background-color:yellow;}

In your template, you'd use
CODE
<h3 class="cat<$MTEntryCategory dirify="1"$>"><$MTEntryTitle$></h3>


When there is no category, it will just specify the cat class. When there is a category, it will use cat + the category name.

And then, a single line PHP code that could work would be:
CODE
<h3 class="<? if ("<$MTEntryCategory"=="") {echo "<$MTEntryCategory dirify="1"$>";} else {echo "default";} ?>"><$MTEntryTitle$></h3>

That does the same thing ndns' code did, just a bit more condensed.

smile.gif
Kristine
ndns
To correct Kristine's last line of code:
CODE
<h3 class="<? if ("<$MTEntryCategory$>"=="") {echo "<$MTEntryCategory dirify="1"$>";} else {echo "default";} ?>"><$MTEntryTitle$></h3>

(You just left off part of the first MTEntryCategory tag.)

I like your CSS-only solution...would be good for people who don't have PHP or don't want to bother switching just to be able to set a default category.
kadyellebee
Oh, thanks for catching that, ndns!! smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.