Community Forums › Forums › Archived Forums › General Discussion › Right way to program/code ?
Tagged: code, function, programming, template
- This topic has 2 replies, 2 voices, and was last updated 9 years, 8 months ago by ottie.
-
AuthorPosts
-
May 3, 2015 at 4:04 am #149939ottieMember
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):
http://(localhost)<?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();
May 3, 2015 at 10:36 am #150020Brad DaltonParticipantHappy 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
May 5, 2015 at 11:45 am #150244ottieMemberThank 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. -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.