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.

Include entries from a second weblog if first weblog doesn't have enough current entries

June 19, 2010 5:02pm

Subscribe [2]
  • #1 / Jun 19, 2010 5:02pm

    kasdesign

    13 posts

    Hi,

    I’d like to have a list of only 5 of the most recent entries within the past 10 days display. If there are less than 5, I’d like to include entries from another weblog to fill the list. I used the http://expressionengine.com/wiki/Start_on_for_recent_content/, but I can’t seem to figure out how to get the list to fill out if there are less than 5.

    I tried doing an if statement with the {total_results} tag, along with some other things, but was unsuccessful. I seem to be stuck at the moment. If anyone has any ideas, I’d greatly appreciate it.

    Thanks!

  • #2 / Jun 19, 2010 7:17pm

    Michael Rog

    179 posts

    Using the {total_results} tag to achieve this effect probably leads to nesting {exp:weblog:entries} tags in this case, which isn’t very useful.

    Try “joining” the two (or more) weblogs at the outset (assume you’ll need both of them). Then, sort by weblog first and date second to make sure the entries from the primary weblog show up first if they exist. Then user your start_on=”” trick to show only recent entries, and user the limit=”” parameter to show the desired number of entries.

    This is untested and off the top of my head, but I think it’ll look something like this:

    {exp:weblog:entries  weblog="primary|secondary"  orderby="weblog_short_name|date"  sort="asc|desc" limit="5" start_on="<?php echo date('Y-m-d H:i', strtotime('-10 days')); ?>"}
  • #3 / Jun 19, 2010 7:53pm

    Michael Rog

    179 posts

    erm… Actually, it seems like EE won’t let us sort by weblog_id or weblog_short_name. So… actually this will probably take some PHP. Do you need the entry contents or just the title info?

  • #4 / Jun 19, 2010 8:23pm

    Michael Rog

    179 posts

    The only way I can think of to do this while preserving a specific weblog order is to select the posts using MySQL and then spit them out with the {exp:weblog:entries} tag.

    The query to give you the entry IDs is something like:

    SELECT entry_id
    FROM exp_weblog_titles
    WHERE entry_date > <?php echo date('U',strtotime('-10 days')); ?>
    ORDER BY
       CASE channel_id 
          WHEN '4' THEN 1
          WHEN '2' THEN 2
          WHEN '3' THEN 3
       END ASC,
       entry_date DESC
    LIMIT 5

    Then you could assemble those values into a parameterizable string (using PHP) and put that string inside the entry_id=”” parameter of the Weblog tag…

    There has to be an easier way to do this.

  • #5 / Jun 19, 2010 11:36pm

    ender

    1644 posts

    I managed to get something along these lines to work by using the simple math plugin along with the string plugin... though I’ll warn you there is a bit of template processing order voodoo in there as well 😉

    {exp:weblog:entries weblog="another_weblog" limit="5"}
        {exp:string:set name="filler_needed"}{exp:simple_math calculate="5-{total_results}"}{/exp:string:set}
        another_weblog #{count}: {title}
    {/exp:weblog:entries}
    
    {exp:stats}
        {exp:string:output name="filler_needed" parse="inward"}
            {exp:weblog:entries weblog="default_site" limit="{string}"}
                default_site #{count}: {title}
            {/exp:weblog:entries}
        {/exp:string:output}
    {/exp:stats}

    the {exp:stats} tag pair is in there purely to prevent the EE template parser from running the {exp:string:output} tag too early (before its value is set).  It tends to run things in levels, so the outer tags are run first, then the next tags, etc.

    Output on my test box looks like this with 2 entries in “another_weblog”:

    another_weblog #1: entry 2
    
    another_weblog #2: entry 1
    
    default_site #1: something else
    
    default_site #2: test entry
    
    default_site #3: asdf4
  • #6 / Jun 20, 2010 4:47pm

    kasdesign

    13 posts

    Thanks Michael and Ty!

    I really appreciate your help. I figured I wasn’t looking at this the right way.

    Ty, I went with the example you posted and it works great.

    Thanks again for your help guys!

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

ExpressionEngine News!

#eecms, #events, #releases