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 to test if entry is latest/newest entry within channel (using Playa field)

August 01, 2012 7:10pm

Subscribe [2]
  • #1 / Aug 01, 2012 7:10pm

    Varyable

    11 posts

    Here’s my situation.

    I’ve got a channel of Articles and a channel of Issues. Each issue has many articles. I’m using a Playa field in the Articles channel to relate each Article entry to an Issue entry.

    In a template for a single article, I’d like to be able to tell if the specific article is a part of the latest issue or not. Something like this:

    {playa_related_issue}
       {if IS_LATEST_ISSUE}
          From the latest issue
       {if:else}
          From issue {issue_number}
       {/if}
    {/playa_related_issue}

    The latest issue would simply be the most recent entry in the issues channel.

    I can’t really think of way to do this. Any thoughts? Thanks!

  • #2 / Aug 02, 2012 9:43am

    Dan Decker

    7338 posts

    Hi Scott,

    You could do a check on the entry date perhaps. Depending on when issues are published, let’s say monthly for example, this may be a way to go:

    {if {entry_date format="%m"} == {current_time format="%m"}}
      do stuff…
    {/if}

    What do you think?

    Cheers,

     

  • #3 / Aug 02, 2012 1:17pm

    Varyable

    11 posts

    Hmm, I don’t think this will cover me fully, as (a) the most recent issue could be several months back or (b) there could possibly be multiple issues in the same month. I appreciate the response though! Thanks!

    I’m not sure there is going to be an easy way to do this, but it must be possible. I will report back to this thread if I figure something out.

  • #4 / Aug 02, 2012 3:44pm

    Dan Decker

    7338 posts

    Hey Scott,

    That was the easy out, going on dates keeps things a bit more automatic 😉

    A more manual process would be to set up a custom status that marks the article as part of the “current issue”. That carries a bit of editorial overhead though.

    Cheers,

  • #5 / Aug 02, 2012 3:54pm

    Varyable

    11 posts

    A better option for a more manual process would be a checkbox/status on the Issue channel, flagging it as the current issue. I’d be able to read that just fine. I just would really rather this work automatically and have it be a good experience for the client. Getting around in EE is confusing enough as it is for folks.

  • #6 / Aug 02, 2012 8:10pm

    Royama Design

    34 posts

    You can use Playa’s parent and child tags to list all the articles that are related to the latest issue. But I’m not sure you can use those tags the exact way you describe—in a single article template.

    You could definitely have a template that automatically presents any and all articles that are a part of the latest issue without using statuses or custom fields indicating that an issue is the latest. But this is not exactly what you are describing.

    Is this an option? Or must you have some form of an indicator on a single article page that says this is part of the latest issue?

  • #7 / Aug 02, 2012 8:46pm

    Royama Design

    34 posts

    I think this can be done after all. What you want to do in your single article template is to call the latest issue, pull up all the articles related to it, then try and match it to the current article you are looking at. If you get a match, then the article is in the latest issue.

    Note, in your case: Articles = The Playa Parents and Issues = The Playa Children. Seem backwards, but the Article entries have the Playa field and so are the parents.

    You would put this in your single article template:

    <!-- get latest issue (playa children) -->
    {exp:channel:entries channel="issues" orderby="date" sort="desc" limit="1"}
     
        <!-- get articles of the issue (playa parents) -->
        {exp:playa:parents channel="articles"}
      
            <!-- try and match entry_id of article with the entry_id of an article in the latest issue -->
            <!-- segment_x is the segment in your URL that hold the current articles entry_id -->
            {exp:ifelse parse="inward"}
                {if entry_id == "{segment_x}"}
                    This article is in the latest issue.
                {if:else}
                    Something else…
                {/if}
            {/exp:ifelse}   
        
        {/exp:playa:parents}
    
    {/exp:channel:entries}

    Give it a whirl. I’m not sure if I have the code right, but it should be possible. You may need the Playa coparent tag instead of the parent tag. Not sure.

    Note: I am using the free Mark Croxton IfElse plugin that makes IfElse statements more efficient.

  • #8 / Aug 02, 2012 8:57pm

    Varyable

    11 posts

    Thanks for the response, Sam.

    I was hitting a wall with a similar approach to this, but I think the magic key in here is that you’re using the url segment to get the entry_id of the article. Nice! I’m going to give this all a shot soon and I’ll post how this turns out.

    Also will definitely check out that IfElse plugin.

    Thanks Sam!

  • #9 / Aug 03, 2012 1:08pm

    Dan Decker

    7338 posts

    Hi Scott,

    Let us know how it shakes out.

    Indeed, with the articles related to the “current issue”, you could use a status on the current issue entry.

    Personally, I haven’t used Playa enough, but I look forward to your follow up!

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases