Monday, October 18, 2021

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

You can use and anchor text instead of the regular long URL in the website link on the single 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 Directory Builder section like below. Please make sure you name the key "website-anchor" like the following image -

Directorist | Anchor Text For Website Field | 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/single/fields/website.php" in your currently activated theme folder with the following code -

Directorist | Anchor Text For Website Field | Custom Code




  1. <?php
  2. /**
  3.  * @author  wpWax
  4.  * @since   6.7
  5.  * @version 7.0.5.2
  6.  */
  7.  
  8. if ( ! defined( 'ABSPATH' ) ) exit;
  9. $website_anchor = get_post_meta( $data['listing_id'], '_website-anchor', true );
  10. $website_anchor = $website_anchor && !empty( $website_anchor ) ? $website_anchor : 'Visit Website';
  11. ?>
  12.  
  13. <div class="directorist-single-info directorist-single-info-web">
  14.  
  15.     <div class="directorist-single-info__label">
  16.         <span class="directorist-single-info__label-icon"><?php directorist_icon( $icon );?></span>
  17.         <span class="directorist-single-info__label--text"><?php echo esc_html( $data['label'] ); ?></span>
  18.     </div>
  19.    
  20.     <div class="directorist-single-info__value">
  21.         <a target="_blank" href="<?php echo esc_url( $value ); ?>"<?php echo !empty( $data['use_nofollow'] ) ? 'rel="nofollow"' : ''; ?>><?php echo esc_html( $website_anchor ); ?></a>
  22.     </div>
  23.  
  24. </div>



4. Input: 


Directorist | Anchor Text For Website Field | Custom Code


5. Output:


Directorist | Anchor Text For Website Field | Custom Code


No comments:

Post a Comment