• Resolved Ov3rfly

    (@ov3rfly)


    Another issue with 1.8.2.1 and child themes which we found when adding a custom category filter to WP_Query() in latest news section.

    When putting a file sections/latest_news.php in child theme folder, it should be used automatically but it is not. The reason is in front-page.php of parent theme:

    if( isset($zerif_latestnews_show) && $zerif_latestnews_show != 1 ):
    
    	include get_template_directory() . "/sections/latest_news.php";
    
    endif;

    The problem is the use of get_template_directory() which returns the parent theme folder.

    The correct way to include files compatible for use with a child theme would be like this (docs here):

    if( isset($zerif_latestnews_show) && $zerif_latestnews_show != 1 ):
    
    	get_template_part( 'sections/latest_news' ); // NO trailing .php
    
    endif;

    To the theme authors: Thanks for your great theme!

    It would be even better if you could change all include get_template_directory() .. instances to get_template_part(..) in front-page.php in next release, thanks.

    To users: As a workaround for now also copy front-page.php to child-theme folder and apply the fix by hand where necessary.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Ov3rfly

    (@ov3rfly)

    PS. Here is how to add a category filter for child themes to latest news area in file sections/latest_news.php, current code in 1.8.2.1:

    $args = array( 'post_type' => 'post', 'posts_per_page' => $total_posts, 'order' => 'DESC');
    $loop = new WP_Query( $args );

    Added filter

    $args = array( 'post_type' => 'post', 'posts_per_page' => $total_posts, 'order' => 'DESC');
    $zerif_latestnews_cat = apply_filters( 'zerif_latest_news_cat', '' );
    if ( !empty($zerif_latestnews_cat) ):
    	$args['cat'] = $zerif_latestnews_cat;
    endif;
    $loop = new WP_Query( $args );

    The filter zerif_latest_news_cat then can be used from functions.php in child theme with return values like:

    • empty = all categories
    • ‘1,4,5’ = posts with one of category-ID 1, 4, 5
    • ‘-1’ = all posts, but not with category-ID 1
    • and similar, docs here

    Ov3rfly you rock!

    Thanks for this one as well.

    @ov3rfly thank you so much for all your work here ??
    We are definitely going to use get_template_part as you mentioned, and we will try to add as much hooks as posible to help users with child themes.

    Regards,
    Rodica

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Child theme fix: use get_template_part instead include get_template_directory…’ is closed to new replies.