我如何才能在主页上只显示来自父类别的粘性帖子?

时间:2013-04-24 作者:Jayx

在我的主页上,我有三列显示特定类别中帖子的不同信息量(前两列是子类别,第三列显示不同子类别的帖子,但只有一个父类别)。

我想通过使帖子具有粘性来确定在每个帖子中显示哪些帖子(按描述的类别)。

帮助

2 个回复
SO网友:Jayx

好啊代码可能仍然有点站不住脚,但以下是我的想法(仍然没有发布旧的、甚至更糟糕的PHP;抱歉,让投票人失望),以防其他人可能使用它或可以改进它。

<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>
请随时对此进行改进或评论如何优化和/或我的方法的利弊。

SO网友:vancoder

根据要求,这是一个粗略的示例:

<?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>

结束

相关推荐