通过php plugin命令显示背景图片

时间:2015-05-20 18:16:42

标签: php html wordpress wordpress-plugin

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

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

所以我有插件可以将图像分类。我需要的是图像将适用于​​&#39; li&#39;阻止并可以看作背景:url(插件网址)&#39;在CSS中如果可能的话。这个没有做任何事。 不知道我应该使用哪个。

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 .'>';

它现在显示在我的网站的检查器网址中,而不是插件给出的图片。 enter image description here

编辑3 如果我添加foreach (get_categories() as $cat)它的工作,我得到了图像

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代码,添加了大量<li>块和其他内容,因此它会相互覆盖。也许我应该把它放在其他地方?

小编辑 - 代码在索引页面上不起作用(它是带有类别侧边栏的块),但是当您输入任何帖子时,它会显示正确的类别图片,但它也会显示每个块边栏上的类别相同的图片。 所以也许我会问一些不可能的东西,插件不能这样做。

1 个答案:

答案 0 :(得分:0)

您需要在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);
            }
        });