Улучшим SEO показатели категорий, а именно сделаем возможность прописывать свои заголовки H1. После этого можно будет делать любые красивые заголовки с ключевыми словами.
Что же приступим. Для этого нам понадобится разместить нижеуказанный код в functions.php вашего шаблона:
Теперь открываем файл archive.php и надо привести старый заголовок к такому виду:
Так же если у вас не выводится описаний категорий, то вам поможет данный код вставленный ниже заголовка:
Вот и всё теперь у вас должны быть свои заголовки h1 в категориях.
Что же приступим. Для этого нам понадобится разместить нижеуказанный код в functions.php вашего шаблона:
PHP:
add_filter( 'get_the_archive_title', 'get_the_archive_title_change');
function get_the_archive_title_change($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
}
return $title;
}
// h1
if ( function_exists( 'get_term_meta' ) ) :
/**
* Получаем всю таксономию и устанавливаем приоритет действия до 99
*/
function add_taxonomy_actions() {
$get_taxonomies = get_taxonomies(array('public' => true));
if ( is_array($get_taxonomies) ) {
foreach ($get_taxonomies as $get_taxonomy) {
add_action( $get_taxonomy . '_add_form_fields', 'add_taxonomy_header_field', 10, 2 );
add_action( 'created_' . $get_taxonomy, 'save_taxonomy_header_field', 10, 2 );
add_action( $get_taxonomy . '_edit_form_fields', 'edit_taxonomy_header_field', 10, 2 );
add_action( 'edited_' . $get_taxonomy, 'update_taxonomy_header_field', 10, 2 );
}
}
}
add_action('init', 'add_taxonomy_actions', 99);
/**
* Добавляем и сохраняем поле таксономии при создании формы
*/
//add_action( 'category_add_form_fields', 'add_taxonomy_header_field', 10, 2 );
function add_taxonomy_header_field( $taxonomy ) {
?>
<div class="form-field term-group">
<label for="taxonomy_header">Заголовок</label>
<input name="taxonomy_header" id="taxonomy_header" type="text" value="" size="40">
<p class="description">Заголовок H1 в архивах таксономий</p>
</div>
<?php
}
//add_action( 'created_category', 'save_taxonomy_header_field', 10, 2 );
function save_taxonomy_header_field( $term_id, $tt_id ){
if( isset( $_POST['taxonomy_header'] ) && '' !== $_POST['taxonomy_header'] ){
$taxonomy_header = trim( $_POST['taxonomy_header'] );
add_term_meta( $term_id, 'taxonomy_header', $taxonomy_header, true );
}
}
/**
* Добавляем и сохраняем поле таксономии в форме редактирования
*/
//add_action( 'category_edit_form_fields', 'edit_taxonomy_header_field', 10, 2 );
function edit_taxonomy_header_field( $term, $taxonomy ){
// get current
$feature_group = get_term_meta( $term->term_id, 'taxonomy_header', true );
?><tr class="form-field term-group-wrap">
<th scope="row"><label for="taxonomy_header">Заголовок</label></th>
<td>
<input name="taxonomy_header" id="taxonomy_header" type="text" value="<?php echo $feature_group ?>" size="40">
<p class="description">Заголовок H1 в архивах таксономий</p>
</td>
</tr><?php
}
//add_action( 'edited_category', 'update_taxonomy_header_field', 10, 2 );
function update_taxonomy_header_field( $term_id, $tt_id ){
if( isset( $_POST['taxonomy_header'] ) && '' !== $_POST['taxonomy_header'] ){
$taxonomy_header = trim( $_POST['taxonomy_header'] );
update_term_meta( $term_id, 'taxonomy_header', $taxonomy_header );
}
}
/**
* Изменяем the_archive_title
*/
add_filter( 'get_the_archive_title', 'taxonomy_header_archive_title' );
function taxonomy_header_archive_title( $title ) {
if ( is_tax() || is_category() || is_tag() ) {
$taxonomy_header = get_term_meta(get_queried_object()->term_id, 'taxonomy_header', true);
if ( ! empty($taxonomy_header) ) {
$title = $taxonomy_header;
}
}
return $title;
}
endif;
Теперь открываем файл archive.php и надо привести старый заголовок к такому виду:
PHP:
<?php the_archive_title( '<h1>', '</h1>' ); ?>
PHP:
<?php the_archive_description( '<div class="description">', '</div>' ); ?>
Вот и всё теперь у вас должны быть свои заголовки h1 в категориях.