如何在woocommerce页面上添加编辑链接?(How to add edit links on woocommerce pages?)

编程入门 行业动态 更新时间:2024-10-24 20:17:37
如何在woocommerce页面上添加编辑链接?(How to add edit links on woocommerce pages?)

我正在尝试添加编辑链接到我的woocommerce页面以及存档页面上的所有产品。

对于页面本身,我试过这个:

add_filter( 'woocommerce_before_shop_loop', 'woo_page_edit_link', 10 ); function woo_page_edit_link() { edit_post_link( 'e', '', '' ); }

但是,我总是得到列出的第一个产品的ID,而不是页面ID。

对于循环,我尝试了这个:

add_filter('woocommerce_before_shop_loop_item' , 'woo_item_edit_link'); function woo_item_edit_link() { edit_post_link( 'e', '', '' ); }

但编辑链接始终嵌套商店项目链接中。

任何想法如何解决这两个问题?

I'm trying to add edit links to my woocommerce pages as well as all products on archive page.

For the page itself, I tried this:

add_filter( 'woocommerce_before_shop_loop', 'woo_page_edit_link', 10 ); function woo_page_edit_link() { edit_post_link( 'e', '', '' ); }

But instead of the page id, I always get the id of the first product listed.

For the loop, I tried this:

add_filter('woocommerce_before_shop_loop_item' , 'woo_item_edit_link'); function woo_item_edit_link() { edit_post_link( 'e', '', '' ); }

But the edit link always gets nested inside the shop item link.

Any ideas how to resolve both problems?

最满意答案

woocommerce_before_shop_loop在循环标记之前,但产品查询已经设置,因此当前的$post是第一个产品。 您可以使用edit_post_link()的第4个参数来定义特定的页面ID。 WooCommerce有一个内置函数,用于获取其自定义页面的页面ID。

add_filter( 'woocommerce_before_shop_loop', 'woo_page_edit_link', 10 ); function woo_page_edit_link() { edit_post_link( 'edit shop', '', '', wc_get_page_id( 'shop' ) ); }

并且您的项目编辑链接显示在链接内,因为woocommerce_template_loop_product_link_open()被挂钩到woocommerce_before_shop_loop_item ,优先级为10.因此,如果您在相同(或更高)优先级上添加您的功能,则可能会进入链接。 要解决此问题,您可以使用下面的早期优先级。

add_filter('woocommerce_before_shop_loop_item' , 'woo_item_edit_link', 5); function woo_item_edit_link() { edit_post_link( 'edit product' ); }

woocommerce_before_shop_loop is before the loop markup, but the product query is already set up, so the current $post is for the first product. You can use the 4th parameter of edit_post_link() to define a specific page ID. WooCommerce has a built-in function for getting the page IDs of it's custom pages.

add_filter( 'woocommerce_before_shop_loop', 'woo_page_edit_link', 10 ); function woo_page_edit_link() { edit_post_link( 'edit shop', '', '', wc_get_page_id( 'shop' ) ); }

And your item edit link appears inside the link because woocommerce_template_loop_product_link_open() is hooked to woocommerce_before_shop_loop_item with a priority of 10. So if you add your function on the same (or later) priority, you risk being inside the link. To counter this, you can use an earlier priority like below.

add_filter('woocommerce_before_shop_loop_item' , 'woo_item_edit_link', 5); function woo_item_edit_link() { edit_post_link( 'edit product' ); }

更多推荐

本文发布于:2023-08-05 21:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   页面   链接   如何在   woocommerce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!