简体   繁体   中英

How to display only the posts of a category?

I'm in charge of the development and the improvement of the website of my company which uses Wordpress and I search to display only some of the job adverts. I don't know if I'm doing well but I create a category in the back office and I get the category of each post like this:

$category = wp_get_post_terms( 'metiers' );
    if ($my_query->have_posts()) {
    while ($my_query->have_posts() && $category == 'Ingénieur DevOps') : $my_query->the_post(); ?>

But nothing is displayed. How can I correct my code to display only my posts of my "Ingénieur DevOps" category?

This Will Work

   <?php
    $featuredPosts = new WP_Query();
    $featuredPosts->query('showposts=5&cat=3');
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
    <h1><a href="<?php the_permalink()
    <div class="meta">
    By <?php the_author() ?>
    </div>
    <div class="storycontent">
    <?php the_excerpt(); ?>
    </div>
    <?php endwhile; ?>
<?php
$Posts = new WP_Query();
$Posts->query('cat=3');
while ($Posts->have_posts()) : $Posts->the_post(); ?>

the_title();

endwhile; ?>

Remember to echo the variable. You can also just echo the $category in the while loop.

$category = wp_get_post_terms('metiers');
            if ($my_query->have_posts()) {
                while ($my_query->have_posts() && $category == 'Ingénieur DevOps') : echo $my_query->the_post(); ?>
$catQuery = new WP_Query( 'cat=72&posts_per_page=5' ); // put category id instead of name. Category name can change.
while($catQuery->have_posts()) : echo $catQuery->the_post();
endwhile;
wp_reset_postdata();

Ok I think I made a mistake. I want to classify my posts by categories.

my categories selected in my post

my posts and catégories selected

my categories

So I want to display only the posts which are in 'metiers' -> 'Ingénieur DevOps' per exemple. I don't know if 'metier' is called a category but it looks like and acts like a category. Maybe you can enlighten me on it?

Check here to get yout category posts. https://codex.wordpress.org/Template_Tags/get_posts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM