Mostra tutti i post nella categoria corrente

27 ott 2012, 09:39:27
Visualizzazioni: 15.2K
Voti: 1

Sto cercando di creare un comportamento come mostrato nel seguente link:

http://www.javaexperience.com/java-role-of-serialversionuid-in-serialization/

Qui vengono visualizzati tutti i post della stessa categoria. Attualmente è codice HTML scritto manualmente, voglio replicare questo comportamento utilizzando codice PHP nel mio single.php.

Elenco di post correlati per categoria

Ecco il codice che ho scritto finora

<?php
$category = get_the_category(); // Ottiene la categoria del post corrente
   <ul>

query_posts('cat='.$category); // Query per i post della categoria
if ( have_posts() ) : while ( have_posts() ) : the_post();
   <li><a href="get_permalink( $id );">the_title();</a></li>
endwhile; endif;
   </ul>
<br/>

?>

Qualcuno può aiutarmi a farlo funzionare?

0
Tutte le risposte alla domanda 2
1

Prova questo:

$cat = get_query_var('cat');
$PozCat = get_category ($cat);
$PozCat->id // ci restituisce l'ID della categoria corrente.

Poi usa questo hook nella tua query:

<ul>
   <?php
    $cat = get_query_var('cat');
    $PozCat = get_category ($cat);
    //$PozCat->id
    query_posts('posts_per_page=-1&cat='.$PozCat->id);
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>

    <?php endwhile; endif; ?>
</ul>
27 ott 2012 11:48:55
Commenti

funziona benissimo, tranne per il refuso in the_permalink()

Grant Grant
14 dic 2018 07:27:53
0
-2

Puoi farlo utilizzando Wp_query() passando il nome della categoria come argomento:

<?php $my_query = new WP_Query('category_name=mycategory&showposts=-1'); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">

<?php the_title(); ?></a>

<?php endwhile; ?>
27 ott 2012 11:36:27