Custom Fields
Re-Envisioned
Support MB Views can’t get the category slug to use in a condition

  • Creator
    Topic
  • #20365
    Resolved sofdesigne
    Participant

    Hi, i am trying to get the name of the current post category, and compare it to do some conditional tasks

    but the condition don’t work, i think because the category is not just a slug, it’s a link !

    here is my code

    
    {% set cat = mb.get_the_term_list( post.ID, 'category') %}
    {{ cat }}
    
    {% if cat == archives %}
        <p>test</p>
    {% endif %}
    

    i try to echo test when the category slug is ‘archives’

    i also tried with

    {% set termList = mb.get_the_terms( post.ID, 'category' ) %}
    {{ termList[0].slug }}

    but it didn’t work either, maybe because i get a couple empty lines when it showed up in the chrome inspector !

    i just need to get the category slug to compare it with simple text for some conditions

    please help

    thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
    Replies
  • #20368
    Long Nguyen
    Moderator

    Hi,

    This code

    {% set termList = mb.get_the_terms( post.ID, 'category' ) %}
    {{ termList[0].slug }}

    shows the first category slug of the current post as well, please see my screen record https://www.loom.com/share/b520a8d8fe63414081f790e584c5dac2. Could you please clear the cache and check the code again?

    #20369
    sofdesigne
    Participant

    Thanks for your replay,

    the thing is that i can’t get the condition to work, the category slug is showing normally like in your example, but the condition is not working, i think because of the way it’s displayed, please take a look at the capture, it’s from chromes inspector, it has many empty space, this is why i can’t match it with my value condition

    thanks

    https://ibb.co/dPnpLcZ

    #20371
    Long Nguyen
    Moderator

    If you want to compare a variable with a text, you have to wrap it in the single/double quotes.

    {% set termList = mb.get_the_terms( post.ID, 'category' ) %}
    {% set cat = termList[0].slug %}
    {% if cat == 'archives' %}
    <p>
        Test
    </p>
    {% endif %}
    #20372
    sofdesigne
    Participant

    thanks a lot,

    it works because it’s in a new variable, not just the quotes ( i tried them before ), very clever

    thanks again

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.