Chiamate a categoria per elenco POST
-
Buonasera a tutti, per la gestione di una parte della home del mio sito c'è questo php qua sotto:
<!-- INNER content2 --> <div id="inner"> [B]<?php $issuesCategory = get_option('tb_home_issues_category'); ?>[/B] <h3>News dalla Regione<span></span></h3> <?php $args = array(); $args['post_type'] = 'post'; $args['post_status'] = 'publish'; $args['posts_per_page'] = 5; [B]if ($issuesCategory) {[/B] [B] $excludeCategory = '-' . $issuesCategory;[/B] [B] $args['cat'] = $excludeCategory;[/B] [B] }[/B] $tbQuery = new WP_Query($args); $latestIndex = 1; if ($tbQuery->have_posts()) : while ($tbQuery->have_posts()) : $tbQuery->the_post(); ?> <?php $postID = get_the_ID(); ?> <?php $postTitle = get_the_title($postID); ?> <?php $postPermalink = get_permalink($postID); ?> <?php if ($latestIndex == 5) { $newsClass = 'news width50 right'; $dfs = 'small'; $tSize = 'dfs'; } elseif ($latestIndex == 4) { $newsClass = 'news width50 left'; $dfs = 'small'; $tSize = 'dfs'; } elseif ($latestIndex == 3) { $newsClass = 'news width50 left'; $dfs = 'small'; $tSize = 'dfs'; } elseif ($latestIndex == 2) { $newsClass = 'news width50 left'; $dfs = 'small'; $tSize = 'dfs'; } else { $newsClass = 'news'; $dfs = 'medium'; $tSize = 'dfm'; } ?> <div class="<?php echo $newsClass; ?>"> <div class="doubleFramed <?php echo $dfs; ?> left"> <a href="<?php echo $postPermalink; ?>" title="<?php $postTitle; ?>"> <?php echo tb_get_thumbnail($postID, $tSize); ?> </a> </div> <div class="right"> <?php if ($latestIndex > 1) { ?> <div class="newsInfo"><?php echo get_the_date('l j F, Y'); ?></div> <?php } ?> <h4><a href="<?php echo $postPermalink; ?>" title="<?php $postTitle; ?>"><?php echo $postTitle; ?></a></h4> <?php if ($latestIndex == 1) { ?> <div class="newsInfo"><?php echo get_the_date('l j F, Y'); ?></div> <?php } ?> <?php if ($latestIndex == 1) { the_excerpt(); } else { echo '<p>' . tb_max_words(get_the_excerpt(), 18) . '...</p>'; } ?> <a href="<?php echo $postPermalink; ?>" title="<?php $postTitle; ?>" class="tinyButton roundButtonX right">Leggi tutto</a> </div> </div> <?php if ($latestIndex == 1) { ?> <div class="horDashed"></div> <?php } ?> <?php $latestIndex++; ?> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?> <?php if ($issuesCategory) { ?> <?php $args2 = array(); $args2['post_type'] = 'post'; $args2['post_status'] = 'publish'; $args2['posts_per_page'] = 3; $args2['cat'] = $issuesCategory; $tbQuery2 = new WP_Query($args2); $issuesIndex = 1; if ($tbQuery2->have_posts()) { ?> <div class="horDouble"></div> <h4><span></span></h4> <?php while ($tbQuery2->have_posts()) : $tbQuery2->the_post(); ?> <?php $postID = get_the_ID(); ?> <?php $postTitle = get_the_title($postID); ?> <?php $postPermalink = get_permalink($postID); ?> <?php if ($issuesIndex == 1) echo '<div class="news width50 left">'; ?> <?php if ($issuesIndex == 2) echo '</div><div class="news width50 right">'; ?> <div class="newsInfo"><?php echo get_the_date('l j F, Y'); ?></div> <h4><a href="<?php echo $postPermalink; ?>" title="<?php echo $postTitle; ?>"><?php echo $postTitle; ?></a></h4> <?php if ($issuesIndex == 1) { the_excerpt(); ?> <a href="<?php echo $postPermalink; ?>" title="<?php echo $postTitle; ?>" class="tinyButton roundButtonX right">Leggi tutto</a> <?php } ?> <?php if ($issuesIndex > 1 && $issuesIndex < 3) echo '<div class="horDashed"></div>'; ?> <?php if ($issuesIndex == 3) echo '</div>'; ?> <?php $issuesIndex++; ?> <?php endwhile; } ?> <?php if ($issuesIndex > 1 && $issuesIndex < 3) echo '</div>'; ?> <?php wp_reset_postdata(); ?> <?php } $args3 = array(); $args3['post_type'] = 'tb_video'; $args3['post_status'] = 'publish'; $args3['posts_per_page'] = 1; $args3['meta_query'] = array( array( 'key' => '_featured', 'value' => 1, 'compare' => '=' ) ); $tbQuery3 = new WP_Query($args3); ?> <?php if ($tbQuery3->have_posts()) : while ($tbQuery3->have_posts()) : $tbQuery3->the_post(); ?> <?php $postID = get_the_ID(); $postTitle = get_the_title(); ?> <?php $postURL = get_post_meta($postID, '_url', true); ?> <div class="horDouble"></div> <h4>Featured <span>Videos</span></h4> <div class="video"> <div> <a href="<?php echo esc_url($postURL); ?>" title="<?php echo $postTitle; ?>"> <?php echo get_the_post_thumbnail($postID, 'videoHome', array('alt' => $postTitle, 'title' => $postTitle)); ?> </a> </div> <div> <h4><a href="<?php the_permalink(); ?>"><?php echo $postTitle; ?></a></h4> <div class="newsInfo"><?php echo get_the_date('l j F, Y'); ?></div> <div class="horDashed"></div> <?php echo '<p>' . tb_max_words(get_the_excerpt(), 40) . '...</p>'; ?> </div> </div> <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?> </div> <!-- .INNER content2 -->che genera due gruppi di articoli presi da due differenti categorie:
il primo blocco sono tutti gli articoli che non rientrano nella categoria del secondo blocco di articoli, questo valore viene passato tramite il menù impostazioni del tema.
La mia richiesta è quella di esplicitare per i due blocchi due differenti categorie direttamente dentro al codice php.
Dove e come devo inserire le chiamate alle categorie?
GRAZIE IN ANTICIPO
-
keggy
[B]<?php $issuesCategory = get_option('tb_home_issues_category'); ?>[/B]Per prima cosa puoi notare che la categoria $issuesCategory viene presa all'interno di una opzione salvata dal tuo tema. Cambiando questa variabile potrai indicare la categoria di appartenenza
$args = array(); $args['post_type'] = 'post'; $args['post_status'] = 'publish'; $args['posts_per_page'] = 5; [B]if ($issuesCategory) {[/B] [B] $excludeCategory = '-' . $issuesCategory;[/B] [B] $args['cat'] = $excludeCategory;[/B] [B] }[/B]Ma fossi in te, subito sotto a questo codice andrei ad inserire quanto segue:
$args['cat'] = 1;In questo modo, anche se non in maniera pulita, sostituisci il valore della categoria da escludere ed imposta l'ID alla categoria che invece vuoi caricare in questa sezione. Raggiunto il prossimo blocco di codice>
<?php if ($issuesCategory) { ?> <?php $args2 = array(); $args2['post_type'] = 'post'; $args2['post_status'] = 'publish'; $args2['posts_per_page'] = 3; $args2['cat'] = $issuesCategory;potresti sostituire il valore collegato alla variabile $args2['cat'], con il valore di un altro ID corrispondente all'altra categoria che vuoi mostrare.
Facendo in questo modo dovresti essere in grado di mostrare le categorie che desideri, prova e facci sapere

-
Con la riga:
$args['cat'] = 1;
vado a prendere la categoria numero 1 che ho tra le mie del sito?Nella seconda parte stessa domanda, come passo ad arg2 il riferimento ad una altra categoria?
grazie mille
PS: domattina faccio la prova e vi faccio sapere...
-
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi