ThemeShaper Forums » Thematic

[closed]

Edit the Loop to Expire Posts to Archives Only?

(3 posts)
  • Started 9 years ago by elderberry
  • Latest reply from helgatheviking
  • This topic is not resolved
  1. elderberry
    Member

    My client wants to have posts stop displaying on the homepage when they are more than 4 weeks old, but continue to display in the archives. I know I need to filter the loop in functions.php but it's beyond my coding abilities. I did find these bits which may be useful... anyone willing to take a stab at it?

    <?php
    	$today = date("r");
    	$articledate = get_the_time('r');
    	$difference = round((strtotime($today) - strtotime($articledate))/(24*60*60),0);
    
                if ($difference >= 4) {
                   Do Stuff Here
                }else{
                   Do Stuff Here
                }
    ?>
    ***PUT THIS INSIDE THE LOOP, FIRST THING***
    
    <?php  //to check against expiration date;
    $timestamp = strtotime("now + 8 hours");
    $currentdate = date('YmdHis', $timestamp);
    $expirationdate = get_post_custom_values('expiration');
    if (is_null($expirationdate)) {
                        $expirestring = '30005050235959'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
    } else {
    
    if (is_array($expirationdate)) {
                                          $expirestringarray = implode($expirationdate);
                                                    }
    $markup = array("/",":"," ");
    $expirestring = str_replace($markup,"",$expirestringarray);
    } //else
    if (( $expirestring > $currentdate ) || (is_archive())) { ?>
    
    ***THEN PUT THE FOLLOWING LINE AT THE VERY END OF THE LOOP***
    
    <?php } //end if for expiration; ?>
    Posted 9 years ago #
  2. elderberry
    Member

    This seems to be working on my local machine, but not on the live site... any coders wishing to take a look are much appreciated :-)

    function remove_thematic_index_loop(){
    	remove_action('thematic_indexloop','thematic_index_loop');
    	}
    add_action('init', 'remove_thematic_index_loop');
    
    function child_index_loop(){
    	while ( have_posts() ) : the_post() ?>
    <?php
    	$today = date("r");
    	$articledate = get_the_time('r');
    	$difference = round((strtotime($today) - strtotime($articledate))/(24*60*60),0);
    
                if ($difference < 25) { ?>
    			<div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?> <?php if ( has_post_thumbnail() ) { echo "conthumb"; } ?>">
        			<div><?php the_post_thumbnail(); ?>
        			<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><div class="entry-title">
        				<h3><?php the_title(); ?></h3></div>
        				<p><?php the_excerpt(); ?>
    </p>    			</a>
        			</div>
    				<div class="entry-content">
    					<?php thematic_content(); ?>
    				</div>
    				<?php thematic_postfooter(); ?>
    			<br style="clear: both;" />
    			</div><!-- .post --><?php
                }else{
                               }
    
    		endwhile;
    }
    add_action('thematic_indexloop', 'child_index_loop');
    Posted 9 years ago #
  3. you could try the jedi-knight way to limit the query using the posts_where() filter that is shown in the codex:

    http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

    tweaking the "Return posts from the last 30 days:" example to use weeks (though you could obvi just use 28 days) and only applying this query mod to the index since you said you want all the archives to show normally everywhere else, i get the following:

    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    	if(is_home()){
    		// posts in the last 4 weeks
    		$where .= " AND post_date > '" . date('Y-m-d', strtotime('-4 weeks')) . "'";
    	} else {
    		$where = '';
    	}
    	return $where;
    }
    add_filter( 'posts_where', 'filter_where' );

    though you no longer need your loop, as a heads up you don't need to remove and add the loop any more, you can just call your child_index_loop() function childtheme_override_index_loop() instead and it will be automagically added in the right place.

    Posted 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.