WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] HTML-Tags in the_title repeated in title attribute of <a>-link (6 posts)

  1. erzlager
    Member
    Posted 5 years ago #

    Hello,

    I want to use HTML in post titles like <strong> or <span>. Works as intended BUT on the homepage the post title becomes a link with a title attribute. Within this is the WP-tag the_title which repeats the HTML-Tags.Example:
    title="<span>Chapter 01: </span>Hello World"
    Is it possible to remove this and if: how?

    Many thanks in advance

    Raimund

  2. stvwlf
    Member
    Posted 5 years ago #

    Investigate the PHP function strip_tags() You can designate which tags are allowed to remain. All others are stripped out.

    You can apply this selectively in only the cases where you need it with
    if ... else ... logic

  3. erzlager
    Member
    Posted 5 years ago #

    Hello stvwlf,

    thanks for your reply.

    I tried strip_tags but seem to do it wrong :-(. In order to 'help' my possible helpers i will explain my problem more in detail.

    First i want to have a post title like:
    <h2><span>Chapter 01: </span>Hello World</h2>

    Then i will style the span-Element with CSS. So far it works fine both in single post view as on the post overview i.e. homepage, archive, search.

    On these overviews the title is a link to the article which has a title attribute within. This shows the span thing like written in my 1. post.

    The original code for this is:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Link zu »
    <?php the_title(); ?>
    «"><?php the_title(); ?></a>

    I tried strip_tags then in the following way (which works on other places in order to show category description in link titles):
    <?php echo strip_tags(the_title()); ?>

    But that doesn't work. Either it is not capable to delete tags *within* the_title or the tags are already converted to unicode.

    Maybe with WP it is not possible to handle post titles like this? For an answer i would be very grateful because if it is *not* possible i will have to think for an other way to achieve my goal. I don't want to have this span stuff around there ;-).

    Many thanks again

    Raimund

  4. sojweb
    Member
    Posted 5 years ago #

    the_title(), by default, echoes, and doesn't return, the title text. For strip_tags() to work, it has to return the string. Try this:

    <?php echo strip_tags(the_title('','',FALSE)); ?>

    However, WordPress provides a function just for what you're doing: the_title_attribute()

    Reference:
    http://codex.wordpress.org/Template_Tags/the_title_attribute
    http://codex.wordpress.org/Template_Tags/the_title

  5. erzlager
    Member
    Posted 5 years ago #

    sojweb,

    you made my day :-). Thank you *very* much for your exemplary answer! You taught me something about the function, showed me the corresponding solution out of WP, were very fast and finally did not let me feel any annoyance in case my approach was too dilettantish.

    Great!

    Best regards

    Raimund

  6. AlexLakes
    Member
    Posted 4 years ago #

    What about elsewhere, such as sidebars & widgets, where 'the_title' is currently used. Is there any way to replace these with 'the_title_attribute'?

    Thanks in advance.

Topic Closed

This topic has been closed to new replies.

About this Topic