1

We have a client's multi-language site, and are NOT using any plugins to differentiate the site. The different languages are under different url paths (/fr /nl) using pages. However, for the blog element, the client wants all entries to be in one universal blog, but once you go to a French blog, the nav and other elements are French (this is done with segment_1 of fr). Because of that, I want all blog posts in the French category to go to /fr/blog/entry/{url_title} instead of /blog/entry/{url_title}.

This isn't working:

    {exp:channel:entries channel="blog"}
                <div class="blog-entry">
                    <img src="{main-image}" alt="{title}">
     {if category_id == "41"}
       <a href="{path='fr/blog/entry/{url_title}'}"><h1>{title}</h1></a>
    {if:elseif category_id == "42"}
        <a href="/nl/blog/entry/{url_title}/"><h1>{title}</h1></a>
    {if:else}
       <a href="/blog/entry/{url_title}/"><h1>{title}</h1></a>
    {/if}           
        <h5>{entry_date format="%j %M %Y"}</h5>
                    {if summary != ""}<p>{summary}</p>{if:else}<p>{entry}</p>{/if}
                </div>
    {/exp:channel:entries}

However, it's the if category statement that seems to be the culprit. I can replace the <a href="/fr/blog/entry/{url_title}"><h1>{title}</h1></a> with anything and it still shows the h1 title with a link to the /blog/entry/{url_title} page.

2
  • 1
    Can you post your wrapping tags? also are you using the channel entries tag pair or the categories tag pair? – Justin Long Feb 23 '15 at 19:25
  • Sorry, I have edited my post to show the wrapping tags. – shotgunflat Feb 23 '15 at 19:34
1

You have to use the categories tag pair within your channel entries tag because you could theoretically have multiple categories assigned to an entry. IF you do have multiple categories assigned to an entry I would move the language categories into their own group so you could do:

{exp:channel:entries channel="blog"}
<div class="blog-entry">
    <img src="{main-image}" alt="{title}">
    {categories show_group="2" limit="1"}
    {!-- this will loop through each category assigned so you would have duplicates if in multiple categoires --}
        {if category_id == "41"}
            <a href="{path='fr/blog/entry/{url_title}'}"><h1>{title}</h1></a>
        {if:elseif category_id == "42"}
            <a href="/nl/blog/entry/{url_title}/"><h1>{title}</h1></a>
        {if:else}
            <a href="/blog/entry/{url_title}/"><h1>{title}</h1></a>
        {/if}
    {/categories}           

    <h5>{entry_date format="%j %M %Y"}</h5>
    {if summary != ""}<p>{summary}</p>{if:else}<p>{entry}</p>{/if}
</div>
{/exp:channel:entries}

You would then be able to create another categories tag pair to show your normal categories just limit it to your other category groups.

1
  • Thank you. I posted my answer below before I saw your answer, but, yes, it was the correct advice. – shotgunflat Feb 23 '15 at 19:46
0

You can definitely write conditionals based on a category's ID.

If you are indeed using the categories tag pair inside a Channel Entries tag you need to use category_id instead of category (which actually isn't a variable).

Try this instead:

{if category_id == "41"}
    <a href="/fr/blog/entry/{url_title}"><h1>{title}</h1></a>
{if:elseif category_id == "42"}
    <a href="/nl/blog/entry/{url_title}/"><h1>{title}</h1></a>
{if:else}
    <a href="/blog/entry/{url_title}/"><h1>{title}</h1></a>
{/if}

If that doesn't do the trick, then please add a little bit more of your template code to provide context for this conditional (ie, is this inside a channel:entries category variable tag pair, or something else?)

2
  • I did realize that error and added the _id, but that didn't fix. I will show more code now. – shotgunflat Feb 23 '15 at 19:32
  • 1
    THANK YOU. I had added the {categories} tag around the if statement, but that must have been before I corrected the variable to category_id, so I figured that wasn't the case. – shotgunflat Feb 23 '15 at 19:41
0

This worked (I was missing the {categories} tag. [Note, I also had to be sure to have a template fr/blog that worked the same way as /blog]:

    {exp:channel:entries channel="blog" dynamic="no"}
                    <div class="blog-entry">
                        <img src="{main-image}" alt="{title}">
        {categories}
         {if category_id == "41"}
           <a href="/fr/blog/entry/{url_title}"><h1>{title}</h1></a>
        {if:elseif category_id == "42"}
            <a href="/nl/blog/entry/{url_title}/"><h1>{title}</h1></a>
        {if:else}
           <a href="/blog/entry/{url_title}/"><h1>{title}</h1></a>
        {/if}       
        {/categories}   
            <h5>{entry_date format="%j %M %Y"}</h5>
                        {if summary != ""}<p>{summary}</p>{if:else}<p>{entry}</p>{/if}
                    </div>
{/exp:channel:entries}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.