简体   繁体   中英

WordPress wp_query featured posts

I'm working on a WordPress site, and I'm working on a requested feature from my client. The client would like to be able to feature certain posts, so that they appear at the top of a list. To make it easy for my client, I created a featured category. So when the client wants to feature a post, all they have to do is to add the featured category to the post.

Here is my current query to display all posts with the category name of events :

$query = new WP_Query( array( 'category_name' => array('events'), 'posts_per_page' => '1' ) );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            $postLink = get_permalink();
            echo '<li class="wp-post">
                      <h5><a href="'.$postLink.'">'.get_the_title().'</a></h5>
                      <p>'.get_the_excerpt().'</p>
                      <div class="wp-post-details">
                          <span class="post-date">'.get_the_date().'</span>
                      </div>
                </li>';
        }
        echo '<li><a href="/categories/events/" class="btn">View All Events</a></li>';
        wp_reset_postdata();
    } else {
        echo '<li>No Posts Found</li>';
    }

I would like to change it, so it would display events posts that have the additional category of featured first. I have done some searching on Google and here. But as of yet, I have not found a solution that works for my instance.

I found the an solution to my issue. I just had to add if (in_category('feature')) {... before I echo the HTML.

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