WordPress.org

Ready to get started?Download WordPress

Forums

Calling a custom field inside of an echo? (19 posts)

  1. ultramalcolm
    Member
    Posted 2 years ago #

    Let me start at the beginning. Forgive me, as I am not well versed in PHP, and tend to take more of a stumbling, smash-and-grab approach to the language.

    Here's what I'm trying to do:

    I have a conditional statement that displays buttons to download a file using an echo, provided a post from a certain category is being shown. What I need to do is have the href to one of the buttons be taken from a custom field (in this case, "direct_download_link") from that post. Here's the code I'm trying to use to accomplish this:

    <?php 
    
    if ( in_category( 'Episodes' )) {
    
    echo '<a href="<?php echo get_post_meta($post->ID, 'direct_download_link', true); ?>"><img src="/images/download-button_01.png"></a><img src="/images/download-button_02.png"><br><br>';
    
    } 
    
    ?>

    Of course, this isn't working, because the php inside of the echo isn't getting parsed, it's just printing to the browser.

    The page in question is here: http://anchor-consulting.com/dev/bionic/?p=559

    Can anyone help me, or offer a better suggestion for accomplishing what I am trying to do?

    Thank you very much

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    http://php.net/manual/en/language.operators.string.php

    in your example:

    echo '<a href="' . get_post_meta($post->ID, 'direct_download_link', true) . '"><img src="/images/download-button_01.png"></a><img src="/images/download-button_02.png"><br><br>';
  3. ultramalcolm
    Member
    Posted 2 years ago #

    Thanks for the reply. I've modified the code, but it is still not returning the contents of my custom field...it is just showing the URL of the page. Any other ideas?

  4. Andrew Bartel
    Member
    Posted 2 years ago #

    <?php
    
    $var = get_post_meta($post->ID, 'direct_download_link', true);
    
    echo '<a href=$var><img src=whatever></a><img src=somedownloadthing>';
    
    ?>
  5. ultramalcolm
    Member
    Posted 2 years ago #

    Here's my updated code:

    <?php 
    
    if ( in_category( 'Episodes' )) {
    
    $var = get_post_meta($post->ID, 'direct_download_link', true);
    
    echo '<a href=$var><img src="http://www.anchor-consulting.com/dev/bionic/wp-content/themes/photoria/images/download-button_01.png"></a><img src=<img src="http://www.anchor-consulting.com/dev/bionic/wp-content/themes/photoria/images/download-button_02.png">';
    
    } 
    
    ?>

    It looks like the custom field still isn't getting picked up by the browser.

  6. Andrew Bartel
    Member
    Posted 2 years ago #

    Yea sorry didn't think it through. Edited post should work.

    Don't try to get fancy. Just do it in a couple lines. It makes your code more readable too.

  7. Andrew Bartel
    Member
    Posted 2 years ago #

    Re: your original question though:

    See post below

  8. Andrew Bartel
    Member
    Posted 2 years ago #

    Man wordpress edits hate me.

    <?php 
    
    if ( in_category( 'Episodes' )) {
    
    ?>
    
    <a href="<?php echo get_post_meta($post->ID, 'direct_download_link', true); ?>"><img src="/images/download-button_01.png"></a><img src="/images/download-button_02.png"><br><br>
    
    <?php
    
    } 
    
    ?>
  9. ultramalcolm
    Member
    Posted 2 years ago #

    Okay, here's what I am using now:

    <?php 
    
    if ( in_category( 'Episodes' )) {
    
    ?>
    
    <a href="<?php echo get_post_meta($post->ID, 'direct_download_link', true); ?>"><img src="/dev/bionic/wp-content/themes/photoria/images/download-button_01.png"></a><img src="/dev/bionic/wp-content/themes/photoria/images/download-button_02.png"><br><br>
    
    <?php
    
    } 
    
    ?>

    This still isn't pulling the value from my custom field, though...the button is just showing the URL of the page.

  10. Andrew Bartel
    Member
    Posted 2 years ago #

    Link?

  11. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    where is that code located?

    if it is not directly within the loop, you might neet to add <?php global $post; ?> before the code;

    possibly check if the $post variable is set by adding:

    <?php echo $post->ID; ?>

    before your lines.

  12. Andrew Bartel
    Member
    Posted 2 years ago #

    Yea I was thinking it was time to start dying out variables too. The examples given by myself and alch should work.

  13. ultramalcolm
    Member
    Posted 2 years ago #

    http://anchor-consulting.com/dev/bionic/?p=559

    Adding the global post code made no change, and yes, the $post variable IS being sent (I just checked).

  14. Andrew Bartel
    Member
    Posted 2 years ago #

    Looks like it's working to me

  15. ultramalcolm
    Member
    Posted 2 years ago #

    It does? The value of "direct_download_link" should be "/episodes/test.htm" but that's not what I'm seeing.

  16. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    code seems to be ok;
    you say the post ID is ok;

    questions: is the custom field for the download link filled in?

    have you tried with more than one of the posts in category 5 = episodes?

    another control output - what gets output if you add <?php the_meta(); ?> into your code?

  17. ultramalcolm
    Member
    Posted 2 years ago #

    Yes, the custom field for the download link is filled in. I just tried adding it to a second page in the "Episodes" category: http://anchor-consulting.com/dev/bionic/?p=569

    The results were the same...the button is showing the address of the page itself, not the value of the custom field.

    When I add a plain <?php the_meta(); ?> , I do get the list of meta data. (I left it in so you can see.)

    Any other ideas?

  18. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    thanks for leaving the control output in your site.

    your custom field output is showing this key:

    direct_downoad_link

    see (or rather not see) the missing letter l in download ?

    seems to be case of a simple typing mistake ...

  19. ultramalcolm
    Member
    Posted 2 years ago #

    AUUUUUUUUUUUUUUUGH!

    Thank you SO SO much for being a second set of eyes on this. I appreciate it very much.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.