Answer the question
In order to leave comments, you need to log in
How to implement site regionalization in WordPress?
How can I implement the regionality of the site on WordPress 4?
While solving the problem, I ran into problems:
Answer the question
In order to leave comments, you need to log in
Played around a bit and fixed the problem.
First, the problem with the same page slugs in WordPress:
Everything is ridiculously simple, we create pages, and select the "Parent element" of the necessary ones.
For example:
site.ru/moskva/article
site.ru/kazan/article
where moskva and kazan are the parent pages of two different article pages respectively.
With this, everything is simple and clear.
Next, I created "custom fields" for the city pages - "tel" and "address".
WordPress added the following lines to the functions.php of the theme:
// узнаем самый первый родительский элемент (страницу города)
function parent_postID() {
global $post;
$postID = $post->ID;
if ($post->post_parent) {
$anc = get_post_ancestors($postID);
$root = count($anc)-1;
$parentID = $anc[$root];
} else {
$parentID = $postID;
}
return $parentID;
}
// узнаем самую корневую страницу
function show_gorod() {
global $post;
$postID = $post->ID;
$parentID = parent_postID();
if (!is_front_page() and ($parentID or $postID)) {
$city_name = get_the_title($parentID);
$city_link = get_the_permalink($parentID);
$city_class = 'gorod';
$city_string = '<a class="'.$city_class.'" href="'.$city_link.'">'.$city_name.'</a>';
} else {
$city_name = 'Выберите город:';
$city_class = 'gorod small';
$city_string = '<p class="'.$city_class.'">'.$city_name.'</p>';
}
return $city_string;
}
<?php echo show_gorod() ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question