Skip to main content
edited tags; edited title
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Optimize PHP Listing links for better performancecategories

Source Link
Delto
  • 123
  • 3

Optimize PHP for better performance

I've developed some PHP that grabs all of a business listing's categories and then displays just the children categories. This is built on Wordpress. I'm still in the learning process and would appreciate some assistance here. Any optimizations you can suggest I make to my code would be really helpful. Please note, if it's really technical, there's a good chance it'll go over my head at this point, so examples are appreciated.

<?php 

$permalink = get_permalink( $id );

$seo = get_the_title()." : ";

$Category_links = ' Found in the ';

$term_list_category = wp_get_post_terms(get_the_ID(), 'listings_categories', array("fields" => "ids"));


//THIS CODE REMOVES PARENTS FROM BEING DISPLAYED IN THE LISTING CATEGORIES
foreach ($term_list_category as $k=>$term) {
    $children = get_term_children( $term, 'listings_categories');
if ($children)
    unset($term_list_category[$k]);
 }

$i = 0;

$count = count($term_list_category);

if ( $count > 0 ){

    foreach ( $term_list_category as $term_category ) {

        $thisCat = get_term_by( 'id', $term_category, 'listings_categories');

        
        $url = '<a id="'.$term_category.'" slug="'.$thisCat->{'slug'}.'" class="listing-links-cat" href="#" title="'.$thisCat->{'name'}.'" >'.$thisCat->{'name'}.'</a>';

        $i ++;

        $seo .= " " . $thisCat->{'name'} . "";

        $Category_links .= " " . $url . "";

        if($count-1 == $i){
            $Category_links .= " and ";  $seo .= ", ";
        }elseif($count > 1 && $count !== $i){
            $Category_links .= ", ";  $seo .= ", ";
        }

    }
    $Category_links .= " Categories";
?>

<? echo $Category_links;  ?>