Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Show tags from two blogs with taxonomylist plugin

Started by Pedro M 2 years ago · 1 replies · 257 views
2 years ago

I'm messing around setting up popular tags in the sidebar (with the taxonomylist plugin) and I'm not getting what I want. The thing is that with one blog type page, the code works fine, but with more than one blog type page, it no longer works well.

At https://pmdesign.dev I have two blog-type pages, one for tutorials and another for code. Both are blog types and I would like to show the entire taxonomy of the site in the sidebar, but the tag links, when they are on a page other than the blog page, do not point well to it.

The code in sidebar would be the following:

TWIG
{#Set blog settings#}
{% set themeBlog = theme_var('blog_route')|defined('/blog') %}
{% set blog = page.find(header_var('blog_url')|defined(themeBlog)) %}
{% set feed_url = blog.url == '/' or blog.url == base_url_relative ? (base_url_relative~'/'~blog.slug) : blog.url %}
{% set new_base_url = blog.url == '/' ? '' : blog.url %}

{# Taxonomy list (if taxonomylist plugin is enabled) #}
{% if showTaxonomyList %}
{% set taxlist = children_only is defined ? taxonomylist.getChildPagesTags() : taxonomylist.get() %}
{# If there is a taxonomy, load taxonomylist.html.twig #}
{% if taxlist %}
{% include 'partials/sidebar/taxonomylist.html.twig'
with {'base_url':new_base_url, 'taxonomy':'tag', 'block_title': 'FACTION.SIDEBAR.TAXONOMYLIST'|t} %}
{% endif %}
{% endif %}

Obviously, if I'm not on a blog page, it defaults to the route defined in the theme (blog_route), and if it's not set, the url is the root of the site, which is not what I want to achieve.

For example, if I have the tag tag1 and tag2 on blog1, and the tag tag3 and tag4 on blog2, when I show the popular tags in the sidebar, I would like the link to point to http://gravsite/blog1/tag:tag1 when I click on tag1. and when you click on tag3, make the link point to http://gravsite/blog2/tag:tag3.

I don't know how to mix the tags from both blogs in the same popular tags widget.

2 years ago

I have fixed the sidebar code a little, because I realized that it never passed the children_only variable, this is how it looks for now:

sidebar.html.twig

TWIG
{# List of taxonomies (if the taxonomylist plugin is enabled) #}
{% if showTaxonomyList %}
{% includes 'partials/sidebar/taxonomylist.html.twig'
with {'base_url':new_base_url, 'taxonomy':'tag', 'block_title': 'FACTION.SIDEBAR.TAXONOMYLIST'|t, Children_only: true} %}
{% will end if %}

And in taxonomylist.html.twig:

TWIG
{% set tax list = children_only?? TRUE ? taxonomylist.getChildPagesTags() : taxonomylist.get() %}
{# If there is a taxonomy, load taxonomylist.html.twig #}
{% if taxlist and page.template == 'blog' %}
<section>
      <header>
          <h2>{{ 'FACTION.SIDEBAR.TAXONOMYLIST'|t }}</h2>
      </header>
      <ul class="actions">
          {% for taxes,value in tax list[taxonomy] %}
              {% set label_class = uri.param(taxonomy) == tax? 'secondary' : 'primary' %}
              <li>
                  <a class="small secondary button {{ label_class }}" href="{{ base_url }}/{{ taxonomy }}{{ config.system.param_sep }}{{ tax }}">{{ tax }}< /a>
              </li>
          {% end of %}
      </ul>
</section>
{% will end if %}

With this I ensure that the popular tags are seen on the blog pages and that only those that refer to the pages of the blog to which they belong are seen.

It is still pending to be able to mix collections from different blogs and show all the tags of all the blogs and have each tag link to its corresponding blog page.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 125 19 hours ago
General · by pamtbaau, 24 hours ago
1 89 23 hours ago
General · by Andy Miller, 1 day ago
0 73 1 day ago
General · by Marcel, 12 months ago
6 382 5 days ago
General · by Duc , 6 days ago
3 69 6 days ago