WP_Query ,exclusion d'une categorie

Par prem, il y a 9 ans


Bonjour
j'utilises un theme 'themeforest' , sur une de mes pages qui est assez complexe (où il y'a pas mal de sections et de widgets) et qui répertorie tous les articles. Je souhaites n'afficher que certains articles, donc faire une requête personnalisé wp_query en excluant une categorie.

Grace au système de hierarchie j'ai reussi à trouver le fichier en question mais je ne sais pas comment m'y prendre , voilà le code concerné je penses.
ps: il fait mention de ag_get_cats_array(3), j'ai cherché dans le codex et dans le fichier fonctions et un peu partout je ne sais pas d'où cet array sort

est ce qu'une WP_Query peut faire l'affaire?

$section_id = get_the_ID($post); // section id $tw_categories = ag_get_cats_array(3); // get all post categories $post_type = get_post_type(); $thumbnail_size = 'half'; $thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size; if ($grid_size == 'related') { $thumbnail_size = 'related'; } ?> <div class="<?php if ( isset($tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item">

19 réponses

Sparkosis, il y a 9 ans

salut, (j'espère que tu as fais un thème enfant),

ag_get_cats_array

Doit surement être une fonction de ton thème,

cependant, si tu souhaite exclure une catégorie, c'est assez simple:

foreach((get_the_category()) as $cat) { if ($cat->cat_name != 'LaCategQueTuVeuxPas') { $tw_categories = $cat; } }

Normalement ça devrait faire le taf

prem, il y a 9 ans

Je t'avoue que je sèches!
je n'arrives pas à personnaliser le code .
Le code de bas est
global
$tw_section, // section object
$grid_size, // grid size
$post; // section object

section_id = get_the_ID(post); // section id
$tw_categories = ag_get_cats_array(3); // get all post categories
$post_type = get_post_type();

thumbnail_size = 'half'; $thumbnail_size = (grid_size == 'fullgrid') ? 'big' : thumbnail_size; if (grid_size == 'related') {
$thumbnail_size = 'related';
} ?>

<div class="<?php if ( isset(tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item"> <div class="isobrick-inner"> <?php the_post_thumbnail(thumbnail_size, array(
'onload' => 'jQuery(this).animate({ "opacity" : 0.75 }, 500);'
));
?>

    <?php echo themewich_review_circle('none', false); ?>

    <?php if ( 'post' == $post_type ) : ?>
        <span class="thumboverdate">
            <i class="fa fa-bookmark"></i>
            <?php themewich_date('human'); ?>
        </span>
    <?php else: ?>
        <?php if ( function_exists( 'wc_get_template' ) ) : ?>
            <span class="thumboverdate">
                <?php wc_get_template( 'loop/price.php' ); ?>
            </span>
        <?php endif; ?>
    <?php endif; ?>

    <div class="thumbovertext">
        <div class="badge">
            <?php echo ag_get_cats(3); ?>
        </div>
        <h2 class="title">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php
                    $title = get_the_title();
                    if (strlen($title) >= 75) {
                         $title = mb_substr($title, 0, 75) . "... ";
                    }
                    echo $title;
                ?>
            </a>
        </h2>

        <?php if ( 'post' == $post_type ) : ?>
            <?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?>
            <a class="button outline" href="<?php the_permalink(); ?>"  title="<?php the_title_attribute(); ?>">
                <?php
                    if ( isset($matches[1]) && $matches[1] ) {
                        echo $matches[1];
                    } else {
                        _e('Read More', 'themewich');
                    }
                ?>
            </a>
        <?php endif; ?>

    </div>

    <a class="brick-thumb-link" href="<?php the_permalink(); ?>"  title="<?php the_title_attribute(); ?>">
        <?php the_title(); ?>
    </a>
</div>

</div>```

//Ps: j'ai testé content-slug.php est cela me retire mes widget il me reste juste cette alternative pour le moment
Sparkosis, il y a 9 ans

Le code que tu m'as envoyé semble correct, est ce que tout le code réprésente une seule page ?

prem, il y a 9 ans

Oui il est correct car je n'y est pas touchée.
Avec le code que tu m'as donné ,j'ai modifiée ce code de base en :

/** * The template for displaying a grid item. * * @package WordPress * @subpackage Edition * @since Edition 1.0 */?> <?php foreach((get_the_category()) as $cat) : ?> <?php if ($cat->cat_name != 'dossier'): ?> <?php global $tw_categories = $cat; // get all post categories $tw_section, // section object $grid_size, // grid size $post; // section object $section_id = get_the_ID($post); // section id $post_type = get_post_type(); $thumbnail_size = 'half'; $thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size; if ($grid_size == 'related') { $thumbnail_size = 'related'; } ?> <div class="<?php if ( isset($tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item"> <div class="isobrick-inner"> <?php the_post_thumbnail($thumbnail_size, array( 'onload' => 'jQuery(this).animate({ "opacity" : 0.75 }, 500);' )); ?> <?php echo themewich_review_circle('none', false); ?> <?php if ( 'post' == $post_type ) : ?> <span class="thumboverdate"> <i class="fa fa-bookmark"></i> <?php themewich_date('human'); ?> </span> <?php else: ?> <?php if ( function_exists( 'wc_get_template' ) ) : ?> <span class="thumboverdate"> <?php wc_get_template( 'loop/price.php' ); ?> </span> <?php endif; ?> <?php endif; ?> <div class="thumbovertext"> <div class="badge"> <?php echo ag_get_cats(3); ?> </div> <h2 class="title"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php $title = get_the_title(); if (strlen($title) >= 75) { $title = mb_substr($title, 0, 75) . "... "; } echo $title; ?> </a> </h2> <?php if ( 'post' == $post_type ) : ?> <?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?> <a class="button outline" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php if ( isset($matches[1]) && $matches[1] ) { echo $matches[1]; } else { _e('Read More', 'themewich'); } ?> </a> <?php endif; ?> </div> <a class="brick-thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </div> </div> <?php endif ?> <?php endforeach; ?>```
prem, il y a 9 ans

Et cela me donne une page blanche. Cela code une section avec une grid et certainement des appels pour des widgets.Je ne vois pas les widgets dans le code(appels) mais sur ma page ils sautent

Sparkosis, il y a 9 ans

Si il t'affiche une page blanche, c'est que ta boucle ne fonctionne pas, peux tu faire un

var_dump(get_the_category()); ?
prem, il y a 9 ans

j'obtiens:
array(0) { }

prem, il y a 9 ans

je me suis trompé de fichier pardon. plutot ca

array(3) { [0]=> object(WP_Term)#17050 (16) { ["term_id"]=> int(32) ["name"]=> string(7) "Dossier" ["slug"]=> string(7) "dossier" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(32) ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(2) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(32) ["category_count"]=> int(2) ["category_description"]=> string(0) "" ["cat_name"]=> string(7) "Dossier" ["category_nicename"]=> string(7) "dossier" ["category_parent"]=> int(0) } [1]=> object(WP_Term)#17146 (16) { ["term_id"]=> int(16) ["name"]=> string(8) "Football" ["slug"]=> string(8) "football" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(16) ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> int(0) ["count"]=> int(3) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(16) ["category_count"]=> int(3) ["category_description"]=> string(0) "" ["cat_name"]=> string(8) "Football" ["category_nicename"]=> string(8) "football" ["category_parent"]=> int(0) } [2]=> object(WP_Term)#17131 (16) { ["term_id"]=> int(26) ["name"]=> string(7) "Mercato" ["slug"]=> string(7) "mercato" ["term_group"]=> int(0) ["term_taxonomy_id"]=> int(26) ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> int(16) ["count"]=> int(2) ["filter"]=> string(3) "raw" ["cat_ID"]=> int(26) ["category_count"]=> int(2) ["category_description"]=> string(0) "" ["cat_name"]=> string(7) "Mercato" ["category_nicename"]=> string(7) "mercato" ["category_parent"]=> int(16) } }

Sparkosis, il y a 9 ans

Hum alors dans ton if en haut c'est pas

$cat->cat_name

mais

$cat->name
prem, il y a 9 ans

Ah oui c'est logique , il me fait une erreur à la ligne de $tw_categories = $cat; .
J'ai mis des ; au lieu de , à
$tw_section, // section object
$grid_size, // grid size

il me fait maintenant une erreur à la ligne de :
$post_type = get_post_type();

Parse error: syntax error, unexpected '=', expecting ',' or ';' in /customers/f/0/5/.fr/httpd.www/wp-content/themes/edition/functions/templates/template-grid.php on line 16
prem, il y a 9 ans

Je ne vois pas d'erreur pourtant

Sparkosis, il y a 9 ans

envoie moi ton code ^^

prem, il y a 9 ans

C'étais celui du dessus ^^

* The template for displaying a grid item. * * @package WordPress * @subpackage Edition * @since Edition 1.0 */?> <?php foreach((get_the_category()) as $cat) : ?> <?php if ($cat->cat_name != 'dossier'): ?> <?php global $tw_categories = $cat; // get all post categories $tw_section; // section object $grid_size; // grid size $post; // section object $section_id = get_the_ID($post); // section id $post_type = get_post_type(); $thumbnail_size = 'half'; $thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size; if ($grid_size == 'related') { $thumbnail_size = 'related'; } ?> <div class="<?php if ( isset($tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item"> <div class="isobrick-inner"> <?php the_post_thumbnail($thumbnail_size, array( 'onload' => 'jQuery(this).animate({ "opacity" : 0.75 }, 500);' )); ?> <?php echo themewich_review_circle('none', false); ?> <?php if ( 'post' == $post_type ) : ?> <span class="thumboverdate"> <i class="fa fa-bookmark"></i> <?php themewich_date('human'); ?> </span> <?php else: ?> <?php if ( function_exists( 'wc_get_template' ) ) : ?> <span class="thumboverdate"> <?php wc_get_template( 'loop/price.php' ); ?> </span> <?php endif; ?> <?php endif; ?> <div class="thumbovertext"> <div class="badge"> <?php echo ag_get_cats(3); ?> </div> <h2 class="title"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php $title = get_the_title(); if (strlen($title) >= 75) { $title = mb_substr($title, 0, 75) . "... "; } echo $title; ?> </a> </h2> <?php if ( 'post' == $post_type ) : ?> <?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?> <a class="button outline" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php if ( isset($matches[1]) && $matches[1] ) { echo $matches[1]; } else { _e('Read More', 'themewich'); } ?> </a> <?php endif; ?> </div> <a class="brick-thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </div> </div> <?php endif ?> <?php endforeach; ?>```
Sparkosis, il y a 9 ans
<?php foreach((get_the_category()) as $cat) : ?> <?php if ($cat->name != 'dossier'): //NOM ?> <?php $tw_categories = $cat; // get all post categories $section_id = get_the_ID($post); // section id $post_type = get_post_type(); $thumbnail_size = 'half'; $thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size; if ($grid_size == 'related') { $thumbnail_size = 'related'; } ?>

ça me semble plus correct, ce global me semble bizarre

prem, il y a 9 ans

C'est toujours vide tu peux me redonner ton email stp?

prem, il y a 9 ans

Je tai envoyé un mail :)

prem, il y a 9 ans

donc comme tu m'as dis que cela ne boucle pas j'ai fais appel à the_loop

/** * The template for displaying a grid item. * * @package WordPress * @subpackage Edition * @since Edition 1.0 */ $query = new WP_Query( array( 'category__not_in' => array( 32 ) ) ); ?> <?php if ( $query->have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <?php global $tw_section, // section object $grid_size, // grid size $post; // section object $section_id = get_the_ID($post); // section id $tw_categories = get_categories(); // get all post categories $post_type = get_post_type(); $thumbnail_size = 'half'; $thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size; if ($grid_size == 'related') { $thumbnail_size = 'related'; } ?> <div class="<?php if ( !isset($tw_categories[2]->term_id) ) { echo 'highlight_category_' . $tw_categories[2]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item"> <div class="isobrick-inner"> <?php the_post_thumbnail($thumbnail_size, array( 'onload' => 'jQuery(this).animate({ "opacity" : 0.75 }, 500);' )); ?> <?php echo themewich_review_circle('none', false); ?> <?php if ( 'post' == $post_type ) : ?> <span class="thumboverdate"> <i class="fa fa-bookmark"></i> <?php themewich_date('human'); ?> </span> <?php else: ?> <?php if ( function_exists( 'wc_get_template' ) ) : ?> <span class="thumboverdate"> <?php wc_get_template( 'loop/price.php' ); ?> </span> <?php endif; ?> <?php endif; ?> <div class="thumbovertext"> <div class="badge"> <?php echo ag_get_cats(3); ?> </div> <h2 class="title"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php $title = get_the_title(); if (strlen($title) >= 75) { $title = mb_substr($title, 0, 75) . "... "; } echo $title; ?> </a> </h2> <?php if ( 'post' == $post_type ) : ?> <?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?> <a class="button outline" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php if ( isset($matches[1]) && $matches[1] ) { echo $matches[1]; } else { _e('Read More', 'themewich'); } ?> </a> <?php endif; ?> </div> <a class="brick-thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </div> </div> <?php endwhile; ?> <?php endif; ?>```
Sparkosis, il y a 9 ans

et donc là ça te fais une boucle infinie car tu as oublié quelque chose, je t'envoie ton code corrigé, je pense que l'on touche au but et que c'est la dernière chose.

<?php /** * The template for displaying a grid item. * * @package WordPress * @subpackage Edition * @since Edition 1.0 */ $query = new WP_Query( array( 'category__not_in' => array( 32 ) ) ); ?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> //Changement ici <?php global $tw_section, // section object $grid_size, // grid size $post; // section object $section_id = get_the_ID($post); // section id $tw_categories = get_categories(); // get all post categories $post_type = get_post_type(); $thumbnail_size = 'half'; $thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size; if ($grid_size == 'related') { $thumbnail_size = 'related'; } ?> <div class="<?php if ( !isset($tw_categories[2]->term_id) ) { echo 'highlight_category_' . $tw_categories[2]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item"> <div class="isobrick-inner"> <?php the_post_thumbnail($thumbnail_size, array( 'onload' => 'jQuery(this).animate({ "opacity" : 0.75 }, 500);' )); ?> <?php echo themewich_review_circle('none', false); ?> <?php if ( 'post' == $post_type ) : ?> <span class="thumboverdate"> <i class="fa fa-bookmark"></i> <?php themewich_date('human'); ?> </span> <?php else: ?> <?php if ( function_exists( 'wc_get_template' ) ) : ?> <span class="thumboverdate"> <?php wc_get_template( 'loop/price.php' ); ?> </span> <?php endif; ?> <?php endif; ?> <div class="thumbovertext"> <div class="badge"> <?php echo ag_get_cats(3); ?> </div> <h2 class="title"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php $title = get_the_title(); if (strlen($title) >= 75) { $title = mb_substr($title, 0, 75) . "... "; } echo $title; ?> </a> </h2> <?php if ( 'post' == $post_type ) : ?> <?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?> <a class="button outline" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php if ( isset($matches[1]) && $matches[1] ) { echo $matches[1]; } else { _e('Read More', 'themewich'); } ?> </a> <?php endif; ?> </div> <a class="brick-thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </div> </div> <?php endwhile; ?> <?php endif; ?>

Essaie de cette manière et tiens moi au courant par mail :) je ne suis pas sur le pc aujourd'hui

prem, il y a 9 ans

nickel cela marche bien, après mes modif de réglages section au lieu de show posts -> categories en All.
Comme je te disais en mail les articles sont répété X fois alors j'ai utilisée wp_reset_postdata(); et wp_reset_query() . Et maintenant ils se répétent 2 fois . Je dois les mettre au mauvais endroit . Voilà mon code

<?php /** * The template for displaying a grid item. * * @package WordPress * @subpackage Edition * @since Edition 1.0 */ wp_reset_postdata(); $query = new WP_Query( array( 'category__not_in' => array( 32 ) ) ); ?> <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <?php global $tw_section, // section object $grid_size, // grid size $post; // section object $section_id = get_the_ID($post); // section id $tw_categories = get_categories(); // get all post categories $post_type = get_post_type(); $thumbnail_size = 'half'; $thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size; if ($grid_size == 'related') { $thumbnail_size = 'related'; } ?> <div class="<?php if ( isset($tw_categories[2]->term_id) ) { echo 'highlight_category_' . $tw_categories[2]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item"> <div class="isobrick-inner"> <?php the_post_thumbnail($thumbnail_size, array( 'onload' => 'jQuery(this).animate({ "opacity" : 0.75 }, 500);' )); ?> <?php echo themewich_review_circle('none', false); ?> <?php if ( 'post' == $post_type ) : ?> <span class="thumboverdate"> <i class="fa fa-bookmark"></i> <?php themewich_date('human'); ?> </span> <?php else: ?> <?php if ( function_exists( 'wc_get_template' ) ) : ?> <span class="thumboverdate"> <?php wc_get_template( 'loop/price.php' ); ?> </span> <?php endif; ?> <?php endif; ?> <div class="thumbovertext"> <div class="badge"> <?php echo ag_get_cats(3); ?> </div> <h2 class="title"> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php $title = get_the_title(); if (strlen($title) >= 75) { $title = mb_substr($title, 0, 75) . "... "; } echo $title; ?> </a> </h2> <?php if ( 'post' == $post_type ) : ?> <?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?> <a class="button outline" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php if ( isset($matches[1]) && $matches[1] ) { echo $matches[1]; } else { _e('Read More', 'themewich'); } ?> </a> <?php endif; ?> </div> <a class="brick-thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </div> </div> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query();?>