Anzeigen von Inhalten aus einer bestimmten Kategorie
-
-
Genau das,wasnichtfunktioniert?Bitte auchnicht uns "query_posts"._Exactly_ what doesn't work? Also, please do not us `query_posts`.
- 0
- 2014-05-21
- s_ha_dum
-
Hallo,anstatt Beiträge auseinerbestimmten Kategorie anzuzeigen,werden Beiträge aus allen Kategorien angezeigtHi, instead of displaying posts from a certain category it displays posts from all categorys
- 0
- 2014-05-21
- user3615681
-
Ist Ihre News-Seite Ihre Posts-Seitein Ihren Leseeinstellungenfestgelegt?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 Antworten
- Stimmen
-
- 2014-05-21
Das Argumentistnicht
category
,sonderncat
. Ihre Abfrage schlägtfehl,weil Sieein Argument verwenden,dasnicht vorhandenist.$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 } }
Beachten Sie,dassich Ihre
query_posts()
in einneuesWP_Query
-Objekt konvertiert habe. Verwenden Sieniemalsquery_posts()
. Sogar der Codex sagtes so.Hinweis: Diese Funktionistnichtfür Plugins oder Themesgedacht. Wie Spätererklärt,gibt esbessere,leistungsfähigere Optionen zum Ändern die Hauptabfrage. query_posts ()ist zu simpel undproblematisch Ändern der Hauptabfrageeiner Seite durch Ersetzen durcheine neue Instanz von die Abfrage. Esistineffizient (führt SQL-Abfragenerneut aus) und wird sofort ausgeführt scheitern unterbestimmten Umständen (besonders oftbeim Umgangmit Posts Seitennummerierung). Jedermoderne WP-Code sollte zuverlässigere Methoden verwenden,wie z Verwenden Sie dazu den Hakenpre_get_posts.
Beachten Sie auch,dassich unnötige PHP-Tags zum Öffnen und Schließenentfernt und den Code zurbesseren Lesbarkeitformatiert habe. Diese alternative Syntax der Kontrollstruktur isteine Formelfür Fehler.nachmeiner Erfahrung.
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
Ich würde das lieberpersönlichmachen.
Anstelle von:
'category' => '3',
Ersetzen Siees durch:
'category_name' => 'my-category-slug'
Suchen Sie offensichtlich den Namen Ihrer Kategorie-Schnecke undersetzen Sie "Meine-Kategorie-Schnecke".
Wie von @ s-ha-dumerwähnt,istesbesser,diese Methodenicht zu verwenden und stattdessen die WP_Query-Methode zu verwenden.Sie könnenesim WordPress-Codex hier sehen: http://codex.wordpress.org/Class_Reference/WP_Query#Parameter
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
Holen Sie sich dieerstenfünf Beiträgemit einerbestimmten Kategorie
<?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
Fügen Sie diesen Codein Ihre Funktionsdateiein:
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' );
Ändern Sie dasbedingte Tagis_home () so,dassesbei Bedarfmit Ihrer News- oder Postseitenschleife übereinstimmt.Was auchimmer Sie unter Einstellungen> Leseneinstellen.
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
Sie sollten
ändern'category' => '3',
bisin Ihrem Code.
'cat' => '3',
You should change
'category' => '3',
to in your code.
'cat' => '3',
Ich versuche,meine Nachrichtenseite so zugestalten,dassnur Inhalte auseiner Kategorie (Nummer 3) angezeigt werden,aberich kannnicht scheinen,dassesfunktioniert. Anstattnur Beiträge aus Kategorie 3 anzuzeigen,werden Beiträge aus allen Kategorien angezeigt.
Hieristmein Code: