0

Can anyone help me adapt the below which creates a set of <LI> elements with custom taxonomy categories created in a custom post type I have. What I want to achieve is to only list the 'project-type' results which match a defined value.

So, I only want the <LI> to be listed if the 'project-type' value includes 'sector1' in the its name.

So, the 'project-type' names could be "sector1-subsector1", "sector1-subsector2", "sector2-subsector1" - from these only 2 would be listed as an <LI> - e.g. where the start of the name includes "sector1".

<?php
$terms = get_terms('project-type');
foreach ($terms as  $term) { ?>
<li data-filter=".<?php  echo $term->slug; ?>"><?php echo $term->name; ?></li>
<?php  } ?>

1 Answer 1

0

This worked for me:

<?php
$terms = get_terms('project-type');
foreach ($terms as  $term) { 

if(strpos($term->slug, 'sector1-') !== false){
?>
<li data-filter=".<?php  echo $term->slug; ?>"><?php echo $term->name; ?>
<?php
}
?>

</li>
<?php  } ?>
1
  • 2
    Note that unless if you're still using WordPress prior to v4.5.0, you should use get_terms( array( 'taxonomy' => 'project-type' ) ), i.e. pass an array of arguments to the function. See the get_terms() documentation for more details. And your question was actually a PHP question, so you should've instead searched or asked on Stack Overflow, e.g. "how to check in PHP if a string contains a certain text, e.g. if sector1-subsector1 contains sector1".
    – Sally CJ
    Jun 24 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.