Long story short, I'm using a buggy WordPress template and we're too far into development to change. Good news is that we only have one item left to deal with and the rest I've been able to hack away at with my beginner PHP skills and a little help from a friend.
The homepage of the template grabs all posts and creates a thumbnail. What I want to do is have those links go to the corresponding page.
I just need to add some code any posts page that change the URL after WP's output
<li><a href='localhost/brian/vhs_or_beta' 开发者_Python百科title='VHS or Beta'>VHS or Beta</a></li>
<li><a href='localhost/brian/the_ettes' title='The Ettes'>The Ettes</a></li>
becomes
<li><a href='localhost/brian/vhs-or-beta' title='VHS or Beta'>VHS or Beta</a></li>
<li><a href='localhost/brian/the-ettes' title='The Ettes'>The Ettes</a></li>
by changing "_" to "-"
Just to be clear, I still want the page to draw using the latest posts but I want them to link to their corresponding page. I know it's repetitive but for the sake of clarity I've used the same names but I've used "_" to indicate post, and "-" to indicate page.
I figured today would also be a good day to learn PHP regex but there are only 4 links and it's not changing any time soon.
My question is whether it's a better idea to do this with a separate PHP script rather than try to hack WP....either way I'm out of my depth but I at least want to start trying in the right place.
I'm thinking that the easiest way would be a small PHP script that would simple search 4 explicit URLs appearing in <a href>
and changing them for 4 specific alternatives.
Can anyone give me some guidance here?
You can try something like
<li><a href='<? echo preg_replace('/_/','-',$url); ?>' title='<? echo $title; ?>'><? echo $title; ?></a></li>
For more info see PHP preg_replace function
精彩评论