B
B
by_makk2018-03-20 11:28:52
WordPress
by_makk, 2018-03-20 11:28:52

Applying a separate template for subcategories with a specific ID?

There is a main heading "Hotels", in there is a database of hotels in the form of records, distributed by countries, and in countries there are subheadings with "Hotel type".
It is necessary for certain subcategories (preferably by ID), to specify which template they use to display the list (category/archive).
Please do not post a link to the WP Codex :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2018-03-20
@by_makk

1. Use a template by hierarchy: category-{id}.php
2. Substitute on the fly through the hook template_include. You still have to go to the Code, because it's all there .

function my_custom_template( $template ) {

    //  Здесь указываете ID необходимых категорий
    $categories = array( 24, 56, 131, 345 );

    // Вот эта проверка нужна для определения условия:
    if ( in_category( $categories ) ) {
    
        // А вот тут указываем какой шаблон подключать:
        $new_template = locate_template( array( 'my-custom-template.php' ) );

        if ( '' != $new_template ) {
            return $new_template;
        }
    }

    return $template;
}
add_filter( 'template_include', 'my_custom_template', 99 );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question