I am using wordpress's advanced custom fields, which uses the syntax get_field('custom_field_name');
to pull custom fields from the database. in this case, I have a series of checkboxes, and I am using PHP to see if each one has been checked, and if so, it will spit out a string. Here is my PHP:
<?php
if(in_array('brand-id', get_field('type_of_work') )): echo "<a href='../../work/#brand-id'>Brand ID</a> |"; endif; echo " ";
if(in_array('print', get_field('type_of_work') )): echo "<a href='../../work/#print'>Print</a> |"; endif; echo " ";
if(in_array('books', get_field('type_of_work') )): echo "<a href='../../work/#books'>Books</a> |"; endif; echo " ";
if(in_array('web', get_field('type_of_work') )): echo "<a href='../../work/#web'>Web</a> |"; endif; echo " ";
if(in_array('packaging', get_field('type_of_work') )): echo "<a href='../../work/#packaging'>Packaging</a> |"; endif; echo " ";
if(in_array('exhibit', get_field('type_of_work') )): echo "<a href='../../work/#exhibit'>Exhibit</a> |"; endif;
?>
What this is doing is saying, if this checkbox is checked, then spit out a link for that checkbox. As you can see, I am echoing a "|" after each link. The problem is that the last link is followed by a "|". I would like to remove the last "|" programmatically with PHP. Perhaps I can convert the whole thing to a string and then use substr($string, 0, -1);
? Anny ideas how I would go about this?
$links = array();
if(in_array('brand-id', get_field('type_of_work') )) $links[] = "<a href='../../work/#brand-id'>Brand ID</a>";
....
echo implode(" | ",$links);
Use a string:
$checks = "";
if(in_array('brand-id', get_field('type_of_work') )): $checks .= "<a href='../../work/#brand-id'>Brand ID</a> | "; endif;
if(in_array('brand-id', get_field('type_of_work') )): $checks .= "<a href='../../work/#print'>Print</a> |"; endif;
Then: substr() on $checks: echo substr($checks, 0, -2);
trim($string, '|')
to remove trailing pipe |
( manual )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.