WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a widget that shows all the post names. All the post names are "Chapter ## - Title"

I would like the php in the widget to remove the "Chapter 1 -" or "Chapter 12 -" for example so that just the part after the Chapter is left....

share|improve this question
Did you know you can accept the best answer for each of your questions? This will give a reputation increase to you and to the answerer. Now it looks like you never "return the favor" by not accepting any answer. – Jan Fabry Apr 8 '11 at 13:36
up vote 1 down vote accepted

This is a new query loop that I use that shows the latest ten post titles/permalinks in the category mycategoryname and strips the first 15 characters from all of the titles.

<?php $my_query = new WP_Query('category_name=mycategoryname&showposts=10'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="Permanent Link to: <?php the_title_attribute(); ?>">
<?php $mytitle = get_the_title(); $mytitle = substr($mytitle,15); echo $mytitle; ?></a>
<?php endwhile; ?>

If you need to select which posts to strip either 8 or 10 characters, you'd have to select them by some sort of criterea and alternate that character number.

share|improve this answer

You can use php strpos function with substr function go get just the part you want for example:

$title = substr($post->post_title,strpos($post->post_title, " - ");

this will look for " - " inside the string and return all chars after it.


simply change your code to this:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s','PureType'), get_the_title()) ?>">
        <?php substr($post->post_title,strpos($post->post_title, " - "); ?>
share|improve this answer
I like that simple approach - how would I insert it in this code: – dreamgrowers Apr 7 '11 at 2:42
[code] <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s','PureType'), get_the_title()) ?>"> <?php truncate_title(40) ?> </a></li>[code] – dreamgrowers Apr 7 '11 at 2:43
@dreamgrowers: i updated the answer. – Bainternet Apr 8 '11 at 13:30
I really appreciate it. I get a syntax error using that exact code and would still like to include the truncate. Thoughts? – dreamgrowers Apr 15 '11 at 5:12
Create a pastebin or pastor of you full code so could see and help you better – Bainternet Apr 15 '11 at 5:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.