如何对wp_list_ages的结果执行str_place

时间:2016-08-08 作者:Heather

我试图定位并替换函数生成的文本。我不太确定我做错了什么。

$InnerPages = wp_list_pages(\'child_of=\'.($post->post_parent != false ? $post->post_parent : $post->ID).\'&title_li=&echo=0\');
echo $InnerPages;
输出:

<ul>
    <li>1</li>
    <li>2
        <ul class="children">
            <li>2.1</li>
        </ul>
   </li>
</ul>
我想修改<ul class="children"> 因此,我尝试执行str\\u替换:

$InnerPages = wp_list_pages(\'child_of=\'.($post->post_parent != false ? $post->post_parent : $post->ID).\'&title_li=&echo=0\')
$InnerPages = str_replace(\'<ul class="children">\',
                          \'<ul class="children"><li>Overview</li>\',
                          $InnerPages);
echo $InnerPages;
但这没什么作用,我对PHP还不熟悉,所以我不确定我做错了什么。

EDIT

我修改了代码,以反映出在一些故障排除之后迄今为止所做的更改(见下文)。代码仍然不起作用。

1 个回复
SO网友:Andy Macaulay-Brook

更改您的$child 在您的代码中$InnerPages, 像这样:

$InnerPages = wp_list_pages(\'child_of=\'.($post->post_parent != false ? $post->post_parent : $post->ID).\'&title_li=&echo=0\');
$InnerPages = str_replace(\'<ul class="children">\',
                               \'<ul class="children"><li>Overview</li>\',
                               $InnerPages);
echo $InnerPages;
您正在将更改保存在$child中,但随后显示了未更改的$InnerPages。此外,该类是children 而不是child 并且必须与工作完全匹配。

编辑以容纳注释:

如果我获取您的输出:

<li class="page_item page-item-113 page_item_has_children current_page_item"><a href="http://127.0.0.1/abbatech/procurement-capabilities/los-alamos-national-laboratory/">Los Alamos National Laboratory</a>
<ul class="children">
    <li class="page_item page-item-115"><a href="http://127.0.0.1/abbatech/procurement-capabilities/los-alamos-national-laboratory/online-contact-form/">Online Contact Form</a></li>
</ul>
</li>
<li class="page_item page-item-95"><a href="http://127.0.0.1/abbatech/procurement-capabilities/nasa-sewp-v/">NASA SEWP V</a></li>
<li class="page_item page-item-2"><a href="http://127.0.0.1/abbatech/procurement-capabilities/sample-page/">Sample Page</a></li>
我应用你的代码,根据我的回答更正:

$InnerPages = \'
<li class="page_item page-item-113 page_item_has_children current_page_item"><a href="http://127.0.0.1/abbatech/procurement-capabilities/los-alamos-national-laboratory/">Los Alamos National Laboratory</a>
<ul class="children">
<li class="page_item page-item-115"><a href="http://127.0.0.1/abbatech/procurement-capabilities/los-alamos-national-laboratory/online-contact-form/">Online Contact Form</a></li>
</ul>
</li>
<li class="page_item page-item-95"><a href="http://127.0.0.1/abbatech/procurement-capabilities/nasa-sewp-v/">NASA SEWP V</a></li>
<li class="page_item page-item-2"><a href="http://127.0.0.1/abbatech/procurement-capabilities/sample-page/">Sample Page</a></li>
\';

$InnerPages = str_replace(\'<ul class="children">\',
    \'<ul class="children"><li>Overview</li>\',
    $InnerPages);
echo $InnerPages;
我得到以下输出:

<li class="page_item page-item-113 page_item_has_children current_page_item"><a href="http://127.0.0.1/abbatech/procurement-capabilities/los-alamos-national-laboratory/">Los Alamos National Laboratory</a>
<ul class="children"><li>Overview</li>
    <li class="page_item page-item-115"><a href="http://127.0.0.1/abbatech/procurement-capabilities/los-alamos-national-laboratory/online-contact-form/">Online Contact Form</a></li>
</ul>
</li>
<li class="page_item page-item-95"><a href="http://127.0.0.1/abbatech/procurement-capabilities/nasa-sewp-v/">NASA SEWP V</a></li>
<li class="page_item page-item-2"><a href="http://127.0.0.1/abbatech/procurement-capabilities/sample-page/">Sample Page</a></li>
那么,什么不起作用?

相关推荐