Ich habe diesen Code für die Schleife und muss eine Kategorie 4
aus dieser Schleife ausschließen. Irgendwelche Vorschläge, wie dies erreicht werden kann?
Code, der die Schleife startet
<?php if(have_posts()): ?>
<ol class="item_lists">
<?php
$end = array(3,6,9,12,15,18,21,24,27,30,33,36,39,42,45);
$i = 0;
while (have_posts()) : the_post();
$i++;
global $post;
?>
sie können wp_parse_args () verwenden, um Ihre Argumente in der Standardabfrage zusammenzuführen
// Define the default query args
global $wp_query;
$defaults = $wp_query->query_vars;
// Your custom args
$args = array('cat'=>-4);
// merge the default with your custom args
$args = wp_parse_args( $args, $defaults );
// query posts based on merged arguments
query_posts($args);
ich denke jedoch, dass die elegantere Route die Aktion pre_get_posts () verwendet. Dadurch wird die Abfrage vor so geändert, dass die Abfrage nicht zweimal ausgeführt wird.
auschecken:
http://codex.wordpress.org/Custom_Queries#Category_Exclusion
basierend auf diesem Beispiel, um Kategorie 4 aus dem Index auszuschließen, würde ich dies in Ihre functions.php einfügen:
add_action('pre_get_posts', 'wpa_44672' );
function wpa_44672( $wp_query ) {
//$wp_query is passed by reference. we don't need to return anything. whatever changes made inside this function will automatically effect the global variable
$excluded = array(4); //made it an array in case you need to exclude more than one
// only exclude on the home page
if( is_home() ) {
set_query_var('category__not_in', $excluded);
//which is merely the more elegant way to write:
//$wp_query->set('category__not_in', $excluded);
}
}
Aus Ihrer Funktionsdatei
function remove_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-4' );
}
}
add_action( 'pre_get_posts', 'remove_home_category' );
Dieser Code ändert die Abfrage, bevor die eigentliche Abfrage ausgeführt wird. Dies ist in diesem Fall der effizienteste Hook zum Ändern der Schleife.
Adam hat recht. Damit die Paginierung funktioniert, muss außerdem Folgendes vorhanden sein:
<?php query_posts('post_type=post&paged='.$paged.'&cat=-4'); ?>
Vor der Linie
<?php if(have_posts()): ?>
Fügen Sie so etwas ein
<?php query_posts($query_string . '&cat=-4'); ?>
Dies schließt die Kategorie mit Kategorie-ID 4 aus. Wie gesehen hier