Monday, October 18, 2021

Directorist | Anchor Text For Website Field On Archive Page | Custom Code

 You can use and anchor text instead of the regular long URL in the website link on the archive page/all listing page by using some custom code.

Please follow the procedure to achieve this functionality on your website with the Directorist.


1. Enabling the key field:

Please copy and paste the following code at the end of the "functions.php" file of the currently activated theme folder. 


Directorist | Enable Field Key




  1. add_filter('directorist_custom_field_meta_key_field_args', function ($args) {
  2.     $args['type'] = 'text';
  3.     return $args;
  4. });


2. Create a custom text field below the website field:

Please create a new custom text field below the website field on the add listing form layout at the Directory Builder section like below. Please make sure you name the key "website-anchor" like the following image -


Directorist | Anchor Text For Website Field On Archive Page | Custom Code


3. Template Override:

Please read the template override documentation before you start this process. This will help you to understand how to override the templates properly.

Template Override Documentation

Please create a file "directorist/archive/fields/website.php" in your currently activated theme folder with the following code -


Directorist | Anchor Text For Website Field On Archive Page | Custom Code





  1. <?php
  2. /**
  3.  * @author  wpWax
  4.  * @since   6.6
  5.  * @version 6.7
  6.  */
  7.  
  8. if ( ! defined( 'ABSPATH' ) ) exit;
  9. $listing_id = get_the_ID();
  10. $website_anchor = get_post_meta( $listing_id, '_website-anchor', true );
  11. $website_anchor = $website_anchor && !empty( $website_anchor ) ? $website_anchor : 'Visit Website';
  12. ?>
  13.  
  14. <div class="directorist-listing-card-website"><?php directorist_icon( $icon ); ?><?php $listings->print_label( $label ); ?><a target="_blank" href="<?php echo esc_url( $value ); ?>"><?php echo esc_html( $website_anchor ); ?></a></div>



4. Input: 


Directorist | Anchor Text For Website Field On Archive Page | Custom Code


5. Output:


Directorist | Anchor Text For Website Field On Archive Page | Custom Code







No comments:

Post a Comment