PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP-永久链接不适用于自定义帖子和类别大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试在wordpress添加wordpress自定义帖子类型和类别

add_action( 'init', 'work_register' );   

function work_register() {   

$labels = array( 
    'name' => _x('Work', 'post type general name'), 
    'singular_name' => _x('Work Item', 'post type singular name'), 
    'add_new' => _x('Add New', 'work item'), 
    'add_new_item' => __('Add New Work Item'), 
    'edit_item' => __('Edit Work Item'), 
    'new_item' => __('New Work Item'), 

    'view_item' => __('View Work Item'), 
    'search_items' => __('Search Work'), 
    'not_found' => __('Nothing found'), 
    'not_found_in_trash' => __('Nothing found in Trash'), 
    'parent_item_colon' => '' 
);   

$args = array( 
    'labels' => $labels, 
    'public' => true, 
    'publicly_queryable' => true, 
    'show_ui' => true, 
    'query_var' => true, 
    'menu_icon' => get_stylesheet_directory_uri() . '/article16.png', 
    'rewrite' => array( 'slug' => 'work', 'with_front'=> false ), 
    'capability_type' => 'post', 
    'hierarchical' => true,
    'has_archive' => true,  
    'menu_position' => null, 
    'supports' => array('title','editor','thumbnail') 
);   

register_post_type( 'work' , $args ); 

register_taxonomy( 'categories', array('work'), array(
    'hierarchical' => true, 
    'label' => 'Categories', 
    'singular_label' => 'Category', 
    'rewrite' => array( 'slug' => 'categories', 'with_front'=> false )
    )
);

register_taxonomy_for_object_type( 'categories', 'work' );

}

我的永久链接结构是:

/%category%/%postname%/

当我从帖子中添加内容时,它给了我链接

  /category/postname

但是当我从自定义帖子中添加内容时,它只会给我

 /postname 

我也想在自定义帖子中使用/ category / postname

请指出我错了

解决方法:

更改您的重写以添加工作岗位类型查询var:

'rewrite' => array('slug' => 'work/%categories%')

然后过滤post_type_link以将所选类别插入永久链接

function custom_work_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'work' );
        if( $terms ){
            return str_replace( '%categories%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}
add_filter( 'post_type_link', 'custom_work_post_link', 1, 3 );

还有像@L_674_16@这样的插件可以为您完成此操作.

大佬总结

以上是大佬教程为你收集整理的PHP-永久链接不适用于自定义帖子和类别全部内容,希望文章能够帮你解决PHP-永久链接不适用于自定义帖子和类别所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: