• Resolved surfweb82

    (@surfweb82)


    Good evening,

    I wonder if we could extract the first word of each title of a post or a page and insert a html code and then display it.

    For example:

    My first title
    The Neighbor

    I wish I could pull my words and tea, add code (<strong class="blue"> my </ strong>) and then display it.

    I hope I have been clear in the explanation and I apologize for the bad English but not speak it very well.

Viewing 6 replies - 1 through 6 (of 6 total)
  • use get_the_title() and the explode the string, take the first element, add your extra code, implode everything, and echo it:

    instead of:
    <?php the_title(); ?>

    try and use:

    <?php
    $text=get_the_title();
    $text=explode(' ',$text);
    $text[0]='<strong class="blue">'.$text[0].'</strong>';
    $text=implode(' ',$text);
    echo $text;
    ?>

    I have posted a page template based on the Default theme to here: http://wordpress.pastebin.ca/1750186.

    Copy this into a file named ‘listpagetitles.php’, set $number_of_words and $limit to your own values, and upload it into your theme’s folder.

    Then create a new page and assign this as the template.

    You will probably need to change the way titles are displayed, and adjust for your theme, particularly the class names used.

    Please let me know how it works for you.

    Thread Starter surfweb82

    (@surfweb82)

    Thank you for reply.

    @alchymyth: your solution seems just right for me, I tried to insert it into the theme but returns nothing.

    You can help me understand why?

    @vtxyzzy I must not simply count the words but I have to extrapolate the first and assign a code and then show it with another text

    you could copy your php file into a pastebin and post the link here for someone to have a look at it.

    Thread Starter surfweb82

    (@surfweb82)

    @alchymyth: I’m sorry but I have noticed a typographical error and that is what did not work.

    Thank you very much for your help!

    The template I provided does more than count the words. It also provides pagination if you have too many titles to list on a page, and lets you link the titles to their page, so you can click on a title and go to that page.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘extract the first word in the title of a post’ is closed to new replies.