body_class()
и post_class()
if ( ! function_exists( 'get_wpgen_header_classes' ) ) {
/**
* Get classes for header container.
*
* @param string $class Additional header classes.
*
* @return array
*/
function get_wpgen_header_classes( $class = '' ) {
// Add elements to array.
$classes = array();
$classes[] = 'header';
if ( has_custom_header() ) {
$classes[] = 'header--background-image';
}
if ( is_front_page() || is_home() ) {
$classes[] = 'header-bg';
} else {
$classes[] = 'custom-class';
}
// Check the function has accepted any classes.
if ( isset( $class ) && ! empty( $class ) ) {
if ( is_array( $class ) ) {
$classes = array_merge( $classes, $class );
} elseif ( is_string( $class ) ) {
$classes = array_merge( $classes, explode( ' ', $class ) );
}
}
$classes = apply_filters( 'get_wpgen_header_classes', $classes );
// Usage:
/*add_filter( 'get_wpgen_header_classes', 'my_header_classes' );
if ( ! function_exists( 'my_header_classes' ) ) {
function my_header_classes( $classes ) {
$classes[] = 'my-class';
return array_unique( $classes );
}
}*/
return array_unique( (array) $classes );
}
}
if ( ! function_exists( 'wpgen_header_classes' ) ) {
/**
* Display classes for header container.
*
* @param string $class Additional header classes.
* @param bool $echo Echo or return header classes.
*
* @return string
*/
function wpgen_header_classes( $class = '', $echo = true ) {
$classes = get_wpgen_header_classes( $class );
if ( $echo ) {
echo 'class="' . esc_attr( implode( ' ', $classes ) ) . '"';
} else {
return 'class="' . esc_attr( implode( ' ', $classes ) ) . '"';
}
}
}
<header id="header" <?php wpgen_header_classes(); ?>>
<header id="header" <?php wpgen_header_classes( 'new-class' ); ?>>
$separator = '|';
// Title prefixes.
if ( is_post_type_archive() ) {
$title[] = _x( 'Archives:', 'post type archive title prefix', 'wpgen' );
} elseif ( is_category() ) {
$title[] = _x( 'Category:', 'category archive title prefix', 'wpgen' );
} elseif ( is_tag() ) {
$title[] = _x( 'Tag:', 'tag archive title prefix', 'wpgen' );
} elseif ( is_author() ) {
$title[] = _x( 'Author:', 'author archive title prefix', 'wpgen' );
} elseif ( is_date() ) {
if ( is_year() ) {
$title[] = _x( 'Year:', 'date archive title prefix', 'wpgen' );
} elseif ( is_month() ) {
$title[] = _x( 'Month:', 'date archive title prefix', 'wpgen' );
} elseif ( is_day() ) {
$title[] = _x( 'Day:', 'date archive title prefix', 'wpgen' );
}
} elseif ( is_tax( 'post_format' ) ) {
if ( is_tax( 'post_format', 'post-format-aside' ) ) {
$title[] = _x( 'Asides', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-gallery' ) ) {
$title[] = _x( 'Galleries', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-image' ) ) {
$title[] = _x( 'Images', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-video' ) ) {
$title[] = _x( 'Videos', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-quote' ) ) {
$title[] = _x( 'Quotes', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-link' ) ) {
$title[] = _x( 'Links', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-status' ) ) {
$title[] = _x( 'Statuses', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-audio' ) ) {
$title[] = _x( 'Audio', 'post format archive title', 'wpgen' );
} elseif ( is_tax( 'post_format', 'post-format-chat' ) ) {
$title[] = _x( 'Chats', 'post format archive title', 'wpgen' );
}
}
// Main title.
if ( is_single() || is_page() ) {
$title[] = get_the_title();
} elseif ( is_404() ) {
$title[] = __( 'Page not found', 'wpgen' );
} elseif ( is_search() ) {
$title[] = sprintf( __( 'Search Results for “%s”', 'wpgen' ), get_search_query( false ) );
} elseif ( is_post_type_archive() ) {
$title[] = get_queried_object()->label;
} elseif ( is_author() ) {
$title[] = get_queried_object()->display_name ?? '';
} elseif ( is_date() ) {
if ( is_year() ) {
$title[] = get_the_date( 'Y' );
} elseif ( is_month() ) {
$title[] = get_the_date( 'F Y' );
} elseif ( is_day() ) {
$title[] = get_the_date( 'F j, Y' );
}
} elseif ( is_archive() ) {
$title[] = get_queried_object()->name;
}
// Add site info in the title.
if ( ! isset( $title ) ) {
$title[] = get_bloginfo( 'name' );
$title[] = $separator;
$title[] = get_bloginfo( 'description' );
} else {
if ( is_paged() ) {
$title[] = $separator;
$title[] = __( 'Page', 'wpgen' ) . ' ' . $paged;
}
$title[] = $separator;
$title[] = get_bloginfo( 'name' );
}
$title = implode( ' ', $title );
wp_get_nav_menu_items()
и вывести свою разметку// Получаем элементы меню по ID.
$nav_menu_items = wp_get_nav_menu_items( $menu_id );
// Или получаем элементы меню по location.
$menu_location = 'primary';
$locations = get_nav_menu_locations();
if ( isset( $locations[ $menu_location ] ) ) {
$nav_menu_items = wp_get_nav_menu_items( $locations[ $menu_location ] );
}
__()
, _e()
, esc_html__()
, esc_html_e()
, _x()
и _ex()
_x( 'Read', 'past participle: books I have read', 'text_domain' );
load_theme_textdomain()
register_post_type()
и register_taxonomy()
get_template_part( 'templates/archive/post', get_post_type() );
pre_get_posts
wp_get_nav_menu_items()
по переданному ID или location// Получаем элементы меню по ID.
$nav_menu_items = wp_get_nav_menu_items( $menu_id );
// Получаем элементы меню по location.
$menu_location = 'primary';
$locations = get_nav_menu_locations();
if ( isset( $locations[ $menu_location ] ) ) {
$nav_menu_items = wp_get_nav_menu_items( $locations[ $menu_location ] );
}
pre_get_posts
. Используйте проверки is_date()
, is_year()
, is_month()
и is_day()
template_redirect
с теми же проверкамиfunction get_yoast_custom_title() {
if ( is_single() ) {
$string = get_the_title();
} else {
$string = 'Заголовок по умолчанию';
}
return $string;
}
// Define the action for register yoast_variable replacments.
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%CustomTitle%%', 'get_yoast_custom_title', 'advanced', __( 'Some instead title text', 'wpgen' ) );
}
// Add action.
add_action( 'wpseo_register_extra_replacements', 'register_custom_yoast_variables' );
function get_category_post_count() {
$string = '';
if ( is_category() && isset( get_queried_object()->count ) ) {
$string = get_queried_object()->count;
}
return $string;
}
// Define the action for register yoast_variable replacments.
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%PostCount%%', 'get_category_post_count', 'advanced', __( 'Some instead title text', 'wpgen' ) );
}
// Add action.
add_action( 'wpseo_register_extra_replacements', 'register_custom_yoast_variables' );
register_post_type()
указал аргумент 'has_archive' => 'repetitors'
, это общий архив для всех записей этого типа. Так же вам будет достаточно регистрации одной таксономииadd_action( 'init', 'education_register_post_type' );
function education_register_post_type() {
// Taxonomy repetitor_cat.
register_taxonomy( 'repetitor_cat', array( 'repetitor' ), array(
'labels' => array(
'name' => _x( 'Repetitor category', 'taxonomy general name', 'education' ),
'singular_name' => _x( 'Repetitor categories', 'taxonomy singular name', 'education' ),
'search_items' => __( 'Search Repetitor category', 'education' ),
'popular_items' => __( 'Popular Repetitor category', 'education' ),
'all_items' => __( 'All Repetitor categories', 'education' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Repetitor category', 'education' ),
'update_item' => __( 'Update Repetitor category', 'education' ),
'add_new_item' => __( 'Add new Repetitor category', 'education' ),
'new_item_name' => __( 'New Repetitor category name', 'education' ),
'menu_name' => __( 'Repetitor categories', 'education' ),
),
'public' => true,
'show_tagcloud' => true,
'hierarchical' => false,
'query_var' => true,
'show_in_quick_edit' => true,
'sort' => true,
) );
// Post type repetitor.
register_post_type( 'repetitor', array(
'labels' => array(
'name' => __( 'Repetitors', 'education' ),
'singular_name' => __( 'Repetitor', 'education' ),
'add_new' => __( 'Add Repetitor', 'education' ),
'add_new_item' => __( 'Add new Repetitor', 'education' ),
'edit_item' => __( 'Edit Repetitor', 'education' ),
'new_item' => __( 'New Repetitor', 'education' ),
'view_item' => __( 'View Repetitor', 'education' ),
'search_items' => __( 'Search Repetitor', 'education' ),
'not_found' => __( 'Repetitor not found', 'education' ),
'not_found_in_trash' => __( 'Repetitor not found in trash', 'education' ),
'menu_name' => __( 'Repetitors', 'education' ),
),
'public' => true,
'show_in_rest' => true, // Включает Gutenberg.
'capability_type' => 'post',
'hierarchical' => false,
'has_archive' => 'repetitors',
'rewrite' => array( 'slug' => 'repetitors', 'with_front' => false, 'pages' => true, 'feeds' => false, 'feed' => false ),
'query_var' => true,
'supports' => array( 'page-attributes', 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
'taxonomies' => array( 'repetitor_cat' ),
) );
}
rewrite
и фильтры post_type_link
и term_link
$wp_query
, проверить можно распечатав этот запросglobal $wp_query;
var_dump( $wp_query );
pre_get_posts
add_action( 'pre_get_posts', 'blog_posts' );
function blog_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_page_template( 'blog.php' ) ) {
$query->set( 'post_type', 'post' );
$query->set( 'posts_per_page', '10' );
}
}
wp_query()
, но учитывайте, что функция пагинации the_posts_navigation()
работает только с глобальным запросом