Viết lại đường dẫn permalink, url new.accesspath.com (ok)

C:\Users\Administrator\AppData\Local\Temp\scp41437\wp-content\themes\foxiz-child\functions.php

<?php
/* foxiz child theme */
add_action('init', 'wpcom_kx_init');
if (!function_exists('wpcom_kx_init')) :
  function wpcom_kx_init()
  {
    global $options;
      $slug = 'news-flash';
      $labels = array(
        'name' => '快讯',
        'singular_name' => '快讯',
        'add_new' => '添加',
        'add_new_item' => '添加',
        'edit_item' => '编辑',
        'new_item' => '添加',
        'view_item' => '查看',
        'search_items' => '查找',
        'not_found' => '没有内容',
        'not_found_in_trash' => '回收站为空',
        'parent_item_colon' => '',
      );
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'capability_type' => 'page',
        'hierarchical' => true,
        'menu_position' => null,
        'rewrite' => array('slug' => $slug),
        'show_in_rest' => true,
        'supports' => array('title', 'excerpt', 'thumbnail', 'comments'),
      );
      register_post_type('kuaixun', $args);
      // add post meta
      add_filter('wpcom_post_metas', 'wpcom_add_kx_metas');
  }
endif;
add_action('pre_get_posts', 'wpcom_kx_orderby');
function wpcom_kx_orderby($query)
{
  if (function_exists('get_current_screen') && $query->is_admin) {
    $screen = get_current_screen();
    if (isset($screen->base) && isset($screen->post_type) && 'edit' == $screen->base && 'kuaixun' == $screen->post_type && !isset($_GET['orderby'])) {
      $query->set('orderby', 'date');
      $query->set('order', 'DESC');
    }
  }
}
add_action('init', 'wpcom_kx_rewrite');
function wpcom_kx_rewrite()
{
  global $wp_rewrite, $options, $permalink_structure;
    if (!isset($permalink_structure)) {
      $permalink_structure = get_option('permalink_structure');
    }
    if ($permalink_structure) {
      $slug = 'news-flash';
      $queryarg = 'post_type=kuaixun&p=';
      $wp_rewrite->add_rewrite_tag('%kx_id%', '([^/]+)', $queryarg);
      $wp_rewrite->add_permastruct('kuaixun', $slug . '/%kx_id%.html', false);
      //$wp_rewrite->add_permastruct('kuaixun', $slug . '/%kx_id%.html', array('with_front' => true));
    }
}
add_filter('post_type_link', 'wpcom_kx_permalink', 5, 2);
function wpcom_kx_permalink($post_link, $id)
{
  global $wp_rewrite, $permalink_structure, $options;
    if (!isset($permalink_structure)) {
      $permalink_structure = get_option('permalink_structure');
    }
    if ($permalink_structure) {
      $post = get_post($id);
      if (!is_wp_error($post) && $post->post_type == 'kuaixun') {
        $newlink = $wp_rewrite->get_extra_permastruct('kuaixun');
        $newlink = str_replace('%kx_id%', $post->ID, $newlink);
        $newlink = home_url(untrailingslashit($newlink));
        return $newlink;
      }
    }
  return $post_link;
}
function wpcom_kx_custom_rewrite_rule() {
  $slug = 'news-flash';
  //add_rewrite_rule('^' . $slug . '/([0-9]+)/?$', 'index.php?post_type=kuaixun&p=$matches[1]', 'top');
  add_rewrite_rule('^' . $slug . '/?$', 'index.php?post_type=kuaixun', 'top');
}
add_action('init', 'wpcom_kx_custom_rewrite_rule');
// 更新标签链接中的名称为 ID
add_filter( 'tag_link', 'wpcom_tag_id_link', 10, 2 );
function wpcom_tag_id_link( $taglink, $tag_id ) {
  $tag = get_tag( $tag_id );
  if ( $tag ) {
    return get_site_url() . '/tag/' . $tag->term_id;
  }
  return $taglink;
}
// 添加自定义重写规则
function wpcom_tag_rewrite() {
  add_rewrite_rule( '^tag/([0-9]+)/?', 'index.php?tag_id=$matches[1]', 'top' );
}
add_action( 'init', 'wpcom_tag_rewrite' );
// 修改查询参数
function wpcom_tag_id_query_vars( $query_vars ) {
  $query_vars[] = 'tag_id';
  return $query_vars;
}
add_filter( 'query_vars', 'wpcom_tag_id_query_vars' );
// 修改 WP_Query
function wpcom_tag_id_wp_query( $wp_query ) {
  if ( isset( $wp_query->query_vars['tag_id'] ) ) {
    $tag_id = $wp_query->query_vars['tag_id'];
    $wp_query->set( 'tag_id', $tag_id );
  }
}
add_action( 'pre_get_posts', 'wpcom_tag_id_wp_query' );
add_filter( 'category_link', 'wpcom_remove_category_base', 10, 2 );
function wpcom_remove_category_base( $category_link, $category ) {
  $category_link = str_replace( '/category/', '/', $category_link );
  return $category_link;
}
function ecademy_enqueue_style()
{
  wp_enqueue_style("custom-css", get_stylesheet_directory_uri() . "/assets/css/custom.css", array(), '1.0.0');
  wp_enqueue_script('custom-js', get_stylesheet_directory_uri() . '/assets/js/custom.js', array('jquery-core'), '1.0.1', 'true');
  wp_localize_script('custom-js', 'my_ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}
add_action('wp_enqueue_scripts', 'ecademy_enqueue_style');
hp

Last updated