【问题标题】:Button that links to same product in a different category wordpress woocommerce链接到不同类别 wordpress woocommerce 中的相同产品的按钮
【发布时间】:2016-07-17 02:43:41
【问题描述】:

我正在寻找可以帮助我解决我目前遇到的问题的人。 我正在尝试在我的产品页面上添加一个链接,将用户重定向到同一产品但在另一个类别下:

我们有 2 个不同的横幅集出售:

产品名称:Testproduct 1
产品类别:静态横幅集
info:存在 7 种尺寸的横幅集,带有 JPG 扩展名

产品名称:Testproduct 1
产品类别:交互式横幅集
info:存在 .jpg + HTML5 扩展名的 7 种格式的横幅集

现在,在每个单独的产品页面上,我想要一个链接(基本的 html href 链接或按钮并不重要),将客户重定向到同一产品,但在另一个类别中。例如:

static 横幅集产品页面上,我想要一个链接,上面写着:
我想要交互式版本的这个产品。
现在,如果您单击此链接,我希望将客户从当前网址重定向:http://examplesite.com/shop/static-banners/testproduct-1 到:http://examplesite.com/shop/interactive-banners/testproduct-1

有没有办法做到这一点?
我已经尝试过摆弄一些 php 代码,但我只设法获得了永久链接而不更改 url ...

我希望我的解释足够清楚,可以理解。

提前致谢,

约斯特

【问题讨论】:

    标签: php wordpress hyperlink woocommerce categories


    【解决方案1】:
    <a href="<?php echo home_url(); ?>/shop/interactive-banners/<?php echo $post->post_name; ?>">
    

    -在产品模板的循环中添加它

    【讨论】:

      【解决方案2】:

      您需要做的是检查产品当前显示在哪个类别下。即静态横幅集或交互式横幅集。根据这些信息,您还可以格式化目标链接和/或按钮文本。

      我假设您可以直接使用相应的类别 slug。

      以下是供您参考的代码。

      //get current category object
      $current_category = get_queried_object();
      //get current category id and slug
      $current_cat_id = $current_category->term_id;
      $current_cat_slug = $current_category->slug;
      //identify target category based on current category's slug.
      $target_cat = ($current_cat_slug == 'static-banners') ? 'interactive-banners' : 'static-banners';
      //for display text (if required)
      $display_text = ($current_cat_slug == 'static-banners') ? 'I would like this product in the Interactive version.' : 'I would like this product in the Static version.'
      //get target category link
      $target_cat_link = get_term_link($current_category->term_id);
      //display link as anchor tag
      <a href="<?php echo($target_cat_link.$post->post_name); ?>"><?php echo($display_text); ?></a>
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-12-16
        • 2018-01-17
        • 2018-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-18
        • 1970-01-01
        相关资源
        最近更新 更多