• 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

How to correct the link code in my custom field function

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 › Design Tips and Tricks › How to correct the link code in my custom field function

This topic is: not resolved

Tagged: custom functions

  • This topic has 9 replies, 2 voices, and was last updated 11 years ago by Carla the Moose.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • October 9, 2015 at 11:18 am #167731
    Carla the Moose
    Member

    I've really wrestled with this and have sought help in different forums. I could just add divs directly to my blog posts. But I'd love to figure this out.

    For each blog post, I'm going to ask a question, which will display as a link. I've managed to get the question to print as a link, but it's not grabbing the link value. Instead, it's using the current blog page as the URL. It's also not opening a new tab.

    add_action('genesis_entry_content', 'the_question');
     function the_question($post_id) {
     	the_meta();
     	$question = get_post_meta('question');
     	$link = get_post_meta('link');
     	echo '<div class="moose-question">';
     	echo <a href="<?php echo $link; ?>" target="_blank"><?php $question; ?></a>;
     	echo '</div>';
     }

    I'm wondering a couple of things.

    First, am I registering custom fields properly by using the_meta();? Without it, this function resulted in an error message. I'm seeing different ways people are activating custom fields. This is just one of them. I'm not sure if this is the best option.

    Second, the function breaks my design but doesn't result in a white page of death. I originally was using different code for the link to display. Then I came across something in the WordPress forums and add php to the link. As is, this results in the design break.

    I'm trying to learn how to code links with embedded php. But I'm also wondering if I'm not calling the value correctly.

    I've done quite a bit of research on this and would appreciate your suggestions. Thank you!

    October 9, 2015 at 12:23 pm #167741
    Brad Dalton
    Participant

    Try this video http://wpsites.net/web-design/how-to-create-a-custom-field/

    the_meta tag displays the entire meta-data for each post and styles it in an unordered list which is something you probably don't want. Use get_post_meta instead which the video explains in more detail.


    Tutorials for StudioPress Themes.

    October 9, 2015 at 12:46 pm #167745
    Carla the Moose
    Member

    You have an accent!!

    I've seen you in the forums for years and never knew you're from a different country.

    Anyway, I watched the video yesterday and wasn't able to resolve this problem.

    :o)

    October 9, 2015 at 12:53 pm #167747
    Brad Dalton
    Participant

    For each question, you can add the HTML to the value field of each custom field meta box if you want a custom link.

    But your code is wrong which is why i linked to a tutorial which contains the working code.

    Swap out the hook and custom field key in the code.


    Tutorials for StudioPress Themes.

    October 9, 2015 at 1:19 pm #167752
    Carla the Moose
    Member

    I'm not sure what you mean by swapping out the hook and key.

    Here's what I did per your tutorial. I removed the link and will deal with that another time. This isn't working.

    add_action('genesis_entry_content', 'the_question');
     function the_question() {
    	if ( !is_page() ) {
    
     	$question = get_post_meta( get_the_ID(), 'question', true );
     	if( ! empty( $question ) ) {
    
     	echo '<div class="the-question">' . $question . '</div>';
    }}} 
    October 9, 2015 at 1:30 pm #167753
    Brad Dalton
    Participant

    Works when i test it.

    You did create a new custom field using question as the field name and then adding some content to the value box?


    Tutorials for StudioPress Themes.

    October 9, 2015 at 1:47 pm #167757
    Carla the Moose
    Member

    Okay, so this is what I call a double dummy.

    1) All code, beginning with this function, was grayed out and I assumed it was because of an error within my function. Kind of yes, but also not really.

    This is what preceded the code: /* THE QUESTION FUNCTION

    It should have been a double forward slash. I commented out half of my website. :o)

    2) The second dummy was capitalization. I love clean and tidy grammar, so when I created the key, I entered it like this: Question

    But in the function I wrote it all in lower case letters. Function not happy.

    Now I'm going to go back and fiddle with the link and see if I can get it working. I didn't realize I can put html inside a custom field box. But it will be less of a hassle if I can just create another key with the link as the value. I'll update the other forums where I posed this question.

    Thanks for your help, once again, Brad!

    October 9, 2015 at 2:51 pm #167761
    Carla the Moose
    Member

    And now we have success. The dang thing is working. Two fields, one for the question and the other for the link. It outputs the question as a link. Awesomeness personified! I've spent two days on this darn thing.

    Here's what works:

    add_action('genesis_entry_content', 'moose_question');
     function moose_question() {
    	if ( !is_page() ) {
    
     	$question = get_post_meta( get_the_ID(), 'question', true );
     	$link = get_post_meta( get_the_ID(), 'link', true );
    
     	if( ! empty( $question ) ) {
     	echo '<div class="moose-question">';
     	echo '<a href="'.$link.'" target="_blank">'.$question.'</a>';
     	echo '</div>';
    }}}

    Thanks Brad (and Bob from the WordPress forums)!!!!

    October 9, 2015 at 3:21 pm #167763
    Brad Dalton
    Participant

    Well done.

    I assume you're using this method so people can simply add the text and link without needing to create HTML?


    Tutorials for StudioPress Themes.

    October 9, 2015 at 8:26 pm #167777
    Carla the Moose
    Member

    At the bottom of every blog post, a question is asked to stimulate discussion. It displays just below a category prompt. The two centered lines of text are set apart from the blog with some styling.

    I could have used simple HTML within each post, but I'd much prefer using fields. It saves me the hassle of HTML typos or copying and pasting the basic format from other posts and then splicing in the URL for the link.

    And it needs to appear below the category prompt. I couldn't figure out how to do that without a function.

    Also, I'm hoping to do something more with this link someday, and it requires custom fields.

  • Author
    Posts
Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘Design Tips and Tricks’ 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

© 2026 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