Kjapp og trygg hosting for Wordpress

Fjerne poster på forsiden ved hjelp av time stamp

Tonny Kluften

Administrator
Jeg skal fjerne poster på en side ved hjelp av time stamp. Og det skal fungere slik:
Kode:
<?php
if (have_posts()) : while (have_posts()) : the_post(); 
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}

$secondsbetween = strtotime($expirestring)-time()-7200;
if ( $secondsbetween > 0 ) {
// For exemple…
the_title();
the_excerpt();
}
endwhile;
endif;
?>

Dette her er hva jeg har, jeg trenger hjelp til å få koden under her til å fungere som den over her:

Kode:
<?php if (have_posts()) : while (have_posts()) : the_post(); // the loop ?>

    <div class="post">
    
<?php
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time()-7200;
if ( $secondsbetween > 0 ) 
?>

<div id="kategori">
        <strong>Type:</strong> <?php the_category(', ') ?>
</div>

	<!--post title as a link-->
	<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e("Permanent link to"); ?> <?php the_title(); ?>"><?php the_title(); ?></a></h3>

	<!--post text with the read more link-->
	<?php the_content('read the rest of this post &raquo;');  ?>

    </div><!--end one post-->
	
	<?php endwhile;  // end of one post ?>

    <!-- Previous/Next page navigation -->
    <div class="page-nav">
	    <div class="nav-previous"><?php previous_posts_link('&laquo; Previous Page') ?></div>
	    <div class="nav-next"><?php next_posts_link('Next Page &raquo;') ?></div>
    </div>    

	<?php else : // do not delete ?>

    <div class="page">

    <h3><?php _e("Page Not Found");  ?></h3>
    <p><?php _e("We're sorry, but the page you are looking for isn't here."); ?></p>
    <p><?php _e("Try searching for the page you are looking for or using the navigation in the header or sidebar"); ?></p>
    
    </div>

	<?php endif; // do not delete ?>
 
Sist redigert:

nukleuz

Medlem
PHP:
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
// Hva skjer her om $expirationtime ikke er array?
$secondsbetween = strtotime($expirestring)-time()-7200;

Prøv også gjør en output test for å se hva du får:

PHP:
$secondsbetween = strtotime($expirestring)-time()-7200;
$OutputTemp = <<<EOT
time() = %s (timestamp: %d) <br />
\$secondsbetween = %s (timestamp: %d) <br />
\$expirationtime = %s (timestamp: %d)
EOT;

echo sprintf( $OutputTemp, date('d.m.y H:i:s', time()), time(), date('d.m.y H:i:s', $secondsbetween), $secondsbetween, date('d.m.y H:i:s', expirationtime), $expirationtime )

Her sjekker du også om tiden er større en null?
PHP:
if ( $secondsbetween > 0 )
Noe den vil være dersom tiden er etter 01.01.1970 - som er starten til timestamp. $secondsbetween vil jo såklart inneholde ett tidsstempel som er eldre en time(), og dersom du trekker fra ett tidsstempel som er time() vil det alltid bli et negativt resultat. I tillegg trekker du fra ytterligerer 7200.

Du sier i innlegget at du skal fjerne innlegg vet hjelp av timestampt, men ikke hvilke kriterier egentlig. Dog tipper jeg at du vil fjerne innlegg som er eldre enn 7200 sekunder?!

Det du kanskje heller ønsker å gjøre er?

PHP:
$secondsbetween = strtotime( $expirestring ) - ( time()-7200 );
(merk deg parentes; for å trekke fra 7200 av time(), før den substraheres med $expirestring)

eller

PHP:
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post(); 
	$expirationtime = get_post_custom_values('expiration');
	
	if (is_array( $expirationtime ))
		$expirestring = implode( $expirationtime );

	$secondsbetween = ( isset($expirestring) ? strtotime( $expirestring ) : time() );

	if ( $secondsbetween > (time()-7200) ) {
		// For exemple…
		the_title();
		the_excerpt();
	}
endwhile;
endif;
?>
 
Sist redigert:

Tonny Kluften

Administrator
Jeg har muligens forklart meg litt/ekstremt uklart her, beklager det. Det jeg skal gjøre er at innlegg automatisk fjernes fra siden på et tidspunkt satt i Wordpress custom fields, custom fields navn er "expiration". Verdi er f.eks. "18.10.2011 22:03". Verdien -7200 er satt fordi tidspunktet innlegget skal fjernes skal stemme med servertiden/norsk tid.

Dette her som jeg skrev i første post fungerer:
Kode:
<?php
if (have_posts()) : while (have_posts()) : the_post(); 
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}

$secondsbetween = strtotime($expirestring)-time()-7200;
if ( $secondsbetween > 0 ) {
// For exemple…
the_title();
the_excerpt();
}
endwhile;
endif;
?>

Og dette under her er orginalkoden, her forsvinner ikke noen poster automatisk. Det jeg ønsker er at orginalkoden under her skal brukes sammen med koden over her slik at all info og styling beholdes på nettsiden.

Kode:
<?php if (have_posts()) : while (have_posts()) : the_post(); // the loop ?>

    <div class="post">

<div id="kategori">
        <strong>Type:</strong> <?php the_category(', ') ?>
</div>

	<!--post title as a link-->
	<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e("Permanent link to"); ?> <?php the_title(); ?>"><?php the_title(); ?></a></h3>

	<!--post text with the read more link-->
	<?php the_content('read the rest of this post &raquo;');  ?>

    </div><!--end one post-->
	
	<?php endwhile;  // end of one post ?>

    <!-- Previous/Next page navigation -->
    <div class="page-nav">
	    <div class="nav-previous"><?php previous_posts_link('&laquo; Previous Page') ?></div>
	    <div class="nav-next"><?php next_posts_link('Next Page &raquo;') ?></div>
    </div>    

	<?php else : // do not delete ?>

    <div class="page">

    <h3><?php _e("Page Not Found");  ?></h3>
    <p><?php _e("We're sorry, but the page you are looking for isn't here."); ?></p>
    <p><?php _e("Try searching for the page you are looking for or using the navigation in the header or sidebar"); ?></p>
    
    </div>

	<?php endif; // do not delete ?>
 

typisk

Utvikler
Kode:
<?php 
if (have_posts()) :  while (have_posts()) : the_post(); // the loop

$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}

$secondsbetween = strtotime($expirestring)-time()-7200;

if ( $secondsbetween > 0 ) {
 ?>

    <div class="post">

<div id="kategori">
        <strong>Type:</strong> <?php the_category(', ') ?>
</div>

	<!--post title as a link-->
	<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e("Permanent link to"); ?> <?php the_title(); ?>"><?php the_title(); ?></a></h3>

	<!--post text with the read more link-->
	<?php the_content('read the rest of this post &raquo;');  ?>

    </div><!--end one post-->
	
	<?php   // end of one post ?>

    <!-- Previous/Next page navigation -->
    <div class="page-nav">
	    <div class="nav-previous"><?php previous_posts_link('&laquo; Previous Page') ?></div>
	    <div class="nav-next"><?php next_posts_link('Next Page &raquo;') ?></div>
    </div>    

<?php } else { 
// expirationtime
?>

    <div class="page">

    <h3><?php _e("Page Not Found");  ?></h3>
    <p><?php _e("We're sorry, but the page you are looking for isn't here."); ?></p>
    <p><?php _e("Try searching for the page you are looking for or using the navigation in the header or sidebar"); ?></p>
    
    </div>


<?php
} 
endwhile;
else:
// has post
?>

    <div class="page">

    <h3><?php _e("Page Not Found");  ?></h3>
    <p><?php _e("We're sorry, but the page you are looking for isn't here."); ?></p>
    <p><?php _e("Try searching for the page you are looking for or using the navigation in the header or sidebar"); ?></p>
    
    </div>

	<?php endif; ?>

Oppdatert, så en feil.
 
Sist redigert:
Topp