Weezy
The scripts read all the tags in a blog post
it then displays a set number of random posts linked to those tags
Note: the __xtblog_block_id variable is required as multiple blog functions are used
First the twig files
Rename xtblog.twig to xtblog_1.twig
Create a new xtblog.twig file with this code
it uses one of the blogs translation variables to decide which twig file to use
{%if data.translations.t_no_entries_found!='related'%}
{%include 'xtblog_1.twig'%}
{%else%}
{%include 'xtblog_2.twig'%}
{%endif%}
Copy code
Insert this line into xtblog_1.twig in the view=='entry' area to provide the tag data to the xtscript
{% elseif view == 'entry' %} <div class="xt_blog"> {% block entry_tags %} {% if not data.entry.in_comments_thread %} {% if data.hide_tags == false and data.post.tags %} insert this line here
<!--|{% for tag in data.post.tags %}{{ tag.name }}|{% endfor %}-->
Copy code
before the
<div class="xt_tags">....... Create a new xtblog_2.twig file with this code it creates a data string of the post urls using custom blog functions in the xtscript another blog variable is used to filter the current post out of the results
{% if view == 'entries_list' %}
{% block entries_list %}
{% for entry in data.entries %}
{% set blog_entry=entry.url | split ('/__xtblog_entry/')%}
{% set blog_entry=blog_entry[1]|split ('?')%}
{% if data.translations.t_no_search_results_found not in blog_entry[0]%}
{% spaceless %}[{{ blog_entry[0] }}|{{ entry.title }}]{% endspaceless %}
{% endif %}
{% endfor %}
{% endblock %}
{% endif %}
Copy code
Then we have the xtscript that will output the blog and related post links it will insert the links just under the 'back to posts' button The main blog function in the script is set up with dummy tags so that you can still use the blog in the building tool to manage the blog Just don't edit the lines just above and below the blog function using the building tool only use the code editor or the script may be broken
<!--parser:xtscript-->#<!--
# Main blog function
# </xt:code>
var $blog= <xt:blog tzone="0" format="Y-m-d" display="5" show_time="0" entries_per_page="0" comments_per_page="0" comments_sort="-1" entries_sort="-1" display_limit="0" display_symbol="" version="2" t_back_to_posts="" t_comments="" t_comments_empty="" t_post_a_comment="" t_name="" t_comment="" t_post="" t_tags="" t_search="" t_created="" t_showing_tagged_posts="" t_no_entries_found="" t_no_search_results_found="" t_show_all_comments="" t_see_parent_thread="" t_show_all_replies="" t_show_replies="" t_reply="" t_posting_as="" t_comments_login_required="" t_login="" t_signup="" t_confirm_email="" t_report_spam="" include_tags_in_post_list="1" />
# <xt:code>
# Check if it is a post
get __xtblog_entry
if not $__xtblog_entry
print $blog
goto @end
endif
# Fix missing__xtblog _entry value
var $blog=call str_replace $subject=$blog; $search=name="__xtblog_entry" value="";$replace=name="__xtblog_entry" value="$__xtblog_entry"
# Split the blogs HTML
var $split=call strpos $haystack=$blog;$needle=div class="xt_blog_comments"
var $blog1=call substr $val=$blog;$start=0;$length=($split-1)
var $blog2=call substr $val=$blog;$start=($split-1)
# Output the first half
print $blog1
# Find the tags
var $start_tags=call strpos $haystack=$blog;$needle=!--|;offset=0
var $end_tags=call strpos $haystack=$blog;$needle=|--;offset=0
# Exit if no tags
if not $start_tags or not $end_tags
goto @finish
endif
# Get the tags list
var $tags=call substr $val=$blog;$start=($start_tags+4);$length=($end_tags-$start_tags-4)
# Main loop
@loop
# End loop if no more tags
var $tag_end=call strpos $haystack=$tags|;$needle=|;$offset=$tag_start
if not $tag_end
goto @show_related_list
endif
# Get the tag
var $tag=call substr $val=$tags;$start=$tag_start;$length=($tag_end-$tag_start)
# Get the list of related posts for this tag using the xtblog_2.twig and a custom blog function
var $tagged_post_list =<xt:blog entries_per_page="0" entries_sort="-1" version="2" display="1" tag="$tag" t_no_entries_found="related" t_no_search_results_found="$__xtblog_entry"/>
# Process this tags list
var $current_entry_start = 0
@loop2
# Move to next tag if no more entries
var $current_entry_end=call strpos $haystack=$tagged_post_list;$needle=];$offset=$current_entry_start
if not $current_entry_end
goto @next_tag
endif
# Get the current entry
var $current_entry=call substr $val=$tagged_post_list;$start=$current_entry_start;$length=($current_entry_end-$current_entry_start+1)
# Check for duplicate result
var $duplicate=call strpos $haystack=[$related_post_list;$needle=$current_entry
if $duplicate
goto @skip_duplicate
endif
# if entry is unique then randomly add the entry to either the top or bottom of the related posts list
var $rand=call mt_rand $min=0; $max=1
if $rand
# Bottom
var $related_post_list=$related_post_list$current_entry
else
# Top
var $related_post_list=$current_entry$related_post_list
endif
@skip_duplicate
# Move to the next entry
var $current_entry_start=($current_entry_end+1)
goto @loop2
@next_tag
# Move to next tag
var $tag_start=($tag_end+1)
goto @loop
# Show the list of related posts
@show_related_list
var $path=<xt:url type="path">
var $count=0
var $start_entry=0
# Open the container
print <div class="">
# Process the link data
@loop3
# Exit if end of list
var $end_entry=call strpos $haystack=$related_post_list**;$needle=];$offset=$start_entry
if not $end_entry
goto @close
endif
# Process the entry
var $entry=call substr $val=$related_post_list;$start=$start_entry;$length=($end_entry-$start_entry+1)
var $end_value=call strpos $haystack=$entry;$needle=|;$offset=1
var $url=call substr $val=$entry;$start=1;$length=($end_value-1)
var $start_value=($end_value+1)
var $end_value=call strpos $haystack=$entry;$needle=];$offset=$start_value
var $name=call substr $val=$entry;$start=$start_value;$length=($end_value-$start_value)
# Create the related post link
print <a href="$path/__xtblog_entry/$url?__xtblog_block_id=1#xt_blog"><span class="xt_button">$name</span></a>
# Move to the next entry
var $start_entry=($end_entry+1)
var $count=($count+1)
# This sets the number of links to show
if $count < 10
goto @loop3
endif
# Close the container
@close
print </div>
@finish
# Show the second part of the blog
print $blog2
# Exit script
@end
<!--/parser:xtscript-->
Copy code