将GET_CATERIONS限制为每个类别显示一次

时间:2011-01-22 作者:zac

我正在尝试创建一个主页,其中是一个类别列表,每个类别都有一个关联的缩略图。我希望它能返回每个类别一次,并返回最新帖子的相关缩略图,或者更好的是,它不需要我为每个帖子添加特色图像,而是使用post\\u mime\\u type从帖子的图像中获取缩略图?我还有一段路要走,因为它会返回该类别中每个帖子的类别,然后将图像与每个类别关联。

以下是我正在使用的函数:

    function home_filter() {
        if ( is_home() )
        {
        add_filter( \'the_content\', \'home_cats\' );
        }
    }
    add_action(\'template_redirect\', \'home_filter\');
    function home_cats(){
        $args=array(
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
        );
        $categories=get_categories($args);
           foreach($categories as $category) { 
   echo \'<li><a href="\' . get_category_link( $category->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $category->name ) . \'" \' . \'>\' . get_the_post_thumbnail($page->ID, \'thumbnail\') . $category->name.\'</a></li> \';
        } 
}
主页的内容很简单:

<ul>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content();?>
<?php endwhile; ?>
</ul>
输出类似于:

<ul>
<li><a href="../?cat=1" title="View all posts in Cat A" ><img src="..image-A.jpg"  />Cat A</a></li> 
<li><a href="../?cat=3" title="View all posts in Cat B" ><img src="..image-A.jpg"  />Cat B</a></li> 
<li><a href="../?cat=1" title="View all posts in Cat A" ><img src="..image-B.jpg"  />Cat A</a></li> 
<li><a href="../?cat=3" title="View all posts in Cat B" ><img src="..image-B.jpg"  />Cat B</a></li>
<li><a href="../?cat=1" title="View all posts in Cat A" ><img src="..image-C.jpg"  />Cat A</a></li> 
<li><a href="../?cat=3" title="View all posts in Cat B" ><img src="..image-C.jpg"  />Cat B</a></li> 
 </ul>
有人能帮我理解如何只返回每个类别一次,并包括该类别中最新帖子的拇指吗?甚至更好的是,任何人都能想出一种方法来实现这一点,而不必每次都设置特征图像?

2 个回复
最合适的回答,由SO网友:MikeSchinkel 整理而成

通常,我喜欢回避推荐直接SQL,但在您的使用案例中,我认为这是有保证的。

我将其编码为直接SQL查询;你可能会注意到这有点复杂。您可以将以下代码复制到主题的functions.php 文件或将其添加到.php 您可能正在编写的插件的文件:

function home_cats($max_image_height=150){
  $categories= get_taxonomy_latest_posts_with_attachment(\'category\');
  echo \'<h1>Categories</h1>\';
  foreach($categories as $category) {
    $category_link = get_category_link( $category->term_id );
    $category_title = sprintf( __( "View all posts with category: \'%s\'" ),$category->term_name );
    $post_title = get_the_title($category->post_id);
    $post_link = get_permalink($category->post_id);
    $img_html = wp_get_attachment_image( $category->attachment_id, array( \'thumbnail\',$max_image_height ) );
    $html = <<<HTML
<div id="category-{$category->term_slug}" class="category">
  <span style="float:right;">{$img_html}</span>
  <span  style="float:left;">
    <h2><a href="{$category_link}" title="{$category_title}">{$category->term_name}</a></h2>
    <p style="float:left;">Latest post: <a href="{$post_link}" title="{$post_title}">$post_title</a></p>
  </span>
</div>
<br clear="both" />
HTML;
    echo $html;
  }
}
function get_taxonomy_latest_posts_with_attachment($taxonomy) {
    global $wpdb;
    $sql =<<<SQL
SELECT
  categorized_posts.rownum,
  categorized_posts.term_id,
  categorized_posts.term_name,
  categorized_posts.term_slug,
  categorized_posts.post_id,
  categorized_posts.post_date,
  categorized_posts.post_title,
  attachments.ID AS attachment_id,
  attachments.post_title AS attachment_title,
  attachments.post_mime_type AS attachment_mime_type,
  attachments.guid AS attachment_guid
FROM
  (
  SELECT
    rownum,
    term_id,
    term_name,
    term_slug,
    post_id,
    post_date,
    post_title,
    post_parent,
    post_name,
    post_type
  FROM (
    SELECT
      IF( @prev <> {$wpdb->terms}.term_id, @rownum := 1, @rownum := @rownum+1 ) AS rownum,
      @prev := {$wpdb->terms}.term_id,
      {$wpdb->terms}.term_id,
      {$wpdb->terms}.name AS term_name,
      {$wpdb->terms}.slug AS term_slug,
      {$wpdb->posts}.ID as post_id,
      {$wpdb->posts}.post_date,
      {$wpdb->posts}.post_title,
      {$wpdb->posts}.post_parent,
      {$wpdb->posts}.post_name,
      {$wpdb->posts}.post_type
    FROM
      {$wpdb->term_taxonomy}
      INNER JOIN {$wpdb->terms} ON {$wpdb->term_taxonomy}.term_id={$wpdb->terms}.term_id
      INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_relationships}.term_taxonomy_id={$wpdb->term_taxonomy}.term_taxonomy_id
      INNER JOIN {$wpdb->posts} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
      INNER JOIN (SELECT @rownum := NULL, @prev := 0) AS rownum_initializer ON 1=1
    WHERE 1=1
      AND {$wpdb->posts}.post_type=\'post\'
      AND {$wpdb->posts}.post_status=\'publish\'
      AND {$wpdb->term_taxonomy}.taxonomy=\'%s\'
    ORDER BY {$wpdb->posts}.post_parent DESC, {$wpdb->posts}.post_date DESC
    ) x
  ) categorized_posts
    INNER JOIN (SELECT MAX(ID) AS post_id,post_parent FROM {$wpdb->posts} WHERE post_type=\'attachment\' GROUP BY post_parent) attachment_join ON attachment_join.post_parent=categorized_posts.post_id
    INNER JOIN {$wpdb->posts} attachments ON attachments.ID=attachment_join.post_id
WHERE
  categorized_posts.rownum=1
GROUP BY
  categorized_posts.term_id
ORDER BY
  categorized_posts.term_name
SQL;
  return $wpdb->get_results($wpdb->prepare($sql,$taxonomy));
}
您会注意到,我将逻辑分隔开来,这样您就可以通过调用get_taxonomy_latest_posts_with_attachment() 函数并向其传递分类标识符,如下所示:

$post_tags = get_taxonomy_latest_posts_with_attachment(\'post_tags\');
由于该函数中SQL的复杂性,我不打算解释它(或者我会整晚都在这里),但如果您有具体的后续问题,请提问。总之,下面是我的测试端代码与测试数据的关系:

Screenshot of a WordPress site with the list of Categories and the Latest Post for Each
(来源:mikeschinkel.com)

P、 照片中的人都是我的朋友,都以这样或那样的方式与WordPress合作。希望他们不介意我使用他们的肖像。:)

SO网友:Rarst

那个钩子装置有点乱,为什么不打个电话呢home_cats() 在模板中?

尝试以下操作(请注意,这是按类别查询,可能会影响性能):

function home_cats() {

    $args = array(
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
    );
    $categories = get_categories($args);

    echo \'<ul>\';

    foreach ( $categories as $category ) {

        $link = get_category_link($category->term_id);
        $title = sprintf(__("View all posts in %s"), $category->name);

        $posts = get_posts( array(
            \'cat\' => $category->term_id,
            \'numberposts\' => 1,
        ) );

        $post = $posts[0];

        $thumbnail =  get_the_post_thumbnail($post->ID);
        echo "<li><a href=\'{$link}\' title=\'{$title}\'>{$thumbnail}{$category->name}</a></li>";
    }

    echo \'</ul>\';
}
我通常会选择不同的方法来挖掘图像Get The Image.

结束

相关推荐

Paging in a sidebar mini loop

我切换到了另一个主题,并决定用其中的一些默认代码制作一个小部件,在自定义循环中显示我的美味帖子、推特帖子、su帖子和youtube视频(不包括主循环中的这些类别)。但是现在。。。分页不再工作。我制作了这个小部件:// =============================== EDL Sidebar Posts Widget ====================================== class SidebarPosts extends WP_Widget { &#x