-
AuthorPosts
-
December 4, 2019 at 7:36 pm #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
December 9, 2019 at 5:17 am #1163967Hey 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,
IsmaelDecember 9, 2019 at 4:49 pm #1164074Ismael,
The ternary operator worked. They are actually easier than if / else once you get your head around them. :)
Thanks
Tim
December 9, 2019 at 4:54 pm #1164076 -
AuthorPosts
- The topic ‘If / Else statement to ternary condition for input into postslider.php’ is closed to new replies.