Community Forums › Forums › Archived Forums › General Discussion › snippets
- This topic has 8 replies, 4 voices, and was last updated 10 years, 1 month ago by alexadark.
-
AuthorPosts
-
November 29, 2014 at 6:33 am #133199alexadarkMember
Hi,
i am often wondering why genesis snippets are more complicated than writing in the classic wordpress way:
ex:
to display a fet img on single posts, i found that:add_action( 'genesis_entry_header', 'single_post_featured_image', 5 ); function single_post_featured_image() { if ( ! is_singular( 'post' ) ) return; $img = genesis_get_image( array( 'format' => 'html', 'size' => genesis_get_option( 'image_size' ), 'attr' => array( 'class' => 'post-image' ) ) ); printf( '<a href="%s" title="%s">%s</a>', get_permalink(), the_title_attribute( 'echo=0' ), $img ); }
Which works, but which i barely not understand (well i am not a php ninja)
when i can write this without searching for a snippet as i perfectly understand what i am doing
add_action( 'genesis_entry_header', 'wst_featured_image_single_post', 9 ); function wst_featured_image_single_post() { if ( is_singular( 'post' ) ) { ?> <div class="featured-image"> <?php the_post_thumbnail( 'featured'); ?> </div> <?php } }
and here i can a markup around my featured img, in the other way, i have again to search another snippet, as i don't know how to do it.
also why
if ( ! is_singular( 'post' ) ) return;
instead of
if ( is_singular( 'post' ) ) { }
which again is simpler (well for me!)
same thing for displaying widget areas, why not use the wordpress classic way:
<div class"widget-area> <?php ( dynamic_sidebar( 'widget area name' ) ) ?> </div>
i find it easier than the genesis way, and the result is the same.
So is there a reason to write it in the "genesis way" ? i am doing something wrong ?
November 29, 2014 at 7:13 am #133201Brad DaltonParticipantNovember 29, 2014 at 8:41 am #133206alexadarkMemberthis is on your site 😉 http://wpsites.net/web-design/display-featured-image-before-or-after-entry-title-on-single-posts-pages/
the other code is my way of writingNovember 29, 2014 at 9:13 am #133207cdilsParticipantif ( ! is_singular( 'post' ) ) return;
if ( is_singular( 'post' ) ) { }
The first statement says, "if it's not a single post", exit the function, otherwise carry on. The second statement says the opposite, "if it IS a single post" then do something..." The first statement is preferred because it exits the function immediately without reading further. In the second funtion, if it doesn't match the first conditional, it'll execute through the remainder of the function to see if there's anything else it needs to do.
Regarding use of the curly braces... both statements are semantically, however in order to standardize usage, Nacin suggested in 2013 to always use braces. Not a big deal, just the direction that WordPress chose to standardize.
Onto the thumbnail...
You're correct that both ways accomplish basically the same thing, but what I like about the Genesis way is:
* The
genesis_get_image()
encapsulates both thehas_post_thumbnail
conditional check (which the other code example should have to prevent that html markup printing in the event of no thumbnail. You can get the same output with less code.Also:
* I (personally) prefer not to escape PHP (end curly bracket) and go back to HTML for such a small a small amount of HTML. The Genesis version is more readable. Of course, you could refactor the second code example to do that, so that's not a Genesis thing, just a coding preference
The Sidebar:
Using
genesis_widget_area()
nets you the advantage of including contextual HTML5 markup around the widget (assuming your site supports HTML5), so no need to manually write out those divs with hard-coded classes.Good questions. Hope that helps.
Carrie
Have you been helped in this forum? Pay it forward and answer someone else’s question. I bet you’ll know the answer to at least one question. 🙂
I host a weekly WordPress-focused podcast called Office Hours. I tweet @cdils.
November 29, 2014 at 9:30 am #133210alexadarkMemberThank you very much Carrie, that's help a lot 🙂
so i will use the genesis functions, but as i understand there can be coding preferences
i prefer escaping php, as it's more readable with the code coloring etc...
🙂November 29, 2014 at 12:41 pm #133222alexadarkMemberWell i'm trying to use genesis_get_image, but don't work...or don't know how to write it...
<div class="featured-image"> <?php genesis_get_image(array ('size' => 'featured-big')); ?> </div>
November 30, 2014 at 1:25 am #133249Sridhar KatakamParticipantA few links that might help:
http://designsbynickthegeek.com/tutorials/genesis-explained-image-functions
November 30, 2014 at 9:48 am #133259alexadarkMemberThanks Sridhar,
i have seen all that, and understood, but don't know why don't work with genesis_get_image , anyway i will keep the_post_thumbnail for this site; and will use it on the nextAnother question, when you make an archive page for a CPT, how do you link this page in the menu ?
a part putting an absolute link, which is not very good when you transfer sites...November 30, 2014 at 10:54 am #133265alexadarkMemberJust found this plugin: https://wordpress.org/plugins/post-type-archive-links/
🙂 -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.