在给定的搜索结果页面上,我需要修改特定结果的链接目标,例如:
http://www.name.com/company/company
到。{
http://www.name.com/company/
; http://www.name.com/company/something
到。{
http://www.name.com/company/#something
; http://www.name.com/business/business
到。{
http://www.name.com/business/
; http://www.name.com/business/somethingelse
到。{
http://www.name.com/business/#somethingelse
; http://www.name.com/portfolio/projectname
到。{
http://www.name.com/portfolio/
; 请注意,这是一个多语言网站,网址也可能采用http://www.name.com/en/company/something-en
的形式,我需要将其更改为http://www.name.com/en/company/#something
,依此类推。
我相信这可以通过使用PHP的正则表达式实现,但我不够熟练,无法从头开始编写实用的东西。
实际示例,更改search-content
div
内的以下链接(由wordpress循环生成的链接):
<div id="search-content">
<h3><?php _e( 'Search Results for', 'foundationpress' ); ?> <span>"<?php echo get_search_query(); ?>"</span>:</h3>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class('blogpost-entry'); ?>>
<header>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif;?>
</div>
答案 0 :(得分:0)
Tbis就是这个职能:
function processHref($input_url) {
$regex_one = '#http://([^/]+)/([^/]+)/([^/]+)#';
$regex_two = '#http://([^/]+)/([a-z]{2})/(.+)/([^-]+)#';
if(preg_match($regex_one, $input_url, $matches)) {
$domain = $matches[1];
$first = $matches[2];
$second = $matches[3];
}
elseif(preg_match($regex_two, $input_url, $matches)) {
$domain = $matches[1];
$lang = $matches[2];
$first = $matches[3];
$second = $matches[4];
}
$output_url = "http://$domain/";
if(isset($lang)) $output_url .= "$lang/";
if(isset($first)) $output_url .= "$first/";
if($first != $second && $first != "portfolio")
$output_url .= "#$second";
return $output_url;
}
?>