Add Archive Page Headers

From IThemes Codex
Jump to: navigation, search

If you want to show the header that appears on Archive and Search Results pages ("Archive for etc."), you can do so using a shortcode. The following code is taken from any theme's archive.php page, you can add it at the end of your functions.php file (but before the closing ?>, if any).

Code

function my_lb_archives_header() {
    
    if ( is_category() ) { // Category Archive
        $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), single_cat_title( '', false ) );
    }
    else if ( is_search() ) { // Tag Archive
        $title = sprintf( __( 'You searched for %s', 'it-l10n-Builder' ), get_search_query());
    }
    else if ( is_tag() ) { // Tag Archive
        $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), single_tag_title( '', false ) );
    }
    else if ( is_tax() ) { // Taxonomyg Archive
        $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), builder_get_tax_term_title() );
    }
    else if ( function_exists( 'is_post_type_archive' ) && is_post_type_archive() && function_exists( 'post_type_archive_title' ) ) { // Post Type Archive
            $title = post_type_archive_title( '', false );
    }
    else if ( is_author() ) { // Author Archive
        $title = sprintf( __( 'Author Archive for %s', 'it-l10n-Builder' ), get_the_author() );
    }
    else if ( is_year() ) { // Year-Specific Archive
        $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), get_the_time( 'Y' ) );
    }
    else if ( is_month() ) { // Month-Specific Archive
        $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), get_the_time( 'F Y' ) );
    }
    else if ( is_day() ) { // Day-Specific Archive
        $title = sprintf( __( 'Archive for %s', 'it-l10n-Builder' ), get_the_date() );
    }
    else if ( is_time() ) { // Time-Specific Archive
        $title = __( 'Time Archive', 'it-l10n-Builder' );
    }
    else { // Default catchall just in case
        $title = __( 'Archive', 'it-l10n-Builder' );
    }

    // wrap result in h4 tags
    $output = '<h4 class="loop-title">' . $title .  '</h4>';
    
    return $output;
}
add_shortcode( 'archives-header', 'my_lb_archives_header' );

Usage

Insert the shortcode [archives-header] in the "Pre-Loop Text / HTML" field in your LoopBuddy layout editor for the specific layouts you created.


← Back to LoopBuddy Codex Home

Personal tools
Namespaces
Variants
Actions
iThemes Codex
Codex Navigation
Toolbox