• Resolved Brian

    (@bkurious)


    Hopefully someone far more experienced working with theme files can help me.

    I need to echo a custom field value inside of an already existing echo statement in my theme files. It seems straight-forward enough, but no amount of Googling has helped.

    Here’s the code I’m working with:

    <?php $product_id = get_post_meta($post->ID, 'product_id', true);if ($product_id) { ?>
    <?php echo get_button_code_for_product(<?php echo $product_id; ?>); ?>
    <?php} ?>

    The original line I started with was

    <?php echo get_button_code_for_product(1); ?>

    I need to be able to echo each post’s product_id into the parentheses of the above code.

    Any help? Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can’t echo inside an echo and guessing on what you’re trying to achieve above, you don’t have to. Also, you have php tags inside existing php tags.

    This is how I would write it:

    <?php
       $product_id = get_post_meta($post->ID, 'product_id', true);
    
       if ( $product_id ) {
          echo get_button_code_for_product( $product_id );
       }
    ?>

    Thread Starter Brian

    (@bkurious)

    Works like a charm. Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Echo Custom Field Key Inside PHP Echo Statement’ is closed to new replies.