• I tryed to add a meta data value to my post page representing the distance from the sea, whose key metadata name is “function_number”, but it doesnt work. I can see the post ID correctly but no distance from the sea (a number). Here is the code:

    <?php echo "ID: ",get_the_ID(),get_post_meta( $post_id, "function_number" ), colormag_entry_meta(); ?>

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • The code you provided seems to have a small issue. Instead of using $post_id as the parameter for get_post_meta(), you should use get_the_ID() to retrieve the current post ID within the loop. Here’s the corrected code:

    <?php echo "ID: ", get_the_ID(), get_post_meta( get_the_ID(), "function_number", true ), colormag_entry_meta(); ?>

    In the updated code, get_the_ID() is used to retrieve the current post ID within the loop and is passed as the first parameter to get_post_meta(). Additionally, I added the third parameter, true, to ensure that the meta value is returned as a single value instead of an array.

    Ensure the meta key “function_number” is correctly set and associated with the desired post. Also, double-check if the colormag_entry_meta() function is valid and specific to your theme.

    If the issue persists, you may need to examine how the meta value is being set or if any conflicts with other parts of your code or plugins could affect the retrieval of the meta value.

    Thank you.

    Thread Starter sacconi

    (@sacconi)

    you are a genious, it’s working! I added also an icon for the sea and “m.” after the function_number, so:

    <?php echo "ID: ",get_the_ID(), "<img src='/wp-content/uploads/2023/06/icona_mare.jpg'/>", get_post_meta( get_the_ID(), "function_number",true )," m.",colormag_entry_meta(); ?>
    

    Maybe do you have a solution for not showing the icon when there is not the meta value for the distance for the sea?, I suppose a conditional “if…”, but where and how? thank you (or I put it in a separate topic)

    Moderator bcworkz

    (@bcworkz)

    Something like this:

    $meta = get_post_meta( get_the_ID(), "function_number",true );
    if ( ! empty( $meta )) {
        echo "ID: ",get_the_ID(), "<img src='/wp-content/uploads/2023/06/icona_mare.jpg'/>", $meta," m.",colormag_entry_meta();
    }

    Yes, the above recommendation will determine if there’s a value assigned for the distance from the sea i.e. function_number field. If the distance isn’t set, the above solution will simply not output anything. However, if you prefer only the icon not to be displayed when there’s no distance, you can adjust the code like this:

    
    $meta = get_post_meta( get_the_ID(), "function_number", true );
    $imgTag = ! empty( $meta ) ? "<img src='/wp-content/uploads/2023/06/icona_mare.jpg'/>" : "";
    echo "ID: ", get_the_ID(), $imgTag, $meta," m.",colormag_entry_meta();

    Here ! empty( $meta ) check verifies if there’s a value for “function_number”. If it’s not empty, $imgTag gets assigned the HTML code for an image. If it’s empty, $imgTag remains an empty string.

    Thank you.

    Thread Starter sacconi

    (@sacconi)

    In the meanwhile I added new icons and meta data but only the icon of the distance of the sea should be invisible if the “function_number” value is not set

      <?php echo "ID: ",get_the_ID(),  "<img src='/wp-content/uploads/2023/06/icona_mare.jpg'/>", get_post_meta( get_the_ID(), "function_number",true )," m. ","<img src='/wp-content/uploads/2023/06/ospiti3.jpg'/>",get_post_meta( get_the_ID(), "tax_input[numero_ospiti]",true ), "<img src='/wp-content/uploads/2023/06/camere.jpg'/>",get_post_meta( get_the_ID(), "tax_input[numero_camere]",true ),colormag_entry_meta(); ?>

    I tryed to use

    $meta = get_post_meta( get_the_ID(), "function_number",true );
    if ( ! empty( $meta )) {
        echo "ID: ",get_the_ID(), "<img src='/wp-content/uploads/2023/06/icona_mare.jpg'/>", $meta," m.","<img src='/wp-content/uploads/2023/06/ospiti3.jpg'/>",get_post_meta( get_the_ID(), "tax_input[numero_ospiti]",true ), "<img src='/wp-content/uploads/2023/06/camere.jpg'/>",get_post_meta( get_the_ID(), "tax_input[numero_camere]",true ) ,colormag_entry_meta();
    }
    

    according the advice of bcworkz but in case the “function_number” value is not set all the other icons and meta values disappear, so I cant mix the 2 different behaviours ??

    • This reply was modified 1 year, 9 months ago by sacconi.
    Moderator bcworkz

    (@bcworkz)

    Sorry, I misunderstood your intent. Exactly what part of the output should be hidden? I get the distance value and " m.", what else?

    Thread Starter sacconi

    (@sacconi)

    Only the icon <img src=’/wp-content/uploads/2023/06/icona_mare.jpg’/> representing the sea should stay hidden if the value “function_number “doesnt exist (distance from the sea), thank you! Here an example of its use: https://sacconicase.com/case-vacanza/italia/toscana/marina-di-massa-appartamenti-vacanze/

    Moderator bcworkz

    (@bcworkz)

    Like this then:

    $meta = get_post_meta( get_the_ID(), "function_number",true );
    if ( ! empty( $meta )) {
      $src = wp_content_url('/uploads/2023/06/icona_mare.jpg');
      $distance = "<img src='$src'/> $meta m."
    } else {
      $distance = '';
    }
        echo "ID: ",get_the_ID(), $distance ,colormag_entry_meta();

    Introduced wp_content_url() because you’ll want to avoid relative paths in any WP URL.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘calling a meta data in a single.php file’ is closed to new replies.