Visualizza i contenuti di una categoria specifica
-
-
_Esattamente_ cosanonfunziona?Inoltre,non usare "query_posts"._Exactly_ what doesn't work? Also, please do not us `query_posts`.
- 0
- 2014-05-21
- s_ha_dum
-
Ciao,invece di visualizzarei post di una determinata categoria,mostrai post ditutte le categorieHi, instead of displaying posts from a certain category it displays posts from all categorys
- 0
- 2014-05-21
- user3615681
-
Latuapagina dellenotizie è latuapagina deipostimpostatanelle Impostazioni di lettura?Is your news page your posts page set in your Reading Settings?
- 0
- 2014-05-21
- Brad Dalton
-
https://wordpress.stackexchange.com/a/290909/133699https://wordpress.stackexchange.com/a/290909/133699
- 0
- 2018-03-28
- Super Model
-
5 risposta
- voti
-
- 2014-05-21
L'argomentonon è
category
,ècat
. Latua querynon riesceperché stai utilizzando un argomento chenonesiste.$args = array( 'post_type' => 'post' , 'orderby' => 'date' , 'order' => 'DESC' , 'posts_per_page' => 6, 'cat' => '3', 'paged' => get_query_var('paged'), 'post_parent' => $parent ); $q = new WP_Query($args); if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); // your loop } }
Notare che ho convertitoiltuo
query_posts()
in unnuovo oggettoWP_Query
. Non utilizzaremaiquery_posts()
. Ancheil Codex lo afferma.Nota: questafunzionenon èpensataperessere utilizzata daplugin otemi. Come spiegatopiù avanti,ci sono opzionimigliorie piùperformanti damodificare la domandaprincipale. query_posts () è unmodoeccessivamente semplicisticoe problematico permodificare la queryprincipale di unapagina sostituendola con unanuovaistanza di la query. Èinefficiente (eseguenuovamente le query SQL)e lofarà atitolo definitivo fallirein alcune circostanze (soprattutto quando sitratta dipost impaginazione). Qualsiasi codice WPmoderno dovrebbe utilizzaremetodipiù affidabili,come facendo uso dell'hookpre_get_posts,per questo scopo.
Nota anche che ho rimossoi tag di aperturae chiusura PHPnonnecessarie hoformattatoil codiceper unamigliore leggibilità. Quella sintassi della struttura di controllo alternativa è unaformulaperilfallimento,nellamiaesperienza.
The argument isn't
category
, it iscat
. Your query fails because you are using an argument that doesn't exist.$args = array( 'post_type' => 'post' , 'orderby' => 'date' , 'order' => 'DESC' , 'posts_per_page' => 6, 'cat' => '3', 'paged' => get_query_var('paged'), 'post_parent' => $parent ); $q = new WP_Query($args); if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); // your loop } }
Notice that I have converted your
query_posts()
into a newWP_Query
object. Do not usequery_posts()
, ever. Even the Codex states so.Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.
Also note that I removed unnecessary PHP opening and closing tags and formatted the code for better readability. That alternative control structure syntax is a formula for failure, in my experience.
-
- 2014-05-21
Personalmente lofareipiuttosto.
Invece di:
'category' => '3',
Sostituiscilo con questo:
'category_name' => 'my-category-slug'
Ovviamentetrovailnome dellatua categoria sluge sostituisci "my-category-slug".
Comemenzionato da @ s-ha-dum,sarebbemeglionon usare questometodoe piuttosto usareilmetodo WP_Query.Puoi vederlonel codice WordPress qui: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
I would personally do this rather.
Instead of:
'category' => '3',
Replace it with this:
'category_name' => 'my-category-slug'
Obviously find the name of your category slug and replace 'my-category-slug'.
As mentioned by @s-ha-dum it would be better to not use this method and rather use WP_Query method. You can see it in the WordPress Codex here: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
-
- 2018-01-12
Ottienii primi cinquepost con una categoria specifica
<?php // the query $the_query = new WP_Query(array( 'category_name' => 'post_category_name', 'post_status' => 'publish', 'posts_per_page' => 5, )); ?> <?php if ($the_query->have_posts()) : ?> <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> <?php the_category(); ?> <?php the_title(); ?> <?php the_excerpt(); ?> <?php the_post_thumbnail(); ?> <?php the_content(); ?> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> <p><?php __('No News'); ?></p> <?php endif; ?>
Get First Five Posts With Specific Category
<?php // the query $the_query = new WP_Query(array( 'category_name' => 'post_category_name', 'post_status' => 'publish', 'posts_per_page' => 5, )); ?> <?php if ($the_query->have_posts()) : ?> <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> <?php the_category(); ?> <?php the_title(); ?> <?php the_excerpt(); ?> <?php the_post_thumbnail(); ?> <?php the_content(); ?> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> <p><?php __('No News'); ?></p> <?php endif; ?>
-
- 2014-05-21
Aggiungi questo codiceneltuofile difunzioni:
function wpsites_display_one_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '3' ); } } add_action( 'pre_get_posts', 'wpsites_display_one_category' );
Modificail tag condizionale is_home ()in modo che corrisponda allapagina dellenotizie o al loop dellapagina deipost,senecessario.Qualunque cosaimpostiin Impostazioni> Lettura.
Add this code in your functions file:
function wpsites_display_one_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '3' ); } } add_action( 'pre_get_posts', 'wpsites_display_one_category' );
Change the is_home() conditional tag to match your news page or posts page loop if needed. Whatever you set in Settings > Reading.
-
- 2018-11-13
Dovresti cambiare
'category' => '3',
anel codice.
'cat' => '3',
You should change
'category' => '3',
to in your code.
'cat' => '3',
Sto cercando difarein modo che lamiapagina dellenotizie visualizzii contenuti di una sola categoria (numero 3)manon riesco afarlofunzionare. Invece di visualizzare soloi post della categoria 3,mostrai post ditutte le categorie.
Eccoilmio codice: