• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Right way to program/code ?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Right way to program/code ?

This topic is: not resolved

Tagged: code, function, programming, template

  • This topic has 2 replies, 2 voices, and was last updated 7 years, 10 months ago by ottie.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • May 3, 2015 at 4:04 am #149939
    ottie
    Member

    I'm quite new at using the genesis framework and I have a Flash actionscript 3 background.
    I tend to break code in seperate functions in order to build a page: is this the right way to go? I ask this question because most of the custom pages I've seen don't tend to do this.
    I'd like to improve this if I can: any suggestions?
    My example (I'm using the Advanced Custom Fields plugin):

    
    <?php
    remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_loop', 'single_house_loop' );
    
    function single_house_loop() { 
    	if(have_posts()) : while(have_posts()) : the_post();		
    		make_header();  //* Header of the article : place title and categories		
    		echo '<article>';
    			echo '<div class="two-thirds first">';	//* Left picture grid	
    				echo get_field('grid');
    			echo '</div>';
    			echo '<div class="one-third">'; //* Right Table and description
    				fill_table(); // build table
    				description();  // get the description
    			echo '</div>';
    		echo '</article>';
    	endwhile; 
    	endif;	
    }
    
    function make_header() {	
    		echo '<h1>';
    		echo the_title();
    		echo '</h1>';
    		echo get_the_term_list( $post->ID, 'locatie', '<p>Locatie  : ', ', ' );
    		echo get_the_term_list( $post->ID, 'type', ' - Type  : ', ', ', '</p>' );		
    }
    
    function fill_table() {
    	?>
    		<div class="datatabel">
    			<table class="table table-responsive ">
    			        <tbody> 
    			         <tr class="active plaatsrij"><td>Plaats:</td> <td>Kortrijk</td></tr>
    			         <tr><td class="td_prijstitel">Prijs:</td> <td class="td_prijs">€ <?php echo get_field('prijs'); ?></td></tr>
    			         <tr class="active"><td>Totale oppervlakte:</td> <td><?php echo get_field('totale_oppervlakte'); ?> m²</td></tr>
    			         <tr><td>Bewoonbare oppervlakte:</td> <td><?php echo get_field('bewoonbare_oppervlakte'); ?> m²</td></tr>
    			       </tbody>
    			</table>
    		</div>
    	<?php
    }
    
    function description() {
    	?> 
    	<div class="single_tekst">
            <p><?php echo get_field('omschrijving'); ?>
            </p>        
            </div>
    	<?php
    }	
    
    genesis();
    
    http://(localhost)
    May 3, 2015 at 10:36 am #150020
    Brad Dalton
    Participant

    Happy to contribute my suggestions which are:

    Never use the plugins functions like get_field as they are plugin dependant meaning they won't work when you remove the plugin.

    Use the native WordPress function to return the values of the custom fields with the specified key from the specified post.

    Example:

    $grid = get_post_meta( get_the_ID(), 'grid', true );
    if( ! empty( $grid ) ) {
      echo $grid;
    } 
    

    1. What this code does is create a variable named $grid
    2, The $grid variable is equal to the value for your custom field named grid
    3, The code uses empty to check if the value field for your custom field has a value
    4. And then prints the value in your template


    Tutorials for StudioPress Themes.

    May 5, 2015 at 11:45 am #150244
    ottie
    Member

    Thank you Brad for the hint.
    I guess I won't remove the plugin as my customer's page was built on it.
    I can understand what you mean - if I would swap it against another custom fields plugin (like Types/Views) or handcode fields, the code would still work as it is just a custom field filled with data.
    The only thing that really worried me was the amount of echoes - I tied to concatenate, but ended up with a different page 😉
    The way it is built now shows me structure and it even looks like html all the way.
    Would the amount of echoes influence the speed?
    Or should I just leave it like this?
    Thanks.

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble