我们正在try 从WordPress的CPT中删除Slug. 我们确实使用Blow代码做到了这一点,但我们有数百个CPT,并在我们的子主题函数中重复代码.php看起来非常难看

`add_filter( 'post_type_link', 'cxc1_product_remove_slug', 10, 3 );
function cxc1_product_remove_slug( $post_link, $post, $leavename ) {

    // product is my post type
    if ( 'taller_marmo' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}



add_filter( 'post_type_link', 'cxc2_product_remove_slug', 10, 3 );
function cxc2_product_remove_slug( $post_link, $post, $leavename ) {

    // product is my post type
    if ( '16arlington' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'cxc3_product_remove_slug', 10, 3 );
function cxc3_product_remove_slug( $post_link, $post, $leavename ) {

    // product is my post type
    if ( 'alemais' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}`your text``
add_action( 'pre_get_posts', 'cxc_parse_request_function' );
function cxc_parse_request_function( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'page','taller_marmo', '16arlington', 'alemais' ) ); 
    }
}

有没有办法将第一块代码组合到一个函数中?

有没有办法将第一块代码组合到一个函数中?

推荐答案

您可以创建一个更通用的函数来处理多个自定义POST类型的插件的移除.该代码定义了单个函数cxc_remove_custom_post_type_slug,该函数处理多个定制POST类型的插件的移除.您只需根据需要在$custom_post_types数组中添加或删除帖子类型(Define your array with hundreds of CPT).这使您的代码更简洁、更易于维护,因为您不必 for each 定制帖子类型重复类似的代码.

add_filter('post_type_link', 'cxc_remove_custom_post_type_slug', 10, 3);
function cxc_remove_custom_post_type_slug($post_link, $post, $leavename) {

    // Define an array of custom post types without slugs
    $custom_post_types = array('taller_marmo', '16arlington', 'alemais');

    // Check if the current post type is in the array and is published
    if (in_array($post->post_type, $custom_post_types) && 'publish' == $post->post_status) {
        $post_link = str_replace('/' . $post->post_type . '/', '/', $post_link);
    }

    return $post_link;
}

add_action('pre_get_posts', 'cxc_parse_request_function');
function cxc_parse_request_function($query) {

    if (!$query->is_main_query() || 2 != count($query->query) || !isset($query->query['page'])) {
        return;
    }

    if (!empty($query->query['name'])) {
        // Add your custom post types to the array
        $query->set('post_type', array('post', 'page', 'taller_marmo', '16arlington', 'alemais'));
    }
}

Php相关问答推荐

如果活动订阅者的购物车中有订阅项目,则在WooCommerce签出中显示通知

如何在Woocommerce中禁用机器人添加到推车?

在函数内部获取一致的单选按钮值以更新WordPress用户数据

在php中有没有办法比较两个包含相同枚举类型的枚举数组

PHP中的圆生成器在国际日期变更线上失败

为什么正则表达式与得到的文本块之前得到的也行?

通过DO LAMP进行身份验证并从邮箱发送邮箱的最简单方式是什么

如何在WooCommerce产品子类别中显示特定内容

无法使用php IMAP从Gmail获取收件箱

从目录文件夹中获取文件并追加到 Select 下拉列表(选项)-wordpress/wc

使用php ZipArhive类将Zip压缩文件分成多个部分

Laravel路由到控制器传输变量解决方案需要

使用DOMPDF获取PDF格式的本 map 像

在PHP项目中安装OpenAI PHP SDK时出现问题

无额外字段的Laravel同步

正则表达式将文本替换为标签 html 以字符开头

将 WooCommerce 0 销售价格替换为自定义文本,并保留常规价格删除线

使用 splat 运算符时按引用传递 (...)

如何将 2021-04-08T22:25:09.335541Z 作为时间戳保存到 Laravel 中的数据库中?

getenv() 有时在 CodeIgniter 4 中返回 false.为什么?