skip to Main Content

I have added advanced custom fields to woocommerce category pages to help with SEO.

Is there a cleaner way to code this as a lot of excess probably don’t need

        <div class="full top-cat-seo">
                <?php
                $queriedObject=get_queried_object();
                echo get_field('product_category_top_section_seo','product_cat_'.$queriedObject->term_id);
            
                $link1 = get_field('link_url_1','product_cat_'.$queriedObject->term_id);
                $link1name = get_field('link_url_1_name','product_cat_'.$queriedObject->term_id);
                $link2 = get_field('link_url_2','product_cat_'.$queriedObject->term_id);
                $link2name = get_field('link_url_2_name','product_cat_'.$queriedObject->term_id);
                $link3 = get_field('link_url_3','product_cat_'.$queriedObject->term_id);
                $link3name = get_field('link_url_3_name','product_cat_'.$queriedObject->term_id);
                $link4 = get_field('link_url_4','product_cat_'.$queriedObject->term_id);
                $link4name = get_field('link_url_4_name','product_cat_'.$queriedObject->term_id);
            
                if( $link1 ): ?>
                    <a class="button" href="<?php echo esc_url( $link1 ); ?>"><?php echo esc_html( $link1name );?></a>
                <?php endif; ?>
                <?php if( $link2 ): ?>
                    <a class="button" href="<?php echo esc_url( $link2 ); ?>"><?php echo esc_html( $link2name );?></a>
                <?php endif; ?>
                <?php if( $link3 ): ?>
                    <a class="button" href="<?php echo esc_url( $link3 ); ?>"><?php echo esc_html( $link3name );?></a>
                <?php endif; ?>
                <?php if( $link4 ): ?>
                    <a class="button" href="<?php echo esc_url( $link4 ); ?>"><?php echo esc_html( $link4name );?></a>
                <?php endif; ?>
            
        </div>

there are 8 custom fields to create 4 buttons as per here

https://onepoundcrisps.com/cat/brand/kp/

2

Answers


  1. Using a loop, you build the name of the field from the loop key. So instead of hardcoding link_url_1, you can use 'link_url_' . $i where $i is the loop index.

    You can also fetch the link and only fetch the linkname if there is a value.

    This means you do 1 set of code and any changes will be reflected on all elements…

    $queriedObject = get_queried_object();
    echo get_field('product_category_top_section_seo', 'product_cat_' . $queriedObject->term_id);
    
    for ($i = 1; $i < 5; $i++) {
        $link = get_field('link_url_' . $i, 'product_cat_' . $queriedObject->term_id);
    
        if ($link) {
            $linkName = get_field('link_url_' . $i . '_name', 'product_cat_' . $queriedObject->term_id);
            echo '<a class="button" href="' . esc_url($link) . '">' . esc_html($linkName) . '</a>';
        }
    }
    

    I’ve converted the HTML to an echo, if you feel more comfortable with your current version, you should just need to change the field names (just remove the number).

    Login or Signup to reply.
  2. Instead of using a separate conditional statement for each link, you can use a loop to iterate over all the links and display them. Here’s how it can be done:

    <div class="full top-cat-seo">
      <?php
      $queriedObject = get_queried_object();
      echo get_field('product_category_top_section_seo', 'product_cat_' . $queriedObject->term_id);
      ?>
      <?php
      for ($i = 1; $i <= 4; $i++) {
        $link = get_field("link_url_{$i}", "product_cat_{$queriedObject->term_id}");
        $linkname = get_field("link_url_{$i}_name", "product_cat_{$queriedObject->term_id}");
        if ($link) {
          ?>
          <a class="button" href="<?php echo esc_url($link); ?>"><?php echo esc_html($linkname); ?></a>
          <?php
        }
      }
      ?>
    </div>
    

    Instead of using the get_field() function repeatedly, you can use the get_fields() function to retrieve all the fields for a specific product category in a single call, and then access the individual fields as an array. Here’s how it can be done:

    <div class="full top-cat-seo">
      <?php
      $queriedObject = get_queried_object();
      $fields = get_fields("product_cat_{$queriedObject->term_id}");
      echo $fields['product_category_top_section_seo'];
      ?>
      <?php
      for ($i = 1; $i <= 4; $i++) {
        $link = $fields["link_url_{$i}"];
        $linkname = $fields["link_url_{$i}_name"];
        if ($link) {
          ?>
          <a class="button" href="<?php echo esc_url($link); ?>"><?php echo esc_html($linkname); ?></a>
          <?php
        }
      }
      ?>
    </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search