最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Get sticky post from category?

matteradmin8PV0评论

I have parent category is Game cat_id=42. Child categories are : acrade, causual,.... I want show a top game module in mainpage template, it will show 10 sticky post from game category.

This my code :

<?php 
   $args = array(
      'cat' => 42,
      'posts_per_page' => 10,
      'post__in'  => get_option( 'sticky_posts' ),
   );
   query_posts( $args );
?>

I have parent category is Game cat_id=42. Child categories are : acrade, causual,.... I want show a top game module in mainpage template, it will show 10 sticky post from game category.

This my code :

<?php 
   $args = array(
      'cat' => 42,
      'posts_per_page' => 10,
      'post__in'  => get_option( 'sticky_posts' ),
   );
   query_posts( $args );
?>
Share Improve this question edited Dec 13, 2013 at 10:57 Rahil Wazir 1,03116 silver badges26 bronze badges asked Dec 13, 2013 at 9:49 user43862user43862 335 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Use the below code in the mainpage template to show the sticky posts.

<?php 
$args = array(
    'cat'            => 42,
    'posts_per_page' => 10,
    'post__in'       => get_option( 'sticky_posts' ),
);
// The Query
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
}

The above will print the title of the sticky posts

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far