Come visualizzare i post correlati della stessa categoria?
4 risposta
- voti
-
- 2011-10-02
Unapossibilità:
$related = get_posts( array( 'category__in' => wp_get_post_categories( $post->ID ), 'numberposts' => 5, 'post__not_in' => array( $post->ID ) ) ); if( $related ) { foreach( $related as $post ) { setup_postdata($post); /*whatever you want to output*/ } wp_reset_postdata(); }
Riferimento:
Risposta riscrittain base a
WP_Query()
:$related = new WP_Query( array( 'category__in' => wp_get_post_categories( $post->ID ), 'posts_per_page' => 5, 'post__not_in' => array( $post->ID ) ) ); if( $related->have_posts() ) { while( $related->have_posts() ) { $related->the_post(); /*whatever you want to output*/ } wp_reset_postdata(); }
One possibility:
$related = get_posts( array( 'category__in' => wp_get_post_categories( $post->ID ), 'numberposts' => 5, 'post__not_in' => array( $post->ID ) ) ); if( $related ) { foreach( $related as $post ) { setup_postdata($post); /*whatever you want to output*/ } wp_reset_postdata(); }
Reference:
Answer re-written based on
WP_Query()
:$related = new WP_Query( array( 'category__in' => wp_get_post_categories( $post->ID ), 'posts_per_page' => 5, 'post__not_in' => array( $post->ID ) ) ); if( $related->have_posts() ) { while( $related->have_posts() ) { $related->the_post(); /*whatever you want to output*/ } wp_reset_postdata(); }
-
** Staimolto attento coni nomi delletue variabili! ** Stai usando un "$post"globalepereseguire latua query,quindi ridefinendo "$post"nello stesso contesto all'interno deltuo ciclo "foreach".**Be very careful with your variable names!** You're using a global `$post` to perform your query, then redefining `$post` in the same context within your `foreach` loop.
- 0
- 2011-10-02
- EAMann
-
@EAMann -in genere hai ragione -tuttavia,nellamiaesperienza,usare `setup_postdata ()` con qualcosa di diverso da `$post`nonfornisceilgiusto outputper`the_title () `,peresempio.`wp_reset_postdata ()` dovrebbein teoria occuparsi del ripristino di $post.@EAMann - you are generally right - however in my experience, using `setup_postdata()` with anything other than `$post` does not deliver the right output for `the_title()` for instance. `wp_reset_postdata()` should in theory take care of resetting $post.
- 0
- 2011-10-02
- Michael
-
Questoperché `setup_postdata ()` stessofa riferimento a `$post` comeglobale.Invece di usare `get_posts ()` dovresti [definire un'istanzapersonalizzata di `WP_Query`] (http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts)e usaloper riceverei tuoipost.Configurerài dati delpostperte efarà sì che `the_title ()`e altrifunzioninonelmodoin cui dovrebbero.That's because `setup_postdata()` itself references `$post` as a global. Instead of using `get_posts()` you should [define a custom instance of `WP_Query`](http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts) and use that to get your posts. It will set up the post data for you as well as make `the_title()` et al work the way they're supposed ti.
- 1
- 2011-10-03
- EAMann
-
@EAMann Questo è unpostpiuttosto vecchio,ma sarebbe apprezzato sepotessi scrivere una risposta su comefarlo correttamente con la querypersonalizzatae tutto@EAMann This is a rather old post, but it would be appreciated if you could write an answer on how to do that correctly with the custom query and all
- 0
- 2015-11-25
- GiantCowFilms
-
- 2018-07-02
Ecco un'altra opzionepulitae moltoflessibile:
Metti questo codiceneltuofilefunctions.php
function example_cats_related_post() { $post_id = get_the_ID(); $cat_ids = array(); $categories = get_the_category( $post_id ); if(!empty($categories) && is_wp_error($categories)): foreach ($categories as $category): array_push($cat_ids, $category->term_id); endforeach; endif; $current_post_type = get_post_type($post_id); $query_args = array( 'category__in' => $cat_ids, 'post_type' => $current_post_type, 'post__not_in' => array($post_id), 'posts_per_page' => '3' ); $related_cats_post = new WP_Query( $query_args ); if($related_cats_post->have_posts()): while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php the_content(); ?> </li> </ul> <?php endwhile; // Restore original Post Data wp_reset_postdata(); endif; }
Orapuoi semplicemente chiamare lafunzione ovunqueneltuo sito utilizzando:
<?php example_cats_related_post() ?>
Potresti voler rimuovereglielementi dell'elenco o definirli secondo letuenecessità.
* Modifica:puoi cambiare questo:post_not_inin questopost__not_innellatua query
Here another clean and very flexible option:
Put this code in your functions.php file
function example_cats_related_post() { $post_id = get_the_ID(); $cat_ids = array(); $categories = get_the_category( $post_id ); if(!empty($categories) && is_wp_error($categories)): foreach ($categories as $category): array_push($cat_ids, $category->term_id); endforeach; endif; $current_post_type = get_post_type($post_id); $query_args = array( 'category__in' => $cat_ids, 'post_type' => $current_post_type, 'post__not_in' => array($post_id), 'posts_per_page' => '3' ); $related_cats_post = new WP_Query( $query_args ); if($related_cats_post->have_posts()): while($related_cats_post->have_posts()): $related_cats_post->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> <?php the_content(); ?> </li> </ul> <?php endwhile; // Restore original Post Data wp_reset_postdata(); endif; }
Now you can simply call the function anywhere in your site using:
<?php example_cats_related_post() ?>
You may want to remove the list elements or style them as per your need.
*Edit - you to change this: post_not_in to this post__not_in in your query
-
Mostra solo unpost.Èpossibilemostraretuttii post della stessa categoria su unapagina di categoria?It is showing only one post. Is it possible to show all posts of the same category on a category page?
- 0
- 2019-08-29
- Rahul
-
- 2018-08-20
Questa risposta assicura chei post correlati siano ordinatiin base alnumero ditag corrispondenti.
Adesempio,se un articolo ha 3tage c'è un altro articolo che hagli stessiidentici 3tag,dovrebbe apparire all'inizio dell'elenco. L'ordinamento secondario dovrebbeessereper data dipubblicazionein modo daprivilegiarei contenutipiù recenti.
/** * Seleziona contenuti contag comuni. * Ordinain modo chei contenuti conpiùtag corrispondenti sianoin alto. * Ordinamento secondarioprima sui contenutipiù recenti. * * @param $post_id * @paramint $ limit * @return array */ function related_posts ($post_id,$ limit=5) { global $ wpdb; $ query="SELEZIONA TOP% d x.object_id come ID A PARTIRE DAL ( SELEZIONA TOP 10tr1.object_id,COUNT (tr1.term_taxonomy_id) AS common_tag_count DA {$ wpdb- >term_relationships} AStr1 INNER JOIN {$ wpdb- >term_relationships} AStr2 ONtr1.term_taxonomy_id=tr2.term_taxonomy_id WHEREtr2.object_id=% d GROUP BYtr1.object_id HAVINGtr1.object_id!=% D ORDINA PER COUNT (tr1.term_taxonomy_id) DESC ) X INNER JOIN {$ wpdb- >posts}p ONp.ID=x.object_id ORDER BY common_tag_count DESC,p.post_date DESC; "; $ query=$ wpdb- >prepare ($ query,$ limit,$post_id,$post_id); $ids=$ wpdb- >get_col ($ query); $post=[]; foreach ($ids as $id) { $posts []=get_post ($id); } restituire $post; } La queryinterna qui è selezionareil contenuto coni tagpiù corrispondenti,quindi la queryesterna viene utilizzata soloper applicare l'ordinamento secondarioper data dipubblicazione.
Tienipresente che questa query è scrittaper SQL Server,quindipotrebbeesserenecessario aggiornare alcune sintassi (adesempio TOP vs LIMIT).
This answer makes sure that related posts are ordered by how many tags match.
For example if an article has 3 tags and there is another article that has the exact same 3 tags it should appear at the top of the list. Secondary sorting should be by post date so newer content is favored.
/** * Select content with common tags. * Sort so content with multiple matching tags are at the top. * Secondary sort on most recent content first. * * @param $post_id * @param int $limit * @return array */ function related_posts($post_id, $limit = 5) { global $wpdb; $query = "SELECT TOP %d x.object_id as ID FROM ( SELECT TOP 10 tr1.object_id, COUNT(tr1.term_taxonomy_id) AS common_tag_count FROM {$wpdb->term_relationships} AS tr1 INNER JOIN {$wpdb->term_relationships} AS tr2 ON tr1.term_taxonomy_id = tr2.term_taxonomy_id WHERE tr2.object_id = %d GROUP BY tr1.object_id HAVING tr1.object_id != %d ORDER BY COUNT(tr1.term_taxonomy_id) DESC ) x INNER JOIN {$wpdb->posts} p ON p.ID = x.object_id ORDER BY common_tag_count DESC, p.post_date DESC;"; $query = $wpdb->prepare($query, $limit, $post_id, $post_id); $ids = $wpdb->get_col($query); $posts = []; foreach($ids as $id) { $posts[] = get_post($id); } return $posts; }
The inner query here is to select the content with the most matching tags, and then the outer query is just used to to apply secondary sorting by post date.
Note this query is written for SQL Server so some syntax may need updating (e.g. TOP vs LIMIT).
-
- 2019-12-02
puoi utilizzare questo codiceper ottenerepost correlati dalla stessa categoria
$args = array( 'category__in' => wp_get_post_categories( get_queried_object_id() ), 'posts_per_page' => 5, 'orderby' => 'rand', 'post__not_in' => array( get_queried_object_id() ) ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : ?> <ul class=""> <!-- the loop --> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <h6> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </h6> </li> <?php endwhile; ?> <!-- end of the loop --> </ul> <?php wp_reset_postdata(); ?> <?php endif; ?>
e utilizza questo codiceper ottenerepost correlati dagli stessitag
$tags = wp_get_post_terms( get_queried_object_id(), 'post_tag', ['fields' => 'ids'] ); $args = [ 'post__not_in' => array( get_queried_object_id() ), 'posts_per_page' => 5, 'orderby' => 'rand', 'tax_query' => [ [ 'taxonomy' => 'post_tag', 'terms' => $tags ] ] ]; $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : ?> <ul class=""> <!-- the loop --> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <h6> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </h6> </li> <?php endwhile; ?> <!-- end of the loop --> </ul> <?php wp_reset_postdata(); ?> <?php endif; ?>
you can use this code to get related posts from the same category
$args = array( 'category__in' => wp_get_post_categories( get_queried_object_id() ), 'posts_per_page' => 5, 'orderby' => 'rand', 'post__not_in' => array( get_queried_object_id() ) ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : ?> <ul class=""> <!-- the loop --> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <h6> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </h6> </li> <?php endwhile; ?> <!-- end of the loop --> </ul> <?php wp_reset_postdata(); ?> <?php endif; ?>
and use this code to get related posts from the same tags
$tags = wp_get_post_terms( get_queried_object_id(), 'post_tag', ['fields' => 'ids'] ); $args = [ 'post__not_in' => array( get_queried_object_id() ), 'posts_per_page' => 5, 'orderby' => 'rand', 'tax_query' => [ [ 'taxonomy' => 'post_tag', 'terms' => $tags ] ] ]; $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) : ?> <ul class=""> <!-- the loop --> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <h6> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </h6> </li> <?php endwhile; ?> <!-- end of the loop --> </ul> <?php wp_reset_postdata(); ?> <?php endif; ?>
Èpossibile visualizzarei post correlati della stessa categoria delpost corrente?