我想到的是通过global $post
, 下一步是通过$post->post_name
, 接下来检查slug是否需要使用utm查询更新链接。
就像这样。
// get the current post
global $post;
// get the post slug, if not set then set it to empty string
$post_slug = $post->post_name ?? \'\';
// an array of slugs that need to add utm to link
$utm_slugs = [
\'slug-1\',
\'slug-2\',
\'slug-3\'
];
// set default value (empty string), in case no slug was matched
$utm_query = \'\';
// if post slug exists and in the utm slugs array, set a utm query
if (!empty($post_slug) && in_array($post_slug, $utm_slugs)) $utm_query = \'?utm_source=\' . $post_slug;
现在我们已经处理了php部分,我们需要创建链接
这只是一个示例,因此请相应地进行更改。
<a href="http://domain.com/<?= $utm_query; ?>">Some link</a>