ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Conditional Statement and Checkbox Field

November 09, 2011 2:27pm

Subscribe [3]
  • #1 / Nov 09, 2011 2:27pm

    FrankJohnson

    150 posts

    Hi. I’m trying to set up an approach where a statement will appear in the sidebar of a single-entry page when the entry has a checkbox field checked on the Publish page in the control panel. Here is an example of such a single-entry page on the site in question.

    Specifically, I want to be able to specify within the control panel that a particular entry is available for syndication and then show a statement to that effect within the sidebar of that entry’s page.

    I have set up the custom channel field with a field type of Checkboxes, text formatting of None and a single Multi-Select Option of Yes.

    I’m then trying to put the appropriate code within the sidebar (which is a separate template embedded in the single-entry template - so I can’t put the code within the main channel entries tag that calls the article’s content).

    I thought something like this would work:

    {exp:channel:entries url_title="{segment_3}"}
    {available_for_syndication}
      {if item != ""}
      This article is available for syndication
      {/if}
    {/available_for_syndication}
    {/exp:channel:entries}

    It’s working on the single-entry page - displaying or not displaying the “This article is available for syndication” line depending on whether or not the checkbox is checked. The problem is that the statement is also showing up on every non single-entry page.

    For the moment, I’ve removed the code so it’s not functioning at all right now.

    Any thoughts on ways to fine-tune the code so that the statement doesn’t show up on non single-entry pages? I’ve given some brief thought about ways I might add parameters to the conditional statement so that it would only apply if it was a single-entry page - something based on the segments perhaps - but my first thought (of adding a conditional that segment 3 is not equal to “”) won’t work because there are other pages with a third segment.

    I have a feeling that I might be missing something obvious, but ....

    Thanks in advance for any help anyone can offer - I really appreciate it.

    Frank

  • #2 / Nov 09, 2011 4:40pm

    Rob Allen

    3114 posts

    If it was me I wouldn’t use a checkbox here, but opt for a select (or radio input).

    If using a select field populate the options list with:

    No
    Yes

    Then in your template query the value of the field:

    {if my_select_field == "Yes"}
    This article is available for syndication
    {/if}

     

     

     

  • #3 / Nov 09, 2011 5:02pm

    FrankJohnson

    150 posts

    Thanks Rob. Per your suggestion, I changed the code to this:

    {exp:channel:entries url_title="{segment_3}"}
    {if available_for_syndication == "Yes"}
    This article is available for syndication
    {/if}
    {/exp:channel:entries}

    But it continues to behave the same way - changing the select box to a value of Yes causes the text “This article is available for syndication” to appear in the sidebar on the single-entry page but also on every other page of the site.

    Thoughts?

    Thanks again,

    Frank

  • #4 / Nov 09, 2011 6:36pm

    Rob Allen

    3114 posts

    Did you change the custom field type to a select or radio button? If you did make sure you edit and save the entry again else there won’t be a value there.

  • #5 / Nov 09, 2011 7:10pm

    FrankJohnson

    150 posts

    Hi Rob - thanks again for the quick response. Yes, I changed the custom field type to select and then went back into the entry and changed the value to Yes.

    Frank

  • #6 / Nov 10, 2011 7:07am

    Mark Bowen

    12637 posts

    Your best bet here for figuring all of this out would be to just output that fields value to your template each time and see what is being spat out. Forget about the conditional for now and just output the value.

    If you are getting the same value every time regardless then that should give you a heads up as to what the problem is here.

    Best wishes,

    Mark

  • #7 / Nov 10, 2011 7:33am

    FrankJohnson

    150 posts

    Thanks Mark. Per your advice, I changed the code in the sidebar to be this:

    {exp:channel:entries url_title="{segment_3}"}
    {available_for_syndication}
    {/exp:channel:entries}

    and then set the value of the available_for_syndication field to Yes for this article.

    If you look at the very bottom of the sidebar on that page, you should see a small “Yes”.

    But if you look at other pages on the site - this one for example or the home page - I’m also seeing the small “Yes” at the bottom of the sidebar on those pages.

    That doesn’t make sense to me since my channel:entries tag is specifying that it should be keying off the url_title in segment 3. Again, there’s probably something obvious that I’m missing, but it’s not jumping out at me right now.

    Thanks!
    Frank

  • #8 / Nov 10, 2011 7:54am

    Mark Bowen

    12637 posts

    Hi Frank,

    Do you have to have the code as an embedded template or could you not just have the entries tag code in that template in place of the embed?

    It would definitely help you for fault finding if you were able to place the code directly into the template instead.

    Best wishes,

    Mark

  • #9 / Nov 10, 2011 9:19am

    FrankJohnson

    150 posts

    Thanks Mark. Right now, the code is part of the sidebar template which is embedded in every page.

    Are you suggesting putting the snippet of code directly into the single-entry template so that I can narrow down the possibilities - in effect, remove the embed part of the scenario to see if that could be causing the problem?

    Thanks again!

    Frank

  • #10 / Nov 11, 2011 11:26am

    FrankJohnson

    150 posts

    Well, I’ve been able to prevent the value of available_for_syndication from appearing on non-single-entry pages through a complicated series of segment-based conditionals (not ideal, but it works).

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases