• 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: https://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

Viewing 15 replies - 1 through 15 (of 18 total)
  • https://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>';
    Thread Starter ultramalcolm

    (@ultramalcolm)

    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?

    <?php
    
    $var = get_post_meta($post->ID, 'direct_download_link', true);
    
    echo '<a href=$var><img src=whatever></a><img src=somedownloadthing>';
    
    ?>
    Thread Starter ultramalcolm

    (@ultramalcolm)

    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="https://www.anchor-consulting.com/dev/bionic/wp-content/themes/photoria/images/download-button_01.png"></a><img src=<img src="https://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.

    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.

    Re: your original question though:

    See post below

    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
    
    } 
    
    ?>
    Thread Starter ultramalcolm

    (@ultramalcolm)

    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.

    Link?

    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.

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

    Thread Starter ultramalcolm

    (@ultramalcolm)

    https://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).

    Looks like it’s working to me

    Thread Starter ultramalcolm

    (@ultramalcolm)

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

    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?

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Calling a custom field inside of an echo?’ is closed to new replies.