WordPress.org

Ready to get started?Download WordPress

Forums

Optimizing Code (2 posts)

  1. Alex Vasquez
    Member
    Posted 2 years ago #

    I'm creating a custom child theme w/ Genesis. The home page is going to be static. I've created a home page template and the necessary widget areas in the child's functions.php file.

    I've created the following widget areas in functions.php

    // Widget Areas
    	genesis_register_sidebar( array(
    	    'id'            => 'home-feature',
    	    'name'          => __( 'Home Feature Article', 'NRG' ),
    	    'description'   => __( 'This is a widget area for your home page featured article area', 'NRG' ),
    	) );
    	genesis_register_sidebar( array(
    	    'id'            => 'home-left-feature',
    	    'name'          => __( 'Home Feature Left', 'NRG' ),
    	    'description'   => __( 'This is a widget area for your home page featured article area', 'NRG' ),
    	) );
    	genesis_register_sidebar( array(
    	    'id'            => 'home-right-feature',
    	    'name'          => __( 'Home Feature Right', 'NRG' ),
    	    'description'   => __( 'This is a widget area for your home page featured article area', 'NRG' ),
    	) );
    	genesis_register_sidebar( array(
    	    'id'            => 'home-bottom-feature',
    	    'name'          => __( 'Home Feature Bottom', 'NRG' ),
    	    'description'   => __( 'This is a widget area for your home page featured article area', 'NRG' ),
    	) );

    For the home page template, here's what I have:

    <?php
    /**
     * WARNING: This file is part of the core Genesis framework. DO NOT edit
     * this file under any circumstances. Please do all modifications
     * in the form of a child theme.
     *
     * Template Name: Home Page Template
     */?>
    <?php get_header(); ?>
    
    <?php genesis_before_content_sidebar_wrap(); ?>
    <div id="content-sidebar-wrap">
    
    	<?php genesis_before_content(); ?>
    	<div id="content" class="hfeed">
    
    		<?php if (!dynamic_sidebar('home-feature')) : ?>
    
    		<?php endif; ?>
    	</div><!-- end #content -->
    	<?php genesis_after_content(); ?>
    
    </div><!-- end #content-sidebar-wrap -->
    <?php genesis_after_content_sidebar_wrap(); ?>
    
    <?php get_footer(); ?>

    What I'm not clear on is this syntax to make my code for this template more "efficient." I want to include 4 widget areas on the home page template. And I've modified the above code with the following and it works. I just want to know a better way to write this, because four consecutive if statements seems inefficient.

    <?php genesis_before_content(); ?>
    	<div id="content" class="hfeed">
    
    		<?php if (!dynamic_sidebar('home-feature')) : ?>
    		<?php endif; ?>
    		<?php if (!dynamic_sidebar('home-left-feature')) : ?>
    		<?php endif; ?>
    		<?php if (!dynamic_sidebar('home-right-feature')) : ?>
    		<?php endif; ?>
    		<?php if (!dynamic_sidebar('home-bottom-feature')) : ?>
    		<?php endif; ?>
    	</div><!-- end #content -->
    	<?php genesis_after_content(); ?>
  2. Alex Vasquez
    Member
    Posted 2 years ago #

    So one of my buddies helped me out here and gave me some tips. Since this is genesis the structural parts I was calling weren't needed and could be replaced w/ genesis();

    Here's what I ended up with:

    <?php
    /**
     * Template Name: Alt Home Page
     * Description: A Three Column Layout for the Home Page
     */
    
    // Force full width layout
    add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_sidebar_content_sidebar' );
    
    // Build our custom loop
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_loop', 'special_loop' );
    function special_loop() {
    ?>
    
    	 	<div class="featured-article"><?php if (!dynamic_sidebar('home-feature')) : ?>
    		<?php endif; ?></div>
    		<div class="featured-left"><?php if (!dynamic_sidebar('home-left-feature')) : ?>
    		<?php endif; ?></div>
    		<div class="featured-right"><?php if (!dynamic_sidebar('home-right-feature')) : ?>
    		<?php endif; ?></div>
    		<div class="featured-bottom"><?php if (!dynamic_sidebar('home-bottom-feature')) : ?>
    		<?php endif; ?></div>
    
    <?php
    }
    genesis();

Topic Closed

This topic has been closed to new replies.

About this Topic