Home › Forum › Problemi Vari con WP › selezionare ultimi n post
-
AutorePost
-
-
10 Maggio 2008 alle 15:18 #6619diego77Partecipante
Ciao ragazzi,
sono a digiuno di PHP (provengo da ASP) e pur avendo cercato non sono riuscito a capire come fare per selezionare e visualizzare, ad esempio, gli ultimi 4 post -1 (solo titolo e categoria).
L’ultimo post viene visualizzato in primo piano. A me servirebbe estrarre informazioni essenziali per i 4 precedenti.
Spero possiate aiutarmi.
-
10 Maggio 2008 alle 17:26 #53543AleswarzPartecipante
Io farei cosà¬: col primo ciclo chiami l’ultimo post della categoria “1” mentre col secondo chiami gli altri 4 sempre della categoria “1” mostrando solo titolo e categoria impostando però come offset “offset=1”. Impostando l’offset ad 1 il primo post non verrà mostrato mentre il 2,3,4,5 sà¬.
Spero di essere stato chiaro ed ovviamente se qualcuno conosce opzioni migliori e più sbrigative sarei felice di impararle!!
Ecco il codice:
<?php $posts = get_posts( “category=1&numberposts=1&offset=0” ); ?>
<?php if( $posts ) : ?> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<h2>” rel=”bookmark” title=”Permalink a <?php the_title(); ?>”><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php the_category(‘, ‘) ?>
<?php endforeach; ?> <?php endif; ?>
<?php $posts = get_posts( “category=1&numberposts=4&offset=1” ); ?>
<?php if( $posts ) : ?> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<h2>” rel=”bookmark” title=”Permalink a <?php the_title(); ?>”><?php the_title(); ?></h2>
<?php the_category(‘, ‘) ?>
<?php endforeach; ?> <?php endif; ?>
-
10 Maggio 2008 alle 17:54 #53544diego77Partecipante
Grazie.
Questo lo tengo da parte. Se volessi selezionarli indipendentemente dalla categoria (ultimi 4 post -1) di tutte le categorie?
-
10 Maggio 2008 alle 18:24 #53545AleswarzPartecipante
Basta non specificare la categoria quando fai get_posts ovvero:
<?php $posts = get_posts( “numberposts=4&offset=1” ); ?>
-
11 Maggio 2008 alle 6:11 #53548diego77Partecipante
Ti ringrazio. Ho risolto cosà¬, perché con il tuo codice non riuscivo a fare l’ordinamento corretto (decrescente, anziché crescente):
<?php
// I love WordPress so
query_posts('showposts=1&offset=0');
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; endif; ?>
<?php
// I love WordPress so
query_posts('showposts=1&offset=1');
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
- "><span class="date"><?php the_time( $hemingway->date_format() ) ?></span> <?php the_title() ?>
<?php endwhile; endif; ?>
-
11 Maggio 2008 alle 7:03 #53553AleswarzPartecipante
Sicuro di aver messo quel codice? Io l’ho provato e funziona correttamente..comunque per l’altra domanda io farei sempre lo stesso procedimento lavorando però con l’offset, impostando l’offset decidi quali articoli escludere:
<?php $posts = get_posts( “numberposts=2&offset=1” ); ?>
<?php if( $posts ) : ?> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<?php the_category(‘, ‘) ?>
<?php endforeach; ?> <?php endif; ?> <!– con questo visualizzi gli ultimi 2 post (penultimo e terzultimo) -1 –>
<?php $posts = get_posts( “numberposts=2&offset=3” ); ?>
<?php if( $posts ) : ?> <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<?php the_category(‘, ‘) ?>
<?php endforeach; ?> <?php endif; ?> <!– con questo visualizzi gli ultimi 2 post (quartulimo e quintultimo) -3 –>
Non so se ti è chiaro ma con il numero di offset escludi nel primo caso il primo articolo e nel secondo caso, essendo impostato a 3 il primo, penultimo e terz’ultimo
-
11 Maggio 2008 alle 7:20 #53551diego77Partecipante
Ho copiato pari-pari quest’altro codice (per il quale ti ringrazio), eppure mi continua a visualizzare gli ultimi 4 post (incluso l’ultimo), dal più vecchio al più nuovo.
Con l’altro codice ho risolto, ma imparare nuove cose non fa mai male
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.