简体   繁体   中英

Shopify Liquid If Statement for Shop Name

I have my brand name or shop name added to all my URL's via the following statement which is found in the theme.liquid file. I want to exclude all pages that are blogs or articles. This would mean no shop name on those URL's, which is this pipe and code.

| {{ shop.name }}

Original Code

  {%- capture seo_title -%}
    {%- if template == 'search' and search.performed == true -%}
      {{ 'general.search.heading' | t: count: search.results_count }}: {{ 'general.search.results_with_count' | t: terms: search.terms, count: search.results_count }}
    {%- else -%}
      {{ page_title }}
    {%- endif -%}
    {%- if current_tags -%}
      {%- assign meta_tags = current_tags | join: ', ' -%} – {{ 'general.meta.tags' | t: tags: meta_tags -}}
    {%- endif -%}
    {%- if current_page != 1 -%}
      – {{ 'general.meta.page' | t: page: current_page }}
    {%- endif -%}
    {%- assign escaped_page_title = page_title | escape -%}
    {%- unless escaped_page_title contains shop.name -%}
      | {{ shop.name }}
    {%- endunless -%}
  {%- endcapture -%}
    <title>{{ seo_title | strip }}</title>

I have been trying to place another if statement around the following but I have had no luck yet.

    {%- assign escaped_page_title = page_title | escape -%}
    {%- unless escaped_page_title contains shop.name -%}
      &#124; {{ shop.name }}
    {%- endunless -%}

This is the code that I have tried, is there a better way to do this as I can't quite get it to work.

Attempted Code

  {%- capture seo_title -%}
    {%- if template == 'search' and search.performed == true -%}
      {{ 'general.search.heading' | t: count: search.results_count }}: {{ 'general.search.results_with_count' | t: terms: search.terms, count: search.results_count }}
    {%- else -%}
      {{ page_title }}
    {%- endif -%}
    {%- if current_tags -%}
      {%- assign meta_tags = current_tags | join: ', ' -%} &ndash; {{ 'general.meta.tags' | t: tags: meta_tags -}}
    {%- endif -%}
    {%- if current_page != 1 -%}
      &ndash; {{ 'general.meta.page' | t: page: current_page }}
    {%- endif -%}
    {%- if (template == "blog" or template == "article") and current_tags contains '_NOINDEX' -%}
    {%- assign escaped_page_title = page_title | escape -%}
    {%- unless escaped_page_title contains shop.name -%}
    {%- endunless -%}
    {%- else -%}
    {%- assign escaped_page_title = page_title | escape -%}
    {%- unless escaped_page_title contains shop.name -%}
      &#124; {{ shop.name }}
    {%- endunless -%}
   {%- endif -%}
  {%- endcapture -%}
    <title>{{ seo_title | strip }}</title>

You can just use the following:

 {%- unless escaped_page_title contains shop.name or template == 'blog' or template == 'article' -%}
   &#124; {{ shop.name }}
 {%- endunless -%}

You were close, but you cannot use parentheses to group Liquid condition operators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM