¿Alguien sabe cómo puedo mostrar una lista de publicaciones de WordPress según la categoría actual de las páginas de publicaciones actuales?
Por ejemplo, si actualmente estoy en post333.html y la categoría en la que se encuentra la página es " Bebidas ".
¿Cómo obtendría el resto de las publicaciones en esa categoría para que se muestren en una lista como "temas sugeridos" en post333.html ?
Y si la publicación y la categoría cambian, también lo hará la lista.
El siguiente código debe ir directamente a su archivo de plantilla.
<?php // Get Other posts in same category $suggested_posts = get_posts( array( 'category__in' => wp_get_post_categories( $post->ID ), // Retrieve the list of categories for a post. 'numberposts' => 5, // Number of posts 'post__not_in' => array( $post->ID ) // Exclude current post ) ); // If post found if( $suggested_posts ) { foreach( $suggested_posts as $post ) { setup_postdata($post); ?> <ul> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> <?php the_content('Read the rest of this entry »'); ?> </li> </ul> <?php } } wp_reset_postdata(); ?>Si desea usarlo en cualquier lugar, puede crear un código abreviado como el siguiente.
<?php function yourprefix_suggested_posts( $atts ){ // Get Other posts in same category $suggested_posts = get_posts( array( 'category__in' => wp_get_post_categories( $post->ID ), // Retrieve the list of categories for a post. 'numberposts' => 5, // Number of posts 'post__not_in' => array( $post->ID ) // Exclude current post ) ); // If post found if( $suggested_posts ) { foreach( $suggested_posts as $post ) { setup_postdata($post); ?> <ul> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> <?php the_content('Read the rest of this entry »'); ?> </li> </ul> <?php } } wp_reset_postdata(); } add_shortcode( 'suggested_posts', 'yourprefix_suggested_posts' ); ?> Más tarde, puede usar el código abreviado [suggested_posts] en cualquier lugar.
(No probado, pero debería funcionar).