如何在WordPress中使用短代码或重定向功能修改链接为店铺分类URL
解决方案
方案1:直接修改现有模板代码(最便捷)
直接把你现有代码里的the_permalink()替换为the_shoplink()即可,修改后代码如下:
<h5 class="game-title"><a href="<?php the_shoplink(); ?>"><?php the_title(); ?></a></h5>
如果the_shoplink()需要传入文章ID参数才能正常输出,可以调整为:
<h5 class="game-title"><a href="<?php the_shoplink(get_the_ID()); ?>"><?php the_title(); ?></a></h5>
方案2:全局重定向(避免遗漏其他调用入口)
如果你站点还有其他位置调用了the_permalink()没修改到,也可以通过WordPress钩子实现全局自动跳转,把以下代码添加到主题的functions.php文件中即可:
add_action('template_redirect', 'redirect_post_to_shop_link'); function redirect_post_to_shop_link() { // 判断是否为对应自定义文章类型的单页 if (is_singular('你的自定义文章类型别名')) { global $post; // 获取店铺链接,此处根据你的the_shoplink函数实际参数调整 $shop_link = function_exists('the_shoplink') ? get_the_shoplink($post->ID) : ''; if ($shop_link) { wp_redirect($shop_link, 301); exit; } } }
注意:把代码里的
你的自定义文章类型别名替换为你站点实际的自定义文章类型标识,如果the_shoplink()没有对应的get前缀的获取函数,也可以用get_post_meta($post->ID, '存储店铺链接的自定义字段名', true)来获取对应链接值。
内容的提问来源于stack exchange,提问作者curiousSloth
相关产品推荐
相关产品推荐

