带有图像的自定义类别查看器,回退到类别图像中的最新帖子

时间:2018-11-07 作者:MikeiLL

基于thisthis 答:我正在开发Walker\\u类别的扩展,该类别显示在ACF字段的图像中,返回到最新帖子的缩略图。

我在下面列出了整个步行者,但我认为问题在于$argsWP_Query 和/或我在结果中循环的方式。

我每次都得到相同的图像,尽管tax_query 不同于:

$args = array(
  \'post_type\' => \'project\',
  \'tax_query\' => [
      \'taxonomy\' => \'project-category\',
      \'terms\' => $category->term_id,
  ],
  \'posts_per_page\' => 1,
  \'orderby\' => \'date\',
  \'order\' => \'ASC\'
);

$query = new WP_Query($args);

if ( $query->have_posts() ) {

  while ( $query->have_posts() ) {

    $query->the_post();

    global $post;

    $image = get_the_post_thumbnail_url( $post->ID, \'medium\', null );

  }

wp_reset_postdata();
据我所知WP_Query\' for each iteration (in which the ACF field is empty), but$image`每次都以相同的url结尾。

这些参数是这样的:

Array
(
    [post_type] => project
    [tax_query] => Array
        (
            [taxonomy] => project-category
            [terms] => 43
        )

    [posts_per_page] => 1
    [orderby] => date
    [order] => ASC
)
Array
(
    [post_type] => project
    [tax_query] => Array
        (
            [taxonomy] => project-category
            [terms] => 48
        )

    [posts_per_page] => 1
    [orderby] => date
    [order] => ASC
)
#etc...
更改$args 似乎每次都会返回不同的图像url。不应该\'posts_per_page\' => 1 每个查询只返回一篇帖子?

我错过了什么?

将整个Walker发布为gist.

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

问题的解决方案是tax_query 需要是数组的数组。感谢Sally CJ的上述评论,这也提醒我$query->request.

以下是total Walker,适用于任何对其有用的人:

class List_Category_Walker extends Walker_Category {
    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {

        $cat_name = apply_filters(
            \'list_cats\',
            esc_attr( $category->name ),
            $category
        );

        $link = \'<a href="\' . esc_url( get_term_link( $category ) ) . \'" \';
        $link .= \'>\';

        // Get array of images from ACF Image field
        // Get array of images from ACF Image field
        if function_exists(\'get_field\'):
          $image_array = get_field(\'main_category_image\', \'term_\' . $category->term_id);
        else:
          $image_array = [];
        endif;

        // But if that\'s not set set it to image from latest post within category
        if (empty($image_array[\'sizes\'][\'medium\'])):
          // Get one post
          $args = array(
            \'post_type\' => \'project\',
            \'tax_query\' => array([
                \'taxonomy\' => \'project-category\',
                \'terms\' => $category->term_id,
            ]),
            \'posts_per_page\' => 1,
            \'orderby\' => \'date\',
            \'order\' => \'ASC\'
          );

          global $post;

          $query = new WP_Query($args);

          if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {

              $query->the_post();

              $image = get_the_post_thumbnail_url( $post->ID, \'medium\', null );

            }

          wp_reset_postdata();

          } else {

            // some default image

          }

        else:
          $image = $image_array[\'sizes\'][\'medium\'];
        endif;

        $link .= \'<img src="\' . $image . \'" alt="\'. $category->name .\'">\';
        $link .= "<br/>" . $cat_name . \'</a>\';

        if ( ! empty( $args[\'show_count\'] ) ) {
          $link .= \' (\' . number_format_i18n( $category->count ) . \')\';
        }
        $output .= "\\t<li";
        $class = \'cat-item cat-item-\' . $category->term_id;
        if ( ! empty( $args[\'current_category\'] ) ) {
            $_current_category = get_term( $args[\'current_category\'], $category->taxonomy );
            if ( $category->term_id == $args[\'current_category\'] ) {
                $class .=  \' current-cat\';
            } elseif ( $category->term_id == $_current_category->parent ) {
                $class .=  \' current-cat-parent\';
            }
        }
        $output .=  \' class="\' . $class . \'"\';
        $output .= ">$link\\n";
    }
}

结束