最新消息: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)

How can I display only sticky posts from a parent category on the homepage?

matteradmin4PV0评论

On my homepage, I have 3 columns showing varying amounts of information from posts in specific categories (first 2 of them child categories, the third column displays posts from various child categories, but one parent category).

I want to determine which posts to display (by category as described) in each, by making them sticky.

Help?

On my homepage, I have 3 columns showing varying amounts of information from posts in specific categories (first 2 of them child categories, the third column displays posts from various child categories, but one parent category).

I want to determine which posts to display (by category as described) in each, by making them sticky.

Help?

Share Improve this question asked Apr 24, 2013 at 21:02 JayxJayx 1391 silver badge5 bronze badges 3
  • I tried using 3 loops with some measure of success (my PHP skills are crap, so I'll refrain from posting that here to avoid embarrassment), but I'm hitting 2 walls ... 1. I can get a maximum of 5 sticky posts to display across all columns 2. The method I used gets the category ID from category slug and can therefore not be used on parent category level to catch all posts in child categories of that parent. – Jayx Commented Apr 24, 2013 at 21:08
  • Still, would be great to see that code. For 5 posts issue, can try supplying the posts_per_page pagination parameter. – montrealist Commented Apr 24, 2013 at 21:11
  • 2 Your question will fair far better if you post your crappy PHP than if you don't. – s_ha_dum Commented Apr 24, 2013 at 21:19
Add a comment  | 

2 Answers 2

Reset to default 0

OK ... code might still be a bit on the flimsy side, but here's what I came up with (still not posting the old, even crappier, PHP; sorry down-voter ) in case anyone else might have use for it or can improve on it.

<section class="column first">
  <h2>What's New</h2>
  <?php 
    $sticky = get_option( 'sticky_posts' );
    $args = array(
      'category_name' => 'news',
      'post__in'  => $sticky,
    );
    query_posts( $args ); while (have_posts()) : the_post();
  { ?>
  <h3><?php the_title(); ?></h3>
  <?php the_content(); ?>
  <?php } endwhile; wp_reset_query(); ?>
</section>
<section class="column middle">
  <h2>Country Projects</h2>
  <?php 
    $sticky = get_option( 'sticky_posts' );
    $args = array(
      'category_name' => 'projects',
      'post__in'  => $sticky,
    );
    query_posts( $args ); while (have_posts()) : the_post();
  { ?>
  <h3><?php the_title(); ?></h3>
  <?php the_content(); ?>
  <?php } endwhile; wp_reset_query(); ?>
</section>
<section class="column last">
  <h2>Featured Publications</h2>
  <?php 
    $sticky = get_option( 'sticky_posts' );
    $args = array(
      'category_name' => 'publications', //This is the parent category, no need for using the cat ID this way.
      'post__in'  => $sticky,
    );
    query_posts( $args ); while (have_posts()) : the_post();
  { ?>
  <h3><?php the_title(); ?></h3>
  <?php the_content(); ?>
  <?php } endwhile; wp_reset_query(); ?>
</section>

Feel free to improve this or comment on how it can be optimised and/or pros and cons to my approach.

As requested, this is a rough example:

<?php
$args = array(
    'posts_per_page' => -1,
    'category__in' => array( 1, 2, 3 ), // IDs of your categories
    'post__in' => get_option( 'sticky_posts' ),
);
$query = new WP_Query( $args ); // do one query only

while ( $query->have_posts() ) {
    $query->the_post();
    $categories = get_the_category();
    $categorized_posts[$categories[0]->name][] = array( get_the_title(), get_the_content() ); // build a new array
}
?>

<section class="column first">
    <h2>What's New</h2>
    <?php
    if ( isset( $categorized_posts['news'] ) ) {
        foreach ( $categorized_posts['news'] as $post_stuff ) {
            echo '<h3>' . $post_stuff[0] . '</h3>';
            echo $post_stuff[1];
        }
    }
    ?>
</section>

<section class="column middle">
    <h2>Country Projects</h2>
    <?php
    if ( isset( $categorized_posts['projects'] ) ) {
        foreach ( $categorized_posts['projects'] as $post_stuff ) {
            echo '<h3>' . $post_stuff[0] . '</h3>';
            echo $post_stuff[1];
        }
    }
    ?>
</section>

<section class="column last">
    <h2>Featured Publications</h2>
    <?php
    if ( isset( $categorized_posts['publications'] ) ) {
        foreach ( $categorized_posts['publications'] as $post_stuff ) {
            echo '<h3>' . $post_stuff[0] . '</h3>';
            echo $post_stuff[1];
        }
    }
    ?>
</section>
Post a comment

comment list (0)

  1. No comments so far