我猜是有点肮脏的代码。这是一个带有自定义wp_nav代码的函数php。

    $output .= $indent . '<div class="categories-wrapper"><li' . z_taxonomy_image($cat->term_id) . $id . $class_names .'>';

所以我有一个插件,给图片分类。我需要的是,该图像将应用于“li”块,如果可能的话,可以在css中被视为“background:url(pluginurl)”。这个没用。
但我不知道该用哪种。
z_taxonomy_image($cat->term_id)

插件文档列表-categories image
剩下的代码我想没关系。如果没有,我会补充。
编辑
找到一个有用的帖子,我认为可以解决我的问题,但我仍然不知道如何正确使用它。如何在<li>中插入?
 if (function_exists('z_taxonomy_image_url')) $background_url = 'background:url(' . z_taxonomy_image_url() . ')';
echo '<div class="' . $tax_term->slug . '" style="display:none; ' . $background_url . '">';

完整帮助线程stackoverflow
编辑2
找到了一个合适的密码
$output .= $indent . '<div class="categories-wrapper"><li style="background: url(' . z_taxonomy_image_url($cat->term_id) . ')"' . $id . $class_names .'>';

它现在显示在我的站点的inspector url中,而不是插件提供的图片。
编辑3
如果我加上它工作了,我得到了图像
foreach (get_categories() as $cat)
    $output .= $indent . '<div class="categories-wrapper"><li style="background:url(' . (function_exists('z_taxonomy_image') ? z_taxonomy_image_url($cat->term_id) : '') . ')"' . $id . $class_names .'>';

但它弄乱了整个html代码,添加了大量的foreach (get_categories() as $cat)块和其他块,因此它相互覆盖。也许我应该把它放在别的地方?
小的编辑代码不在索引页上工作(它是一个带有类别侧边栏的块),但是当您输入任何文章时,它会显示正确的类别图片,同时也会为同一个侧边栏上的每个类别块显示相同的图片。
所以我可能会问一些不可能的事情,而那个插件却做不到。

最佳答案

您需要在style=”中插入图像
可能是这样的:

'<li style="url('z_taxonomy_image($cat->term_id) ')"' . $id . $class_names .'>';

另一个解决方案
为什么不使用json响应并附加dom元素?将每张图片插入所需元素。
在下面的例子中,我接收到一个json图片数组,并将它们插入到一个特定的元素中。
例如,您可以检索图片和图片类型,并按类型插入到元素中。
这是我第一次想帮忙,温柔点。
对于您的示例,可以使用.css()设置每个li元素的css
$.ajax({
            type: "POST",
            data: {text: text, images: images, images_big: images_big},
            url: "/ajax/" + $(this).attr('data-post') + "/createPostComment",
            dataType: 'json',
            error: function () {
                $el.remove();
                return;
            },
            success: function (r) {
                $el.attr('data-id',r.comment_id);
                if(typeof r.pictures != 'undefined'){
                    for(var i=0; i < r.pictures.length; i++){
                        el.find('.comment-pictures-container').append('<img src="'+ r.pictures[i] +'" data-big="'+ r.pictures_big[i] +'" />');
                    }
                }
                bindCommentGallery($el);
                bindCommentMenu($el);
            }
        });

10-04 21:31
查看更多