Support » Fixing WordPress » Add prefix to specific post

  • i used this code to add prefix to specific post

    function add_podcast($title) {
    	if( in_category( 'foobar' ) ) $title = 'Podcast' . $title;
    	return $title;
    }
    add_filter('the_title', 'add_podcast');

    and i modified it to be “Updated” prefix

    function add_suffix($title) {
    	if( in_category( 'updated' ) ) $title =  $title . '<span class="updated">' . 'Updated'. '</span>';
    	return $title;
    }
    add_filter('the_title', 'add_suffix');

    but there is something went wrong

    see results

    http://imageshack.us/a/img832/5118/ccnplessonsonlinearabic.png

    Thanks in advance 🙂

Viewing 15 replies - 1 through 15 (of 19 total)
  • Could you show a sample of the source code so I can see what the html looks like?

    @peter

    this is the whole code

    function add_suffix($title) {
    	if( in_category( 'updated' ) ) $title =  $title . '<span class="updated">' . 'Updated'. '</span>';
    	return $title;
    }
    add_filter('the_title', 'add_suffix');

    No no, I meant the front-end code. The actual html that gets shown in the browser.

    Michael

    (@alchymyth)

    Forum Moderator

    your theme seems to be falsely using the_title() instead of the_title_attribute() in the title attribute of the linked post title (possibly in index.php, if your image is from a front page posts?)

    general example of a linked post title with the right title attribute code:

    <a href="<?php the_permalink(); ?>" title="Permalink to <?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a>

    @alchymyth

    yes it worked with <?php the_title_attribute(); ?>

    but there is something needs to get edited here i believe because the styling is not working now 🙂

    function add_suffix($title) {
    	if( in_category( 'updated' ) ) $title =  $title . '<span class="updated">' . 'Updated'. '</span>';
    	return $title;
    }
    add_filter('the_title', 'add_suffix');
    Michael

    (@alchymyth)

    Forum Moderator

    for any styling problems, please post a link to your site to illustrate what is not working.

    a: nobody here knows how the styling is supposed to look;
    b: nobody can check anything, for instance with a browser inspection tool such a Firebug http://getfirebug.com/, to see how the styling is influenced.

    Sorry my bad

    http://ccnplessons.com/page/7/

    look @ post “06 – How Data TransfersUpdated”

    “updated” suppose to be

    .updated{float:right;color:#ff0000;}

    and it worked before i change <?php the_title(); ?> to <?php the_title_attribute(); ?>

    There’s a lot of things going wrong there.
    The span element seems to have been stripped but there’s something else too. The entire span element has also been parsed inside the title attribute of a link below. (the watch this lesson button)

    yeah i think there is something wrong with original code, i dunno!

    function add_suffix($title) {
    	if( in_category( 'updated' ) ) $title =  $title . '<span class="updated">' . 'Updated'. '</span>';
    	return $title;
    }
    add_filter('the_title', 'add_suffix');

    I’m thinking you’re probably using a filter so you don’t have to manually change each relevant template file, right?

    But you could do just that, add it manually in each template file:

    <?php if( in_category( 'updated' ) ){
    echo '<span class="updated">Updated</span>';
    }?>

    i added it to every template file and it does nothing.

    I tested this line, worked for me. Where did you place it? Could you show snippet?

    Michael

    (@alchymyth)

    Forum Moderator

    and it worked before i change <?php the_title(); ?> to <?php the_title_attribute(); ?>

    you only change this witin the title attribute, not the actual title;

    I posted an exact example already:

    <a href="<?php the_permalink(); ?>" title="Permalink to <?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a>

    so there is this part (the title attribute part):
    title="Permalink to <?php the_title_attribute(); ?>"

    and then there is (the title directly):
    <?php the_title(); ?>

    @alchymyth

    it worked now .. thank you for help

    now i have another problem .. when you browse the category that has an updated post .. the menus are effected too, check here

    http://ccnplessons.com/lessons/ccna/network-introduction/

    ===========================================

    @peter_l
    the filter worked itself without your code,
    it was in the bottom of my template files 🙂

    Michael

    (@alchymyth)

    Forum Moderator

    add another conditional check into your filter:

    http://codex.wordpress.org/Function_Reference/in_the_loop

    function add_suffix($title) {
    	if( in_the_loop() && in_category( 'updated'  ) ) $title =  $title . '<span class="updated">' . 'Updated'. '</span>';
    	return $title;
    }
    add_filter('the_title', 'add_suffix');
Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Add prefix to specific post’ is closed to new replies.