Home › Forum › Problemi Vari con WP › Navigare i post per mesi
-
AutorePost
-
-
11 Maggio 2008 alle 14:25 #6628essezetaPartecipante
Ciao a tutti…
Mi sono realizzato il tema da solo… non sono matto… e avrei anche di meglio da fare… ma cosଠho visto l’interno del giocattolo… e ora mi rendo conto di come avvengono un sacco di cose di cui prima potevo solo vedere gli effetti…
Fila tutto liscio… però alcuni dettagli mi sfuggono…
Avete presente <?php next_post_link(); ?>, la navigazione verso i contenuti successivi o precedenti quando si supera il numero massimo di visualizzazioni?
Bene, e se io lo volessi nell’elenco dei post?
Naturalmente mi basta inserire <?php next_post_link(); ?> nell’index…
Cioè io avevo impostato la visualizzazione di 10 post… quindi l’undicesimo non lo vedevo se non cercando negli archivi o avanzando post per post… Cosଠinvece posso navigare per decine di post…
E se invece volessi che l’elenco dei post visualizzati fosse quello del mese corrente?
E che <?php next_post_link(); ?> mi visualizzasse il mese successivo o precedente?
Ho provato usando <?php next_post_link(‘%link’, ‘Next post in category’, TRUE); ?> e organizzando ogni mese in una categoria nominata col nome del mese…
Ma a parte che ogni anno i mesi si ripetono (e già !), non è una soluzione, ma una toppa… sicuramente esiste una procedura più logica, pulita e pratica?
PS: cosଠse io inserisco un post datato il mese prossimo, non mi si visualizza fino all’1 del mese… suppongo bene? Ho letto da qualche parte che i post possono essere a tempo… sto cercando questo bookmark ma forse non l’ho salvato…
Qualcuno mi sa dare un consiglio?
Grazie
g
-
12 Maggio 2008 alle 22:46 #53583
-
12 Maggio 2008 alle 22:48 #53586essezetaPartecipante
Ho trovato un post in cui viene illustrato proprio ciò che cercavo, how to create a monthly navigation system, eccolo qui…
Però non mi funziona… mi dà errore…
Qualcuno mi sa dire cosa sbaglio?
Ecco cosa inserisco in index.php, o meglio cosa cerco di inserire…
1 – Codice per creare una query
ecco il codice:
//custom query code
//check for cmonth added by the monthly nav
$cmonth = $_GET["cmonth"];
if($cmonth==""){$cmonth = 0;}
//get the month minus the offset of cmonth
$current_month = date('m', strtotime($date.'-'.$cmonth . 'months' ));
//get the year minus the offset of cmonth
$current_year = date('Y', strtotime($date.'-'.$cmonth . 'months' ));
//make the query
query_posts("monthnum=$current_month&year=$current_year&order=DESC");
2 - Codice per visualizzare label del mese corrente e contenuti
ecco il codice:
//set a parameter i as 1
//this helps to identify the first post
$i=1
//loop starts
if (have_posts()) : while (have_posts()) : the_post(); ?>
//display the month and year at the top of the page
<?php if($i=='1'){echo '
<h1>'; the_time('F Y'); echo '</h1>
';} ?>
<!--post title -->
<h2><?php the_title(); ?></h2>
//post content
<?php the_content(__('Read more'));?>
//increase i through the loop
<?php $i++; ?>
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
//end the loop
<?php endif; ?>
3 - Navigazione verso il mese successivo o precedente...
ecco il codice:
//set values for the next and previous cmonth
$cmonth_num_plus = $cmonth + 1;
$cmonth_num_minus = $cmonth - 1;
//set up a way to check the first day
$cmonth_check = $current_month-$cmonth;
//set the date that your blog starts from
//there should be no blog posts earlier than this date
//in this example the start date is sept 2007
//the month is represented by a number
$start_month = 8;
$start_year = 2007;
//set the divider between the links
$divider = ' | ';
//if the previous month exists display the 'previous' link
if($current_year >= $start_year && $cmonth_check>$start_month){
echo '<a href="'.get_bloginfo('url').'/?cmonth=';
echo $cmonth_num_plus.'">« Previous Month';
}
//if there is a 'previous' and 'next' display the divider
if($current_year >= $start_year && $cmonth_check>$start_month && $cmonth>0)
{echo $divider;}
//if its not the current month display the 'next' link
if($cmonth>0){
echo '<a href="'.get_bloginfo('url').'/?cmonth=';
echo $cmonth_num_minus.'">Next Month »';
}
-
13 Maggio 2008 alle 21:20 #53608essezetaPartecipante
Aggiorno la faccenda…
una delle due cose l’ho risolta, l’altra no… nessuno sa indicarmi una soluzione?
Sono riuscito a far visualizzare la lista dei post del mese corrente… ecco:
<?php $cmonth = $_GET["cmonth"]; ?>
<?php if($cmonth==""){$cmonth = 0;}
$current_month = date('m', strtotime($date.'-'.$cmonth . 'months' ));
$current_year = date('Y', strtotime($date.'-'.$cmonth . 'months' ));
query_posts("monthnum=$current_month&year=$current_year&order=DESC"); ?>
<?php $i=1 ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if($i=='1'){echo '
<h1>'; the_time('F Y'); echo '</h1>
';} ?>
<!--post title -->
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read more'));?>
<?php $i++; ?>
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?>
Ora però non riesco a creare una navigazione che mi permetta di accedere alle mensilità precedenti e da queste a quelle successive...
Ho questo codice, che però non mi genera niente...
<?php $cmonth_num_plus = $cmonth + 1; ?>
<?php $cmonth_num_minus = $cmonth - 1; ?>
<?php $cmonth_check = $current_month-$cmonth; ?>
<?php $start_month = 5; ?>
<?php $start_year = 2008; ?>
<?php $divider = ' | '; ?>
<?php if($current_year >= $start_year && $cmonth_check>$start_month){
echo '<a href="'.get_bloginfo('url').'/?cmonth=';
echo $cmonth_num_plus.'">« Previous Month';
}
if($current_year >= $start_year && $cmonth_check>$start_month && $cmonth>0)
{echo $divider;}
if($cmonth>0){
echo '<a href="'.get_bloginfo('http://localhost/ildiavoloa4').'/?cmonth=';
echo $cmonth_num_minus.'">Next Month »';
} ?>
Grazie
g
-
14 Maggio 2008 alle 9:07 #53614wollyAmministratore del forum
essezeta devi avere pazienza perchè purtroppo io di codice so poco, steve è indispinobile per motivi personali per un pò di giorni.
Metto il tuo 3d nei preferiti e appena steve torna online glielo segnalo.
-
14 Maggio 2008 alle 13:18 #53619essezetaPartecipante
Tranquillissimo wolly, certo che ho pazienza… ci mancherebbe…
I miei aggiornamenti del post sono dovuti a piccoli sviluppi che riesco a mano a mano a fare da solo…
Semplicemente aggiorno il mio caso, che steve avrebbe già risolto, ma magari ci riesco da solo… (chissà !! ) io intanto condivido i miei piccoli passi pubblicandoli… tutto qua…
comunque grazie mille dell’attenzione
g
-
20 Maggio 2008 alle 10:20 #53791essezetaPartecipante
Quasi perfetto…
Ora riesco a fare entrambe le cose:
- sia a visualizzare i post del mese corrente (tra parentesi la necessità nasce da ciò: immaginate di dover gestire un mensile on line… impostate la visualizzazione di 99 post, ma finché c’è un solo post volete che venga visualizzato solo quello)
- sia a visualizzare i tasti di navigazione verso i mesi precedenti e da questi verso i successivi…
L’unica cosa è che non capisco esattamente come si imposta lo START… cioè la data a partire dalla quale comincia la visualizzazione dei post… c’è un valore numerico, ma a numero uguale non corrisponde mese uguale… devo procedere sempre a tentativi… qualcuno è in grado di spiegarmi come conta i mesi questo script? vedi il pezzo di codice in grassetto
+ se qualcuno nota una scrittura poco pulita e mi volesse dare una dritta per semplificare il codice e le funzioni, gliene sarei grato…
Comunque per come è ora questo codice è funzionante, eccolo postato qui di seguito (lascio anche i commenti originali) e all’opera su kubric a questo link
Un saluto a tutti…
<!–//custom query code check for cmonth added by the monthly nav–>
<?php $cmonth = $_GET[“cmonth”];
if($cmonth==””){$cmonth = 0;}
//get the month minus the offset of cmonth
$current_month = date(‘m’, strtotime($date.’-‘.$cmonth . ‘months’ ));
//get the year minus the offset of cmonth
$current_year = date(‘Y’, strtotime($date.’-‘.$cmonth . ‘months’ ));
//make the query
query_posts(“monthnum=$current_month&year=$current_year&order=DESC”); ?>
<!–The last, and trickiest part is the navigation, which changes the $cmonth parameter in th URL string:–>
<!–//set values for the next and previous cmonth–>
<?php $cmonth_num_plus = $cmonth + 1; ?>
<?php $cmonth_num_minus = $cmonth – 1; ?>
<!–//set up a way to check the first day–>
<?php $cmonth_check = $current_month-$cmonth; ?>
<!–//set the date that your blog starts from
//there should be no blog posts earlier than this date
//in this example the start date is sept 2007
//the month is represented by a number–>
<?php $start_month = -4; ?>
<?php $start_year = 2008; ?>
<!–//set the divider between the links–>
<?php $divider = ‘ | ‘; ?>
<!–//if the previous month exists display the ‘previous’ link–>
<?php if($current_year >= $start_year && $cmonth>$start_month){
echo ‘<a href=”‘.get_bloginfo(‘url’).’/?cmonth=’;
echo $cmonth_num_plus.'”>« Previous Month’;
} ?>
<!–//if there is a ‘previous’ and ‘next’ display the divider–>
<?php if($current_year >= $start_year && $cmonth_check>$start_month && $cmonth>0)
{echo $divider;} ?>
<!–//if its not the current month display the ‘next’ link–>
<?php if($cmonth>0){
echo ‘<a href=”‘.get_bloginfo(‘url’).’/?cmonth=’;
echo $cmonth_num_minus.'”>Next Month »’;
} ?>
<!–//set a parameter i as 1 this helps to identify the first post–>
<?php $i=1 ?>
<!–//loop starts–>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!–//display the month and year at the top of the page–>
<?php if($i==’1′){echo ‘
<h1>’; the_time(‘F Y’); echo ‘</h1>
‘;} ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<h2>“><?php the_title(); ?></h2>
<small><?php the_time(__(‘F jS, Y’, ‘kubrick’)) ?> <!– by <?php the_author() ?> –></small>
<div class=”entry”><?php the_content(__(‘Read the rest of this entry »’, ‘kubrick’)); ?></div>
</div>
<!–//increase i through the loop–>
<?php $i++; ?>
<?php endwhile; else: ?>
<?php _e(‘Sorry, no posts matched your criteria.’); ?>
<!–//end the loop–>
<?php endif; ?>
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.