最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论