Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to correct the link code in my custom field function
Tagged: custom functions
- This topic has 9 replies, 2 voices, and was last updated 8 years, 5 months ago by Carla the Moose.
-
AuthorPosts
-
October 9, 2015 at 11:18 am #167731Carla the MooseMember
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 #167741Brad DaltonParticipantTry 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.
October 9, 2015 at 12:46 pm #167745Carla the MooseMemberYou 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 #167747Brad DaltonParticipantFor 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.
October 9, 2015 at 1:19 pm #167752Carla the MooseMemberI'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 #167753Brad DaltonParticipantWorks 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?
October 9, 2015 at 1:47 pm #167757Carla the MooseMemberOkay, 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 #167761Carla the MooseMemberAnd 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 #167763Brad DaltonParticipantWell done.
I assume you're using this method so people can simply add the text and link without needing to create HTML?
October 9, 2015 at 8:26 pm #167777Carla the MooseMemberAt 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.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.