在查看器中插入HTMLInside链接

时间:2014-06-17 作者:JacobTheDev

我有一个自定义导航助行器,它(理论上)从自定义分类法的最新帖子中提取特征图像,并在wp_list_categories.

问题是我需要在链接中的类别名称上方显示图像。我似乎不知道该怎么做。

这是我的定制助行器:

class CategoryThumbnailWalker extends Walker_Category {
    function start_el(&$output, $category, $depth, $args) {
        parent::start_el(&$output, $category, $depth, $args);
        $posts = get_posts(array(
            "child_of"         => $category->term_id, // may need to be disabled; we\'ll see
            "post_type"        => "projects",
            "project-category" => $category->slug,
        ));
        if ($posts) {
            foreach ($posts as $post) {
                if (has_post_thumbnail($post->ID)) {
                    $output .= get_the_post_thumbnail($post->ID, "gallery-small");

                } else {
                    $output .= "<img alt=\\"No image available\\" src=\\"" . get_template_directory_uri() . "/img/no-image.jpg\\" />";
                }
                break;
            }
        } else {
            $output .= "<img alt=\\"No image available\\" src=\\"" . get_template_directory_uri() . "/img/no-image.jpg\\" />";
        }
    }
}
这是我的wp_list_categories:

$category = $wp_query->get_queried_object_id();
wp_list_categories(array(
    "child_of"         => $category,
    "depth"            => 1,
    "hide_empty"       => false,
    "hierarchical"     => 1,
    "orderby"          => "name",
    "pad_counts"       => 0,
    "post_type"        => "projects",
    "show_count"       => 0,
    "show_option_none" => "",
    "taxonomy"         => "project-category",
    "title_li"         => "",
    "walker"           => new CategoryThumbnailWalker()
));
以下是it的全部输出:

<li class="cat-item cat-item-13">
    <a href="http://christopherconsultants.myweblinx.net/project-category/education/" title="View all posts filed under Education">
        Education
    </a>
    <img alt="No image available" src="http://christopherconsultants.myweblinx.net/wp-content/themes/christopher-consultants/img/no-image.jpg">
</li>

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

您可以尝试跳过CategoryThumbnailWalker 并使用list_cats 滤器

以下是一个未经测试的示例:

add_filter( \'list_cats\', \'wpse_149898_list_cats\', 99, 2 );

wp_list_categories(array(
    "child_of"         => get_queried_object_id(),
    "depth"            => 1,
    "hide_empty"       => false,
    "hierarchical"     => 1,
    "orderby"          => "name",
    "pad_counts"       => 0,
    "post_type"        => "projects",
    "show_count"       => 0,
    "show_option_none" => "",
    "taxonomy"         => "project-category",
    "title_li"         => "",
));

remove_filter( \'list_cats\', \'wpse_149898_list_cats\', 99, 2 );
在哪里

/**
 * Prepend the featured image, from the most recent project 
 * in a custom taxonomy, to the term name.
 *
 * @param string $cat_name
 * @param object $category
 * @return string 
 */    

function wpse_149898_list_cats(  $cat_name, $category )
{   
    $posts = get_posts( array(
        \'post_type\'        => \'projects\',
        \'posts_per_page\'   => 1,
        \'meta_key\'         =>\'_thumbnail_id\',
        \'project-category\' => $category->slug,
    ) );

    $img = sprintf( "<img alt=\\"No image available\\" src=\\"%s/img/no-image.jpg\\" />",
        get_template_directory_uri()
    );

    if ( $posts ):
        foreach ( $posts as $post )
        {
            if ( has_post_thumbnail( $post->ID ) )
                $img = get_the_post_thumbnail( $post->ID, \'gallery-small\' );

        }
    endif;    
    return $img . $cat_name;
}        
输出(希望)如下:

<li class="cat-item cat-item-13">
    <a href="http://christopherconsultants.myweblinx.net/project-category/education/" title="View all posts filed under Education">
        <img alt="No image available" src="http://christopherconsultants.myweblinx.net/wp-content/themes/christopher-consultants/img/no-image.jpg">
        Education
    </a>
</li>

结束

相关推荐

从WP_LIST_CATEGORIES中删除‘Category’一词

我有以下代码:wp_list_categories();它输出我的所有类别,但是,它使它们成为列表元素的子元素,称为CATEGORIES.所以,我得到了一个无序的列表,如下所示:CATEGORIES * FASHION * DAILY FASHION CANDY * TRENDS * BEAUTY * ACCESSORIES * CELEBRITIES * LIFESTYLE 但是,我只需要:* FASHION * DAILY FA