Conditional in a function
-
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]
-
Hi @sacconi
The variable $wi_fi_esc needs to be declared before use to avoid errors referencing an undefined variable. In the given code, it is necessary to add the declaration of the variable $wi_fi_esc before the if condition.
$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>'; }
-
This reply was modified 1 year, 6 months ago by
Marcio Zebedeu.
I think it’s a part of the code I havent written, but there is already:
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 ) ) { $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; }
It makes a sense?
In your current code, I still feel that the variable $wi_fi_esc is undefined since you didn’t declare it before the if statement. Therefore, inside the HTML in the variable $content, it remains undefined.
Now I understand. You’re saying that you defined the variable outside of the function.
I think there are many things to fix in my code. Because the are many $ variables but only one has a conditional. The original full code, that is working, is:
<?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>'; $wi_fi_esc = '<div class="wi_fi_esc">'. get_the_author_meta( 'wi_fi_esc', $post->post_author ) .'</div>'; $ac_esc = '<div class="tassa_soggiorno">'. get_the_author_meta( 'ac_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 */ ?>
I want a different behaviour just for one variable:
$wi_fi_esc
, this variable is displayed only if in the posts the tag “wi-fi” is setFor this reason I created
$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>'; }
But I dont know how to concatenate it with all the other code
Once you have placed the variable $wi_fi_esc within a condition, it will be necessary to define the variable outside of that condition as an empty variable: $wi_fi_esc=”
What is the meaning of “$posttags = wi-fi”?
Ok, but I can write in the same functions 2 times the same variable? One time under conditional, the other time with no conditional?
Do it : $ wi_fi_esc=”;
if (isset ($posttags)) {
$ wi_fi_esc=’‘. get_the_author_meta (‘wi_fi_esc’, $post->post_author) . ”;
}“$posttags = wi-fi” should mean: if the target post has a tag called “wi-fi”. I mean the default wp non hierarchical taxonomy. Or: If I have selected this tag
ok, and where I can put the div class?
The original function was:
$wi_fi_esc = '<div class="wi_fi_esc">'. get_the_author_meta( 'wi_fi_esc', $post->post_author ) .'</div>';
look:
<?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>'; $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 */ ?>
-
This reply was modified 1 year, 6 months ago by
Marcio Zebedeu.
-
This reply was modified 1 year, 6 months ago by
Marcio Zebedeu.
I have made an update to the previous code. Take a look again.
I see, but the specific tag name “wi-fi” is not defined. ” Only when this specific tag is set, the variable $wi_fi_esc has to output nothing
Only when this specific tag (wi-fi) is NOT set
-
This reply was modified 1 year, 6 months ago by
- The topic ‘Conditional in a function’ is closed to new replies.