SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add line break in PHP for Wordpress Post title containing " | "

    Hello there,

    I have a situation I'd like to fix with our Wordpress template. If you look here: http://www.slarc.com/projects/ Some of the project titles are getting cut off because there are only a certain number of characters allowed per title. But for every title we have " | " between the project title and the project location. Is there a way in our PHP coding that we can tell it to replace " | " with a linebreak, so the location of the projects is always on a line below the title?

    I don't know where to start with this coding if I can get some help, it would be much appreciated!

    Thanks,
    Lorne

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,075
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lorne17 View Post
    Hello there,

    I have a situation I'd like to fix with our Wordpress template. If you look here: http://www.slarc.com/projects/ Some of the project titles are getting cut off because there are only a certain number of characters allowed per title. But for every title we have " | " between the project title and the project location. Is there a way in our PHP coding that we can tell it to replace " | " with a linebreak, so the location of the projects is always on a line below the title?

    I don't know where to start with this coding if I can get some help, it would be much appreciated!

    Thanks,
    Lorne
    http://php.net/manual/en/function.str-replace.php

    Code PHP:
    $new_title = str_replace(" | ", "</br>", $title);

  3. #3
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply Wolfe.

    I added your code to my line of PHP in my template:

    PHP Code:
    <h3><a href="<?php the_permalink(); ?>"><?php $title the_title('','',FALSE); echo substr($title040), $newtitle str_replace(" | ""</br>"$title);?></a></h3>
    However if you look at the page: http://www.slarc.com/projects/ You'll see it's still adding the title with the location, then duplicating the title, and adding the break to another line for the location. I've tried different variations of the code, but can't seem to get it to work.

    Why is it duplicating the title? (sorry for the rookie questions, I'm not very familiar with PHP).

    Thanks,
    Lorne

  4. #4
    SitePoint Addict
    Join Date
    Aug 2009
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally got it!

    I needed to remove "$title" from the echo substr.

    For reference this is working with:

    PHP Code:
    <h3><a href="<?php the_permalink(); ?>"><?php $title the_title('','',FALSE); echo substr(''040), $newtitle str_replace(" | ""</br>"$title);?></a></h3>


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •