Community Forums › Forums › Archived Forums › Design Tips and Tricks › Add Custom Fields To Genesis Child Theme
- This topic has 4 replies, 2 voices, and was last updated 6 years, 4 months ago by ashleymichelle.
-
AuthorPosts
-
April 23, 2018 at 2:38 pm #219204ashleymichelleMember
I am having a lot of difficulty adding custom fields to my theme; I would like to use the built-in Custom Fields feature on the WordPress Posts page, and simply insert the value per post.
This is what I was previously using within my Single Post loop before switching to Genesis Framework:
<?php $affiliates = get_post_meta ($post->ID, 'affiliates', true); ?> <?php if (strlen ($affiliates) > 0): ?> <?php echo '<a href="/./affiliates" target="_blank">affiliate link</a>'; ?> <?php endif; ?> <?php $prpost = get_post_meta ($post->ID, 'prpost', true); ?> <?php if (strlen ($prpost) > 0): ?> <?php echo '<a class="prpost" href="/./pr" target="_blank">PR</a>'; ?> <?php endif; ?> <?php $prsample = get_post_meta ($post->ID, 'prsample', true); ?> <?php if (strlen ($prsample) > 0): ?> <?php echo '<a class="prsample" href="/./pr" target="_blank">Gift</a>'; ?> <?php endif; ?> <?php if(get_post_meta($post->ID, "buyit", true)): { ?> <?php $field_name="buyit"; $field_value = get_post_meta($post->ID, $field_name, true); ?> <a class="buyit" href="<?php echo $field_value; ?>" target="_blank">Get It Here</a> <?php ; } endif ?>
Each of these displayed a different value and were positioned in various places on my theme. I just need to figure out how to accomplish this general concept with the Genesis Framework, as this tutorial isn't quite doing what I need it to.
I would like the custom fields displayed on both the Post, and also beneath the expert on the Main Index, below each Post Image, but above the excerpt and "Read More" link.
Thanks so much!
http://ohmybonbon.comApril 24, 2018 at 12:25 am #219208Brad DaltonParticipantUse the code you linked to as its tested and works.
The hook determines which position the custom field value outputs on the front end.
April 24, 2018 at 6:27 pm #219244ashleymichelleMemberHi Brad,
I did figure out how to implement it; Genesis newbie here.However, my next question is, how do I consolidate all of this into one hook, so that I can apply a single class? I'd like to wrap all of this within a divider, ".awrap"...
add_action( 'genesis_before_entry_content', 'affiliate_link', 12 ); function affiliate_link() { if ( is_single() ) $value = get_post_meta( get_the_ID(), 'affiliates', true ); if ( ! empty( $value ) ) { echo '<span class="awrap"><a href="/./affiliates" target="_blank">affiliate link</a></span>'; } } add_action( 'genesis_before_entry_content', 'pr_sample', 12 ); function pr_sample() { if ( is_single() ) $value = get_post_meta( get_the_ID(), 'prsample', true ); if ( ! empty( $value ) ) { echo '<span class="awrap"><a class="prsample" href="/./pr" target="_blank">gift</a></span>'; } } add_action( 'genesis_before_entry_content', 'pr_post', 12 ); function pr_post() { if ( is_single() ) $value = get_post_meta( get_the_ID(), 'prpost', true ); if ( ! empty( $value ) ) { echo '<span class="awrap"><a class="prpost" href="/./pr" target="_blank">PR</a></span>'; } } add_action( 'genesis_before_entry_content', 'buy_it', 12 ); function buy_it() { if ( is_single() ) $value = get_post_meta( get_the_ID(), 'buyit', true ); if ( ! empty( $value ) ) { echo '<span class="awrap"><a class="buyit" href="'. $value .'" target="_blank">Get It Here</a></span>'; } }
Thank you!
April 25, 2018 at 4:20 am #219254Brad DaltonParticipantWrap the code in 1 hook and conditional.
Use a test site to do this if you want to learn custom coding.
I use flywheel local.
April 25, 2018 at 8:00 pm #219285ashleymichelleMemberCould you reference or show me an example of how to do this? I'm a visual designer rather than developer, so my knowledge base is highly learn-as-I-go, and my googling skills apparently aren't showing me what I need to find.
Many thanks!
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.