【问题标题】:How do I add the page's slug to a link in a wordpress widget如何将页面的 slug 添加到 wordpress 小部件中的链接
【发布时间】:2018-06-13 20:20:15
【问题描述】:

我已经搜索了一个多小时,所以我真的希望有人可以帮助我。

我有一个包含链接的小部件,例如

<a href="/form/fill-in-form">Fill in our form</a>

但我想在链接中添加页面 slug(以便我可以在表单上看到它)。例如,如果用户在我的页面 mysite.com/listing/redcar 上,我希望他们看到: 例如

<a href="/form/fill-in-form/?myparameter=SLUG">Fill in our form</a>

<a href="/form/fill-in-form/?myparameter=redcar">Fill in our form</a>

但无论我做什么,我都无法让 slug 进入小部件中的 URL

我试过了

<a href="/form/fill-in-form/?myparameter=<?php the_permalink(); ?>">Fill in our form</a> 

还有很多其他的事情,但我就是什么都做不了。

有什么想法吗?

【问题讨论】:

    标签: php wordpress url widget slug


    【解决方案1】:

    使用全局获取post slug

    <?php 
        global $post;
        $post_slug = $post->post_name;
    ?>
    <a href="/form/fill-in-form/?myparameter=<?php echo $post_slug; ?>">Fill in our form</a> 
    

    你也可以通过post id获取slug,这样做

    <?php
    $post_id = POST_ID; //replace it with post id
    $post_data = get_post($post_id); 
    $post_slug = $post_data->post_name;
    ?> 
    <a href="/form/fill-in-form/?myparameter=<?php echo $post_slug; ?>">Fill in our form</a> 
    

    【讨论】:

    • 感谢 Regolith - 我试过了,但链接只是添加了 %3C?php%20echo%20$post_slug;%20?%3E 例如它变成了:mysite.com.au/form/fill-in-form/…
    • 我在小部件中使用它
    • 一切顺利!我将 php 放入我的函数中,但它进入了小部件本身 :) 非常感谢 Regolith
    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2016-11-10
    相关资源
    最近更新 更多