我有一些有效的代码(如下),可以将新的端点成功放置到我的woocommerce帐户菜单中。
因此,我得到了https://website.com/account/pets/
(其中“ / account /”是woocommerce myaccount)。我希望实现的是https://website.com/account/pets/new/
。
我已经尝试搜索此答案,我认为可能是add_rewrite_rule()
,但我不知道该使用什么术语来链接信息。我希望我已经为这个问题提供了足够的信息。
[请注意,/new/
子段未链接到自定义标签/分类法等,它们将有效地成为它们自己的页面(或audp_wc_pets_content()
函数中的isset()变量)。
建议的答案在下面更新,请参考//Answer
function audp_wc_pets_endpoint() {
add_rewrite_endpoint( 'pets', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'audp_wc_pets_endpoint' );
function audp_account_menu_order( $items ) {
// Remove orders and logout menu item.
$orders = $items['orders'];
$logout = $items['customer-logout'];
unset( $items['orders'] );
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items['pets'] = __( 'Pet Details', 'woocommerce' );
// Insert back the logout item.
$items['orders'] = $orders;
$items['customer-logout'] = $logout;
return $items;
}
function audp_wc_pets_content() {
// Answer
$query = get_query_var( 'pets', $default = '' );
if ( isset ( $query ) && ( $query == 'new' ) ) {
// Output "New" HTML/Include;
}
if ( isset ( $query ) && ( $query == 'modify' ) ) {
// Output "Modify" HTML/Include;
}
else {
// Output "Pets" / Redirect to "Pets"
return;
}
}
add_action( 'woocommerce_account_pets_endpoint', 'audp_wc_pets_content' );
谢谢你。
[我想我已经找到了我问题的答案,通过使用get_query_var()
,我可以通过echo或include来控制/pets/
之后的段的输出。如果有更好的解决方案,我仍然欢迎您提出建议。
function audp_wc_pets_content() {
$query = get_query_var( 'pets', $default = '' );
if ( isset ( $query ) && ( $query == 'new' ) ) {
// Output 'New' HTML
}
if ( isset ( $query ) && ( $query == 'modify' ) ) {
// Output 'Modify' HTML
}
else {
// Output/Redirect back to /pets/
return;
}
}
我已相应地更新了问题,以显示完整的工作代码。