mdnash
mdnash

Reputation: 81

Remove Redundant text when displaying titles within a loop

I have a loop that calls all my post titles of a CPT, each of with starts the the same X characters.

Example:

XX XXX Alabama

XX XXX Alaska

etc

How can I remove the 'XXXXX' so all that is displayed is:

Alabama

Alaska

etc

Here's my loop:

<?php
wp_reset_postdata();
$state_list = new WP_Query(array(
    'post_type' => 'play_states',
    'post_status' => 'publish',
    'posts_per_page' => -1,

));

while ($state_list->have_posts()) : $state_list->the_post();
    echo '<li><a href="'.get_permalink().'" class="' . $class . '">'.get_the_title().'</a></li>';
endwhile;
wp_reset_query();
?>

Thanks!

EDIT:

I added the following:

$title = get_the_title();
$newTitle = str_replace("XXXXX Post", "", $title);

After the line opening the while statement. By simply replaceing "XXXXX Post" with the text I wanted to remove I was able to accomplish my goal.

Thank you everyone for your help!

Upvotes: 0

Views: 34

Answers (2)

Adarsh Sojitra
Adarsh Sojitra

Reputation: 2199

You can use str_replace function! Here we go!

$variable = str_replace("XXXXX","", $variable);

Let me know it that helps!

In A Loop!

// I hope you have Array named $cities Containing the cities!
foreach($cities as $city)
{
    $city_final = str_replace("XXXX","",$city);
    // Do whatever you want with $city_final now!
}

Sorry if there ar syntax Errors here! I am trying to give you the idea! I don't think there are syntax Errors too!

Upvotes: 2

Vbudo
Vbudo

Reputation: 370

This will split your text into 2 parts on a space. You want the second element in the array.

$onlyState=explode(' ', $stateWithJunk, 2); 
// do something with $onlyState[1]

Upvotes: 0

Related Questions