PHP:向脚本添加html图标

PHP:向脚本添加html图标,php,wordpress,widget,Php,Wordpress,Widget,我需要将相同的图标添加到每个li类中,它们是由我在Wordpress站点的定制小部件中使用的PHP脚本自动生成的 这是我在wordpress小部件中用来显示一些子页面的脚本 <?php $ancestor_id=24; $descendants = get_pages(array('child_of' => $ancestor_id)); $incl = ""; foreach ($descendants as $page) { if (($page->post_pa

我需要将相同的图标添加到每个li类中,它们是由我在Wordpress站点的定制小部件中使用的PHP脚本自动生成的

这是我在wordpress小部件中用来显示一些子页面的脚本

<?php
 $ancestor_id=24;
 $descendants = get_pages(array('child_of' => $ancestor_id));
 $incl = "";

foreach ($descendants as $page) {
 if (($page->post_parent == $ancestor_id) ||
    ($page->post_parent == $post->post_parent) ||
    ($page->post_parent == $post->ID))
 {
     $incl .= $page->ID . ",";
 }
}?>

<ul>
   <?php wp_list_pages(array(
                        "child_of" => $ancestor_id,
                        "include" => $incl,
                        "link_before"      => "",
                        "title_li" => "",
                        "sort_column" => "menu_order"));?>
</ul>

我需要使用的图标代码(已经指定了一个类)是

<img class='sideicons' src="http://accountabletest.com/wp-content/uploads/2013/12/iconSidebar1.jpg" alt="" >


如何使生成的每个
  • 都包含相同的图标?

    听起来在PHP中这样做可能比它的价值更麻烦。。。javascript或jquery怎么样

    $('li').each(function(i,el){
      $(this).prepend('<img src="path/to/image.ico" />');
    })
    
    $('li')。每个(函数(i,el){
    $(this.prepend(“”);
    })
    
    如果您需要更改图像的现有源,请使用类似于
    $('li img')的内容作为目标。


    .prepend()
    更改为
    attr('src','your/new/image.jpg')
    使用css,您需要的规则是
    列表样式类型
    列表样式图标
    为什么不使用css?这是一种更简单的方法。只需为ul分配一个类,并为li标记添加一个背景图像

    比如:

    李先生{ 背景:url(http://accountabletest.com/wp-content/uploads/2013/12/iconSidebar1.jpg)左中无重复; 左侧填充:10px; }
    <ul class="list">
        <?php wp_list_pages(
             array(
                 "child_of" => $ancestor_id,
                 "include" => $incl,
                 "link_before" => "",
                 "title_li" => "",
                 "sort_column" => "menu_order"
             )
         );?>
    </ul>
    
    
    <style>
      ul.list li {
           background: url(http://accountabletest.com/wp-content/uploads/2013/12/iconSidebar1.jpg) no-repeat left center;
           padding-left: 10px;
      }
    </style>