Configuring the Link Preview Template
You’ll find the Link Preview Template option under the Internal Link Juicer’s “Settings” section. Let’s explain how to get to everything you’re looking for.
-
Click on the “Setting” Page
Once you’re in the Internal Link Juicer Settings page, click on the “Links” tab.
-
Enable the Link Preview feature
You can enable and disable this feature using the toggle switch.
-
Customize the link preview template
To add additional functionality to your links, you can customize the link preview template.
Options for customizing the link preview template
1. When you want to change the preview structure
The structure can be changed by modifying the template ( Internal Links > Settings > Links )
The template is written in Liquid, a popular open-source template language. It supports a variety of operators, functions, and even includes control flows. You can read more about it here. By default it displays featured image and excerpt of post / term, if you want to change this behavior to show only excerpt, you change the template to this.
<div class="ilj-link-preview">
{% if excerpt %}
<p class="ilj-link-preview-excerpt">{{ excerpt | truncatewords: 55 }}</p>
{% endif %}
</div>
The link preview will now display only excerpts.
2. Add additional attributes
By default, you will have featuredImage and excerpt available for use inside the liquid template, to add additional attributes you can make use of the following filters.
ilj_post_link_attributes
for postilj_term_link_attributes
for term
For instance if you have a acf field which you need to display on link preview instead of excerpt, you can use the following code snippet.
add_filter('ilj_post_link_attributes', function ($link_attrs, $post_id, $excerpt) {
$link_attrs['data-acf-content'] = get_field('my_field', $post_id);
return $link_attrs;
}, 10, 3);
This adds the content of your ACF field to the data attribute data-acf-content
. Now, this field value will be accessible in the template as acfContent
. Data attributes are directly passed to the template, and you should be mindful of the case conversion that occurs in the data attribute. You can now modify the template to display this acf content
<div class="ilj-link-preview">
{% if acfContent %}
<p>{{ acfContent | truncatewords: 55 }}</p>
{% endif %}
</div>
3. Disabling the link preview for a single post id.
The preview will only be disabled when the link has the data-ilj-link-preview
attribute, you can remove the attribute if you want to disable it for post id 1 by adding:
add_filter('ilj_post_link_attributes', function ($link_attrs, $post_id, $excerpt) {
if (1 === $post_id && array_key_exists('data-ilj-link-preview', $link_attrs)) {
unset($link_attrs['data-ilj-link-preview']);
}
return $link_attrs;
}, PHP_INT_MAX, 3);
4. Disabling the link preview for single term id.
If you want to display the preview only for term with term id 1 add:
add_filter('ilj_term_link_attributes', function ($link_attrs, $term_id, $excerpt) {
if (1 === $term_id && array_key_exists('data-ilj-link-preview', $link_attrs)) {
unset($link_attrs['data-ilj-link-preview']);
}
return $link_attrs;
}, PHP_INT_MAX, 3);
5. Enable the feature only for page
post type.
If you want to enable the feature to appear only for a page post type, add:
add_filter('ilj_post_link_attributes', function ($link_attrs, $post_id, $excerpt) {
if ('page' === get_post_type($post_id) && array_key_exists('data-ilj-link-preview', $link_attrs)) {
unset($link_attrs['data-ilj-link-preview']);
}
return $link_attrs;
}, PHP_INT_MAX, 3);
6. Enable the feature only for tags
If you want to enable the feature only for tags, where only links pointing to tags taxonomy will have the link preview, add:
add_filter('ilj_term_link_attributes', function ($link_attrs, $term_id, $excerpt) {
if ('post_tag' === get_term($term_id)->taxonomy && array_key_exists('data-ilj-link-preview', $link_attrs)) {
unset($link_attrs['data-ilj-link-preview']);
}
return $link_attrs;
}, PHP_INT_MAX, 3);