Last active
June 5, 2018 13:25
-
-
Save levnhub/a771b91ca22e5afd61953355dfdc06e6 to your computer and use it in GitHub Desktop.
WP Category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Заменяем шаблон post зависимости от категории (functions.php) | |
add_filter('template_include', 'service_template'); | |
function service_template( $template ) { | |
global $post; | |
if( ( is_single() && in_category( 1, $post ) ) ) { | |
$template = get_stylesheet_directory() . '/single-service.php'; | |
} | |
return $template; | |
} | |
// Выведем посты конкретной категории | |
// параметры по умолчанию | |
$args = array( | |
'numberposts' => 5, | |
'category' => 0, | |
'orderby' => 'date', | |
'order' => 'DESC', | |
'include' => array(), | |
'exclude' => array(), | |
'meta_key' => '', | |
'meta_value' =>'', | |
'post_type' => 'post', | |
'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса | |
); | |
$posts = get_posts( $args ); | |
foreach($posts as $post){ setup_postdata($post); | |
// формат вывода | |
} | |
wp_reset_postdata(); // сброс | |
// Выведем на экран данные категории, на странице которой мы находимся (текущей): | |
$thisCat = get_category(get_query_var('cat'),false); | |
print_r($thisCat); | |
/* | |
В результате мы увидим нечто подобное: | |
stdClass Object | |
( | |
[term_id] => 85 | |
[name] => Category Name | |
[slug] => category-name | |
[term_group] => 0 | |
[term_taxonomy_id] => 85 | |
[taxonomy] => category | |
[description] => | |
[parent] => 70 | |
[count] => 0 | |
[cat_ID] => 85 | |
[category_count] => 0 | |
[category_description] => | |
[cat_name] => Category Name | |
[category_nicename] => category-name | |
[category_parent] => 70 | |
) | |
*/ | |
// Получим ID категории поста на странице которого мы находимся | |
$category = get_the_category(); | |
echo $category[0]->term_id; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment