• northstatehillbilly

    (@northstatehillbilly)


    I’ve really wrestled with this. 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 and results in two other functions not working properly.

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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="'.$link.'" target="_blank">'.$question.'</a>';
     	echo '</div>';
     }

    Looks like a mistake on the second ECHO line.

    Thread Starter northstatehillbilly

    (@northstatehillbilly)

    Thanks, Bob!

    It doesn’t work yet, but I completely forgot to use periods before and after $link and $question, so thank you!

    I’ll continue to fiddle and will post back here if I figure it out.

    Thread Starter northstatehillbilly

    (@northstatehillbilly)

    I can’t figure out the link portion just yet, but I discovered two errors.

    Dummy errors and easy to overlook.

    1) Instead of a double forward slash on a comment that precedes this function, I used one with an asterisk, which commented out the function and everything that follows. :o)

    2) I named the key Question with a capital Q. But in the function I didn’t capitalize and that caused an error.

    I learned from Brad Dalton in the StudioPress forums that you can use HTML inside a custom field box. So for now that’s how I’m outputting the key as a link.

    Also, you don’t need the_meta(); … it results in a bulleted list and pulls in everything. Not necessary. And there are a few other issues. I’ll post the finished code when it’s done.

    Thread Starter northstatehillbilly

    (@northstatehillbilly)

    I thought I’d go ahead and post what’s working. Do not capitalize anything when creating custom fields, lest you forget to also capitalize in your code. Also, to make this a link you’ll need to insert HTML in the value field.

    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="moose-question">'. $question .'</div>';
    }}}
    Thread Starter northstatehillbilly

    (@northstatehillbilly)

    And now, using Bob’s link from above, the question is displaying 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>';
    }}}
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How do I correct my function for a custom field that displays a link?’ is closed to new replies.