Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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.

Update

simply change your code to this:

<li>
    <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, " - "); ?>
    </a>
</li>
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

 
discard

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.