• Resolved sacconi

    (@sacconi)


    I tryed putting a conditional in the following function:

    <?php
    
    $posttags = wi-fi;
    
    $post=post;
    
    if (isset($posttags)) {
    
    $wi_fi_esc = '<div class="wi_fi_esc">'. get_the_author_meta( 'wi_fi_esc', $post->post_author ) .'</div>';
    
    }
    
    // Pass all this info to post content
    $content = $content . $titolo_prezzo_include . $locazione  .  $commissione . $consumi . $pulizia_inc . $biancheria_inc . $ac_inc .  $titolo_prezzo_non_include . $pulizia_esc . $tassa_soggiorno . $supplemento_animali . $supplemento_culla . $importo_cauzione . $servizio_spiaggia . $suppl_soggiorni_brevi . $biancheria_esc . $asciugamani_esc . $wi_fi_esc . $ac_esc .  $saldo . '<footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti . $titolo_orari . $orari .'</footer>';
    }
    return $content;
    }

    The purpose is to print in a post the variable $wi_fi_esc but only IF in the post target is set the tag (WP taxonomy) whose name is “wi-fi”

    I still havent checked it for real

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

Viewing 10 replies - 16 through 25 (of 25 total)
  • Thread Starter sacconi

    (@sacconi)

    If I havent set tag “wi-fi”, there is no sense in printing “wi fi 20 euro/week” through $wi_fi_esc

    Where are you retrieving the value of the “wi-fi” tag from? Is it a constant? If it is a constant, then you need to declare it like this: $posttags = ‘wi-fi’;

    <?php
    /*
    Plugin Name: MySite-plugin
    Description: Site specific code changes for example.com
    */
    /* Start Adding Functions Below this Line */
    function wpb_author_info_box( $content ) {
     
    global $post;
     
    // Detect if it is a single post with a post author
    if ( is_single() && isset( $post->post_author ) ) {
     
    // Get author's display name
    $display_name = get_the_author_meta( 'display_name', $post->post_author );
     
    // If display name is not available then use nickname as display name
    if ( empty( $display_name ) )
    $display_name = get_the_author_meta( 'nickname', $post->post_author );
     
    // Get author's biographical information or description
    $user_description = get_the_author_meta( 'user_description', $post->post_author );
    
    // Get agency period of rentals
    $periodo_affitti = get_the_author_meta( 'periodo_affitti', $post->post_author );
    
    $titolo_affitti = '<h4 class="titolo_affitti">Periodo affitti</h4>';
    
    // Get check-in e check-out
    $orari = get_the_author_meta( 'orari', $post->post_author );
    
    $titolo_orari = '<h4 class="titolo_affitti">Ingresso e riconsegna</h4>';
    
    
    
    // Get prezzo non include
    $tassa_soggiorno = '<div class="tassa_soggiorno">'. get_the_author_meta( 'tassa_soggiorno', $post->post_author ) .'</div>';
    
    $supplemento_animali = '<div class="tassa_soggiorno">'. get_the_author_meta( 'supplemento_animali', $post->post_author ) .'</div>';
    
    $supplemento_culla = '<div class="tassa_soggiorno">'. get_the_author_meta( 'supplemento_culla', $post->post_author ) .'</div>';
    
    $importo_cauzione = '<div class="tassa_soggiorno">'. get_the_author_meta( 'importo_cauzione', $post->post_author ) .'</div>';
    
    $servizio_spiaggia = '<div class="tassa_soggiorno">'. get_the_author_meta( 'servizio_spiaggia', $post->post_author ) .'</div>';
    
    $suppl_soggiorni_brevi = '<div class="tassa_soggiorno">'. get_the_author_meta( 'suppl_soggiorni_brevi', $post->post_author ) .'</div>';
    
    $pulizia_esc = '<div class="tassa_soggiorno">'. get_the_author_meta( 'pulizia_esc', $post->post_author ) .'</div>';
    
    $posttags = 'wi-fi';
    
    $wi_fi_esc = ''; 
    
    if (isset($posttags)) { $wi_fi_esc = '<div class="wi_fi_esc">' . get_the_author_meta('wi_fi_esc', $post->post_author) . '</div>'; } 
    
    $biancheria_esc = '<div class="biancheria_esc">'. get_the_author_meta( 'biancheria_esc', $post->post_author ) .'</div>';
    
    $asciugamani_esc = '<div class="asciugamani_esc">'. get_the_author_meta( 'asciugamani_esc', $post->post_author ) .'</div>';
    
    
    
    
    $titolo_prezzo_non_include = '<h4 class="titolo_affitti">Il prezzo non include:</h4>';
    
    //prezzo include
    
    $locazione = '<div class="tassa_soggiorno">'. get_the_author_meta( 'locazione', $post->post_author ) .'</div>';
    
    $commissione = '<div class="tassa_soggiorno">'. get_the_author_meta( 'commissione', $post->post_author ) .'</div>';
    
    $consumi = '<div class="tassa_soggiorno">'. get_the_author_meta( 'consumi', $post->post_author ) .'</div>';
    
    $pulizia_inc = '<div class="tassa_soggiorno">'. get_the_author_meta( 'pulizia_inc', $post->post_author ) .'</div>';
    
    $biancheria_inc = '<div class="tassa_soggiorno">'. get_the_author_meta( 'biancheria_inc', $post->post_author ) .'</div>';
    
    $ac_inc = '<div class="tassa_soggiorno">'. get_the_author_meta( 'ac_inc', $post->post_author ) .'</div>';
    
    
    $titolo_prezzo_include = '<h4 class="titolo_affitti">Il prezzo include:</h4>';
    
    
    
    // Get saldo
    $saldo = '<div class="saldo">'. get_the_author_meta( 'saldo', $post->post_author ) .'</div>';
    
     
    // Get author's website URL
    $user_website = get_the_author_meta('url', $post->post_author);
     
    // Get link to the author archive page
    $user_posts = get_author_posts_url( get_the_author_meta( 'ID' , $post->post_author));
     
    if ( ! empty( $display_name ) )
     
    $author_details = '<h4 class="author_name">Condizioni di pagamento ID: ' . get_the_ID(). '</h4>';
     
    if ( ! empty( $user_description ) )
    
    // Author avatar and bio
     
    $author_details .= '<p class="author_details">' . get_avatar( get_the_author_meta('user_email') , 90 ) . nl2br( $user_description ). '</p>';
     
    $author_details .= '<p class="author_links"><a href="'. $user_posts .'">View all posts by ' . $display_name . '</a>';  
     
    // Check if author has a website in their profile
    if ( ! empty( $user_website ) ) {
     
    // Display author website link
    $author_details .= ' | <a href="' . $user_website .'" target="_blank" rel="nofollow">Website</a></p>';
     
    } else {
    // if there is no author website then just close the paragraph
    $author_details .= '</p>';
    }
    
     
    // Pass all this info to post content
    $content = $content . $titolo_prezzo_include . $locazione  .  $commissione . $consumi . $pulizia_inc . $biancheria_inc . $ac_inc .  $titolo_prezzo_non_include . $pulizia_esc . $tassa_soggiorno . $supplemento_animali . $supplemento_culla . $importo_cauzione . $servizio_spiaggia . $suppl_soggiorni_brevi . $biancheria_esc . $asciugamani_esc . $wi_fi_esc . $ac_esc .  $saldo . '<footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti . $titolo_orari . $orari .'</footer>';
    }
    return $content;
    }
     
    // Add our function to the post content filter
    add_action( 'the_content', 'wpb_author_info_box' );
     
    // Allow HTML in author bio section
    remove_filter('pre_user_description', 'wp_filter_kses');
     
    /* Stop Adding Functions Below this Line */
    ?>

    The variable $ac_esc is also not declared. It is used when composing $content, but does not exist beforehand.

    Wrong:

    $content = $content . $titolo_prezzo_include . $locazione . $commissione . $consumi . $pulizia_inc . $biancheria_inc . $ac_inc . $titolo_prezzo_non_include . $pulizia_esc . $tassa_soggiorno . $supplemento_animali . $supplemento_culla . $importo_cauzione . $servizio_spiaggia . $suppl_soggiorni_brevi . $biancheria_esc . $asciugamani_esc . $wi_fi_esc . $ac_esc . $saldo . '<footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti . $titolo_orari . $orari .'</footer>';

    Fixed:

    $content = $content . $titolo_prezzo_include . $locazione . $commissione . $consumi . $pulizia_inc . $biancheria_inc . $ac_inc . $titolo_prezzo_non_include . $pulizia_esc . $tassa_soggiorno . $supplemento_animali . $supplemento_culla . $importo_cauzione . $servizio_spiaggia . $suppl_soggiorni_brevi . $biancheria_esc . $asciugamani_esc . $wi_fi_esc . $saldo . '<footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti . $titolo_orari . $orari .'</footer>';

    Tip 1: with a programme that supports PHP syntax highlighting, you can see something like this right away. E.g. with PHPStorm.

    Tip 2: This is about PHP basics. I would recommend to have a closer look at them: https://www.php.net/manual/en/tutorial.php

    Thread Starter sacconi

    (@sacconi)

    The function doesnt break the site, this is a half victory, but it is ineffective.

    To tell the truth I wanted to do the contrary, I should use the contrary of “isset”. I meant to do the following: when I dont set a specific tag, the variable value is not printed. Maybe each tag has a meta name that is not the one I write in the tags box?

    The opposite of isset is when isset returns false. As a condition, it would look like this:

    if( !isset($var) ) {
     /* do something if $var is not set */.
    }

    See: https://www.php.net/manual/de/function.isset.php

    Thread Starter sacconi

    (@sacconi)

    So like this:

    $var=$posttags;
    
    $posttags = 'wi-fi';
    
    $wi_fi_esc = '';
    
    if( !isset($var) ) {
    
     $wi_fi_esc = '<div class="wi_fi_esc">' . get_the_author_meta('wi_fi_esc', $post->post_author) . '</div>'; } 

    Sorry, I don’t really see the point in the code right now.

    Thread Starter sacconi

    (@sacconi)

    I wrote above what I was trying to do

    Thread Starter sacconi

    (@sacconi)

    Maybe this is more clear?

    "$posttags = animali permessi";
    
    if (isset($posttags)) { $supplemento_animali = '<div class="supplemento_animali">' . get_the_author_meta('supplemento_animali', $post->post_author) . '</div>'; } 

    The purpose is displaying the extra cost for pets only if the tag “animali permessi” (pets allowed) is checked.

    Thread Starter sacconi

    (@sacconi)

    Finally I got the solution:

    if (has_tag( 'animali permessi', $post ) ) {
        $supplemento_animali = '<div class="suppl_animali">'. get_the_author_meta( 'supplemento_animali', $post->post_author ) .'</div>';
    }
Viewing 10 replies - 16 through 25 (of 25 total)
  • The topic ‘Conditional in a function’ is closed to new replies.