Viewing 3 replies - 1 through 3 (of 3 total)
  • Yes, IDs should be unique for each element on a page, so you should set them twice: one for each field (considering that the function will be invoked only once per page).

    Here’s a streamlined version:

    function prezzo_minimo_meta_box_render($post) {
    // Retrieve meta values
    $number = get_post_meta($post->ID, "function_prezzo_primo", true);
    $number2 = get_post_meta($post->ID, "function_prezzo_primo_dom", true);

    // Initialize the output variable
    $output = '';

    // Generate the output for the number field
    if (!empty($number)) {
    $value = round($number / 7, 0);
    $output = '<div><input type="number" id="input_prezzo_primo" value="' . esc_attr($value) . '" placeholder="prezzo/notte in euro" /></div>';
    } elseif (!empty($number2)) {
    $value = round($number2 / 7, 0);
    $output = '<div><input type="number" id="input_prezzo_primo_dom" value="' . esc_attr($value) . '" placeholder="prezzo/notte in euro" /></div>';
    }

    // Output the result
    if (!empty($output)) {
    echo $output;
    }
    }
    Thread Starter sacconi

    (@sacconi)

    OK, but I see they are 2 different ID, how can I put insert both of them to replace this anchor?

    $url = admin_url( '/post.php?post=' . $post->ID . '&action=edit#prezzo_settimana' );

    the anchor to be replaced is #prezzo_settimana. it should be #input_prezzo_primo OR #input_prezzo_primo_dom .

    @sacconi You can do the same thing from the previous function: create a condition to define the ID based on the post meta value and apply it to your $url variable.

    // Initialize the anchor with the default value.
    $anchor = 'prezzo_settimana';

    // Determine the final anchor value based on your condition.
    if ( ! empty( $number ) ) {
    $anchor = 'input_prezzo_primo';
    } elseif ( ! empty( $number2 ) ) {
    $anchor = 'input_prezzo_primo_dom';
    }

    // Append the anchor to the URL.
    $url = admin_url( '/post.php?post=' . intval( $post->ID ) . '&action=edit' ) . '#' . esc_attr( $anchor );

    // Output the URL safely.
    echo esc_url( $url );

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.