Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1162702

    Hi,

    I have a custom field that I am inserting into various elements on site. It’s a field that adds a read time to all blogs.

    I’m using an If / Else statement to say…

    If a value is set for read time echo the value if not, do nothing.

     $k3read = get_post_meta($the_id, 'k3read', true);
      if (empty($k3read)){  
      echo "";
      }
     else{
     echo "<span class='k3-cptc'>{$k3read} MINUTE READ</span>";
     echo "<span class='text-sep text-sep-comment'>/</span>";
     }

    That has worked a treat in loop-index.php and loop-search.php.
    However in postslider.php the original code is slightly different and what ever I do breaks something.
    How do I change the if / else statement to fit into the code below?

    if ( $commentCount != "0" || comments_open($the_id) && $entry->post_type != 'portfolio')
    							
    						{
    							$link_add = $commentCount === "0" ? "#respond" : "#comments";
    							$text_add = $commentCount === "1" ? __('Comment', 'avia_framework' ) : __('Comments', 'avia_framework' );
    							$linkedin = get_post_meta($the_id, 'linkurl', true);
    							$k3read = get_post_meta($the_id, 'k3read', true);
    							$meta .= "<div class='slide-meta-comments'><a href='{$link}{$link_add}'>{$commentCount} {$text_add}</a></div><div class='slide-meta-del'>/</div><span class='k3-cpt'>{$linkedin}</span><span class='k3-cptb'>{$k3read} minutes read</span>";
    						}
                            $markup = avia_markup_helper(array('context' => 'entry_time','echo'=>false, 'id'=>$the_id, 'custom_markup'=>$custom_markup));
    						$meta .= "<time class='slide-meta-time updated' $markup>" .get_the_time(get_option('date_format'), $the_id)."</time>";
    						$meta .= "</div>";

    Thanks

    TJ

    #1163967

    Hey TJ,

    Thank you for the inquiry.

    Put the markup in a separate line so that you can define its own logic. If you want to use a ternary operator, try this:

    $k3read = get_post_meta($the_id, 'k3read', true);
    $meta .= $k3read ? "span class='k3-cptc'>{$k3read} MINUTE READ</span><span class='text-sep text-sep-comment'>/</span>" : '';
    

    Best regards,
    Ismael

    #1164074

    Ismael,

    The ternary operator worked. They are actually easier than if / else once you get your head around them. :)

    Thanks

    Tim

    #1164076

    Hey,

    Glad Ismael could help! Let us know if you have any other questions or issues :)

    Regards,
    Yigit

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘If / Else statement to ternary condition for input into postslider.php’ is closed to new replies.