• hi

    i have seen some website that with the post title they have subtitle in the bracket with color and its show only in the index post list
    like this :
    this is post title(hot)
    this is post title2 (new)

    how can i add like this

    please help

    thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • this would be some code in index.php (or the template that displays your front page) that might add this text to the post title, depending on the age of the post.

    assuming this is the code for the linked title (example taken from default theme TwentyTen):
    <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

    a possible code could be:

    <h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?>
    <?php $post_age = date('U') - get_the_time('U'); if ( $post_age <= 60*60*24 ) { echo '<span style="color: #123456;"> (new)</span>'; } ?> </a></h2>

    ‘U’ is the time in seconds relative to some startpoint – https://uk2.php.net/manual/en/function.date.php

    60*60*24 is for ’60seconds * 60 minutes * 24 hrs’ == 1 day.
    above example would show ‘(new)’ as long as the post is younger than 24hrs.

    Thread Starter asifg123

    (@asifg123)

    thanks @alchymyth but what about other text or can i add my own text ??
    because its show only 24hrs but i want that the text show as i wish ,when i don’t need the text after 1 hour so i delete the text, like this website https://www.goodanime.net here are many subtitle at the same page list but when they don’t want they change it
    thanks

    just some ideas:

    if you need more control, you could try and use a custom field (in the example the key is ‘hot_text’), where you enter the text that you want to show.

    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php $hot = get_post_meta($post->ID,'hot_text',true); if($hot) { echo '<span class="css-'.$hot.'"> ('.$hot.') </span>'; } ?>

    style for instance with span.css-raw { color: #123456; }

    a different approach:

    using tags (taxonomy) and a bit of code to show only certain tags in certain colors.
    the following snippet would show all tags of the post, each within brackets and with a unique css class for the span they are contained in. if you would only have one tag per post, it will show this.

    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php $post_tags = wp_get_post_tags($post->ID); foreach($post_tags as $pt) { echo '<span class="tagged-'.$pt->name.'"> ('.$pt->name.') </span>'; } ?>

    style for instance with span.tagged-raw { color: #123456; }

    you could build on this idea and include a filter to filter out other tags that you don’t want to show.

    Thread Starter asifg123

    (@asifg123)

    thank you alchymyth ??
    custom field code work perfect, but the bracket text not appear after the post title ends.the text appear on second line
    like this
    this is sample
    (hot)
    i try div span class but can’t success here is my full code

    <?php $myposts = get_posts('numberposts=23&offset=0');foreach($myposts as $post) :?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php $hot = get_post_meta($post->ID,'raw_text',true); if($hot) { echo '<span class="css-'.$hot.'"> ('.$hot.') </span>'; } ?>

    you could try and pull it into the link, like so:

    <?php $myposts = get_posts('numberposts=23&offset=0');foreach($myposts as $post) :?>
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?><?php $hot = get_post_meta($post->ID,'raw_text',true); if($hot) { echo '<span class="css-'.sanitize_title_with_dashes($hot).'"> ('.$hot.') </span>'; } ?></a>

    this is new sanitize_title_with_dashes($hot) to allow valid css class if the value of $hot is more then one word.

    if ‘hot’ is the value of your custom field, then you can style the span (with red color for instance):
    span.css-hot { color: #f00; }

    if ‘raw’ is the value, then the style could look like (green):
    span.css-raw { color: #0f0; }

    Thread Starter asifg123

    (@asifg123)

    Thank you very much for you big help

    alchymyth ??

    alchymyth are u still here can i ask one question ??

    is this possible to use this code in category list area

    alchymyth are u still here can i ask one question ??

    is this possible to use this code in category list area

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘post subtitle in bracket with color’ is closed to new replies.