Forums

How can I highlight some posts or place a little gif at some titles? (2 posts)

  1. zaph0d
    Member
    Posted 3 years ago #

    Hello,

    after a long search here and for a plugin, I want to ask the following:

    I want to highlight some specific posts - either with a different background or a little star in the title or something else. You know, some readers send me books or spend a few bucks for a coffee, so I want to highlight the questions they have.

    I could not do this with templates... I can't put an image-link in the title...do you know something else I can do?

    Thank you for answers ;-)

  2. Dagon Design
    Member
    Posted 3 years ago #

    Hello,

    There are several ways to do this. You can do it in a template, but there has to be a way for the template file to know which posts are special.

    Have you considered a special category for those posts? You can still include them in any other categories you want, and it would give you a nice way to do it in your template file.

    For example, say you create a new category called 'Special'

    In your index.php for example, find where the title is displayed:

    <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>

    Replace that with the following code:

    <?php
    $special = FALSE;
    foreach((get_the_category()) as $category) {
        if ($category->cat_name == 'Special') $special = TRUE;
    }
    ?>
    
    <h1 <?php if ($special) echo 'class="special"'; ?>><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>

    As you can see, the first thing the code does is add a quick check to see if the post is in the category called 'Special'. (It has to loop through all categories, because you will probably have posts in multiple categories)

    If it is in the category, it sets $special to TRUE. Then in the code to display the post title, it just adds a check within the H1 tag.

    <?php if ($special) echo 'class="special"'; ?>

    If it is special, it adds the 'special' class to H1, which you could define in your CSS file like this:

    h1.special { }

    Of course, you could use that for anything - to add a class to a tag as shown above, or add some completely new code, or use as a conditional:

    <?php
    if ($special) {
      show some code
    } else {
      show something else
    }
    ?>

    etc.. lots of options :)

Topic Closed

This topic has been closed to new replies.

About this Topic