Как добавить виртуальные страницы на пользовательский тип сообщения - PullRequest
0 голосов
/ 21 декабря 2018

Созданный пользовательский тип записи называется "city" под двумя пользовательскими таксономиями "continent" & "country"

"европа" - это пользовательская таксономия под "continent"

"Испания" - это пользовательскаяТаксономия под "country"

Я изменил структуру постоянных ссылок.

Настраиваемая перезапись Slug: /%continent%/%country%

Итак, моя постоянная ссылка выглядит следующим образом mysite.com/europe/spain/madrid/

add_filter('post_link', 'continent_permalink', 10, 3);
add_filter('post_type_link', 'continent_permalink', 10, 3);

function continent_permalink($permalink, $post_id, $leavename) {
  if (strpos($permalink, '%continent%') === FALSE) return $permalink;
    $post = get_post($post_id);
    if (!$post) return $permalink;
    $terms = wp_get_object_terms($post->ID, 'continent');
    if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
      $taxonomy_slug = $terms[0]->slug;
    else
      $taxonomy_slug = 'continent';
  return str_replace('%continent%', $taxonomy_slug, $permalink);
}


add_filter('post_link', 'country_permalink', 10, 3);
add_filter('post_type_link', 'country_permalink', 10, 3);

function country_permalink($permalink, $post_id, $leavename) {
  if (strpos($permalink, '%country%') === FALSE) return $permalink;
    $post = get_post($post_id);
    if (!$post) return $permalink;
    $terms = wp_get_object_terms($post->ID, 'country');
    if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
      $taxonomy_slug = $terms[0]->slug;
    else
      $taxonomy_slug = 'country';
  return str_replace('%country%', $taxonomy_slug, $permalink);
}

Теперь я хочу иметь virtual pages после городских постов, которые"Мадрид" и будет выглядеть следующим образом

mysite.com/europe/spain/madrid/archives
mysite.com/europe/spain/madrid/features
mysite.com/europe/spain/madrid/details

Но, кажется, не могу заставить его работать

он работает, когда я делаю это так

mysite.com/europe/spain/madrid/?details=archives

но еслиЯ вот так вот так не работает

mysite.com/europe/spain/madrid/archives (going to 404)

Вот код, который я использую для добавления виртуальных страниц

function example_details_query_vars($vars) {
  $vars[] = 'details';
  return $vars;
}
add_filter('query_vars', 'example_details_query_vars');

function example_details_add_rewrite_rules() {
  add_rewrite_tag('%details%', '([^&]+)');
  add_rewrite_rule(
    'archives/?$',
    'index.php?details=archives',
    'bottom'
  );

}
add_action('init', 'example_details_add_rewrite_rules');

/**
 * Assign templates to the virtual pages.
 */
function example_details_template_include($template) {
  global $wp_query;
  $new_template = '';

  if (array_key_exists('details', $wp_query->query_vars)) {
    switch ($wp_query->query_vars['details']) {
      case 'archives':
        $new_template = locate_template(array(
          'custom.php'
        ));
        break;
    }

    if ($new_template != '') {
      return $new_template;
    } else {
      $wp_query->set_404();
      status_header(404);
      return get_404_template();
    }
  }

  return $template;
}
add_filter('template_include', 'example_details_template_include');
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...