-1

I'm newbie in wordpress and wondering how can I set custom <h1> for categories in wordpress. At the moment: <h1>Category:Category name</h1>. I've spent half day to find out how to do that. Advices from articles don't work.

Plugins: Yoast SEO, All-in-one SEO pack, WP SEO Meta Editing functions.php in theme folder

add_action("category_edit_form_fields", 'mayak_category_meta');
function mayak_category_meta( $term ) {
    ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label>Заголовок (title)</label></th>
            <td>
                <input type="text" name="mayak[title]" value="<?php echo esc_attr( get_term_meta( $term->term_id, 'title', 1 ) ) ?>"><br />
                <p class="description">Не более 60 знаков, включая пробелы</p>
            </td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label>Заголовок h1</label></th>
            <td>
                <input type="text" name="mayak[h1]" value="<?php echo esc_attr( get_term_meta( $term->term_id, 'h1', 1 ) ) ?>"><br />
                <p class="description">Заголовок страницы</p>
            </td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label>Ключевые слова</label></th>
            <td>
                <input type="text" name="mayak[keywords]" value="<?php echo esc_attr( get_term_meta( $term->term_id, 'keywords', 1 ) ) ?>"><br />
                <p class="description">Ключевые слова (keywords)</p>
            </td>
        </tr>
    <?php
}

function mayak_save_meta( $term_id ) {
    if ( ! isset($_POST['mayak']) )
        return;
    $mayak = array_map('trim', $_POST['mayak']);
    foreach( $mayak as $key => $value ){
        if( empty($value) ){
            delete_term_meta( $term_id, $key );
            continue;
        }
        update_term_meta( $term_id, $key, $value );
    }
    return $term_id;
}
add_action("create_category", 'mayak_save_meta');
add_action("edited_category", 'mayak_save_meta');

function mayak_filter_single_cat_title($term_name) {
    $terms = get_category( get_query_var('cat'));
    $cat_id = $terms->cat_ID;
    $term_name = get_term_meta ($cat_id, 'title', true);
    return $term_name; 
}
add_filter('single_cat_title', 'mayak_filter_single_cat_title', 10, 1 );

function mayak_single_cat_title ($term_name){
    if(empty($term_name)){
        $terms = get_category( get_query_var( 'cat' ));
        $cat_id = $terms->cat_ID;
        $term_name = get_cat_name($cat_id);
    }
    return $term_name;
}
add_filter( 'single_cat_title', 'mayak_single_cat_title', 10, 1 );

function mayak_cat_caption($caption) {
    $terms = get_category( get_query_var( 'cat' ));
    $cat_id = $terms->cat_ID;
    $caption = get_cat_name($cat_id);
    echo $caption;
}

function mayak_cat_h1($name_cat) {
    $terms = get_category( get_query_var( 'cat' ));
    $cat_id = $terms->cat_ID;
    $name_cat = get_term_meta ( $cat_id, 'h1', true );
    echo $name_cat;
    if(empty($name_cat)){
       echo mayak_cat_caption($caption);
    }
}
2
  • Could you add the code you are trying to modify and the result that you are trying to get? The question does not really make sense, man. Please try to be as clear as possible and provide your code and examples. Sep 10, 2019 at 19:33
  • I've just edited my question and added code that I added to functions.php . After that I got <h1>Category:Title value</h1> instead of <h1>h1 value</h1>
    – Tallik
    Sep 10, 2019 at 19:39

1 Answer 1

0

The Solution was to edit archive.php file in theme folder and adding this code:

 <?php
                    $term = get_queried_object();
                    $term_h1 = get_field('h1', $term);
                    if ( empty( $term_h1 ) ) 
                        the_archive_title( '<h1 class="page-title">', '</h1>' );
                    else 
                        echo '<h1 class="page-title">' . $term_h1 . '</h1>';
                    the_archive_description( '<div class="taxonomy-description">', '</div>' );
            ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.