想要使用函数中的代码从列表中删除一个<li>。

huangapple go评论125阅读模式
英文:

want to remove a <li> from list with a code in functions

问题

我有一个WordPress插件,支持非常差。这是一个用于班车服务的插件。为了预订班车,访客必须选择一个上车点和一个目的地点。

这两个点都在列表中进行配置,并且用于两个选择列表。有些用于上车,其他用于目的地。

在上车点,我想隐藏/删除一些项目,但是插件的任何配置都无法实现。

我希望有人能理解我的问题。首先,我提供一些构建表单的代码,然后是我尝试过但不起作用的方法...

首先是代码:

<div class="mage_input_select mage_bus_boarding_point">
<div class="route-input-wrap"><input id="bus_start_route" type="text" class="mage_form_control" name="bus_start_route" value="F&#252;rth" placeholder="Please Select" autocomplete="off" required=""></div>
<div class="mage_input_select_list" style="display: block;">
<ul>
<li data-route="Bruck a/d Gro&#223;glocknerstra&#223;e"><span class="fa fa-map-marker"></span>Bruck a/d Gro&#223;glocknerstra&#223;e</li>
<li data-route="F&#252;hrt - Mittersill"><span class="fa fa-map-marker"></span>F&#252;hrt - Mittersill</li><li data-route="F&#252;rth"><span class="fa fa-map-marker"></span>F&#252;rth</li>
<li data-route="Kaprun"><span class="fa fa-map-marker"></span>Kaprun</li>
<li data-route="Salzburg Flughafen"><span class="fa fa-map-marker"></span>Salzburg Flughafen</li>
<li data-route="Taxenbach"><span class="fa fa-map-marker"></span>Taxenbach</li>
<li data-route="test patrick"><span class="fa fa-map-marker"></span>test patrick</li>
<li data-route="Zell am See"><span class="fa fa-map-marker"></span>Zell am See</li>
</ul>
</div>
</div>

我想要隐藏的部分是...

<li data-route="F&#252;hrt - Mittersill"><span class="fa fa-map-marker"></span>F&#252;hrt - Mittersill</li>
<li data-route="test patrick"><span class="fa fa-map-marker"></span>test patrick</li>

我在functions.php文件中尝试的代码如下:

function remove_list_items_from_page_content($content) {
   
    $content = preg_replace('/<li data-route="(F&#252;hrt - Mittersill|test patrick)">.*?<\/li>/', '', $content);
    return $content;
}

function modify_page_content() {
   
    if (is_page(130)) { 
       
        $page_content = get_post_field('post_content', 130); 
        
        
        $new_page_content = remove_list_items_from_page_content($page_content);
        
        
        $new_page_content = wp_kses_post($new_page_content); 
        wp_update_post(array('ID' => 130, 'post_content' => $new_page_content)); 
    }
}
add_action('wp', 'modify_page_content');

但是它不起作用,有什么问题吗?

包含该表单的页面的ID是130,也是我的起始页面...

我不知道在这里是否允许提供我的开发页面的URL?

英文:

i have a wordpress plugin where the support is symply bad. this is a plugin for a shuttle service. to book a shuttle, the visitor have to pick a boarding point and a destination point.

both are configured in a list and are used for both select lists. some are used for boarding, other for the destination.

in boarding i want to hide/some items, and this is not possible by any configuration af the plugin.

i hope someone understand my issue. first i give some code how the form is build, then what i have try but it does not work...

first the code:

&lt;div class=&quot;mage_input_select mage_bus_boarding_point&quot;&gt;
&lt;div class=&quot;route-input-wrap&quot;&gt;&lt;input id=&quot;bus_start_route&quot; type=&quot;text&quot; class=&quot;mage_form_control&quot; name=&quot;bus_start_route&quot; value=&quot;F&#252;rth&quot; placeholder=&quot;Please Select&quot; autocomplete=&quot;off&quot; required=&quot;&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;mage_input_select_list&quot; style=&quot;display: block;&quot;&gt;
&lt;ul&gt;
&lt;li data-route=&quot;Bruck a/d Gro&#223;glocknerstra&#223;e&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Bruck a/d Gro&#223;glocknerstra&#223;e&lt;/li&gt;
&lt;li data-route=&quot;F&#252;hrt - Mittersill&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;F&#252;hrt - Mittersill&lt;/li&gt;&lt;li data-route=&quot;F&#252;rth&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;F&#252;rth&lt;/li&gt;
&lt;li data-route=&quot;Kaprun&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Kaprun&lt;/li&gt;
&lt;li data-route=&quot;Salzburg Flughafen&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Salzburg Flughafen&lt;/li&gt;
&lt;li data-route=&quot;Taxenbach&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Taxenbach&lt;/li&gt;
&lt;li data-route=&quot;test patrick&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;test patrick&lt;/li&gt;
&lt;li data-route=&quot;Zell am See&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Zell am See&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;

what i want to hide...

&lt;li data-route=&quot;F&#252;hrt - Mittersill&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;F&#252;hrt - Mittersill&lt;/li&gt;
&lt;li data-route=&quot;test patrick&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;test patrick&lt;/li&gt;

and what i did try for my functions.php file:

function remove_list_items_from_page_content($content) {
   
    $content = preg_replace(&#39;/&lt;li data-route=&quot;(F&#252;hrt - Mittersill|test patrick)&quot;&gt;.*?&lt;\/li&gt;/&#39;, &#39;&#39;, $content);
    return $content;
}

function modify_page_content() {
   
    if (is_page(130)) { 
       
        $page_content = get_post_field(&#39;post_content&#39;, 130); 
        
        
        $new_page_content = remove_list_items_from_page_content($page_content);
        
        
        $new_page_content = wp_kses_post($new_page_content); 
        wp_update_post(array(&#39;ID&#39; =&gt; 130, &#39;post_content&#39; =&gt; $new_page_content)); 
    }
}
add_action(&#39;wp&#39;, &#39;modify_page_content&#39;);

But it is not working, what is wrong?

the page with the form have ID-130 and is my start-page...

I do not know if it is forbidden here to give the url of my develop page?

答案1

得分: 3

你可以使用CSS以以下方式来实现,使用选择器中的"data-route"属性:

li[data-route="Führt - Mittersill"],
li[data-route="test patrick"] {
  display: none;
}
<div class="mage_input_select mage_bus_boarding_point">
  <div class="route-input-wrap"><input id="bus_start_route" type="text" class="mage_form_control" name="bus_start_route" value="Fürth" placeholder="Please Select" autocomplete="off" required=""></div>
  <div class="mage_input_select_list" style="display: block;">
    <ul>
      <li data-route="Bruck a/d Großglocknerstraße"><span class="fa fa-map-marker"></span>Bruck a/d Großglocknerstraße</li>
      <li data-route="Führt - Mittersill"><span class="fa fa-map-marker"></span>Führt - Mittersill</li>
      <li data-route="Fürth"><span class="fa fa-map-marker"></span>Fürth</li>
      <li data-route="Kaprun"><span class="fa fa-map-marker"></span>Kaprun</li>
      <li data-route="Salzburg Flughafen"><span class="fa fa-map-marker"></span>Salzburg Flughafen</li>
      <li data-route="Taxenbach"><span class="fa fa-map-marker"></span>Taxenbach</li>
      <li data-route="test patrick"><span class="fa fa-map-marker"></span>test patrick</li>
      <li data-route="Zell am See"><span class="fa fa-map-marker"></span>Zell am See</li>
    </ul>
  </div>
</div>

注意:以上代码是一个示例,用于演示如何使用CSS选择器和"data-route"属性来隐藏特定的列表项。你可以根据自己的需求进行修改和适应。

英文:

You can use CSS to do that in the following way, using the "data-route" attribute in a selector:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

li[data-route=&quot;F&#252;hrt - Mittersill&quot;],
li[data-route=&quot;test patrick&quot;] {
  display: none;
}

<!-- language: lang-html -->

&lt;div class=&quot;mage_input_select mage_bus_boarding_point&quot;&gt;
&lt;div class=&quot;route-input-wrap&quot;&gt;&lt;input id=&quot;bus_start_route&quot; type=&quot;text&quot; class=&quot;mage_form_control&quot; name=&quot;bus_start_route&quot; value=&quot;F&#252;rth&quot; placeholder=&quot;Please Select&quot; autocomplete=&quot;off&quot; required=&quot;&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;mage_input_select_list&quot; style=&quot;display: block;&quot;&gt;
&lt;ul&gt;
&lt;li data-route=&quot;Bruck a/d Gro&#223;glocknerstra&#223;e&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Bruck a/d Gro&#223;glocknerstra&#223;e&lt;/li&gt;
&lt;li data-route=&quot;F&#252;hrt - Mittersill&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;F&#252;hrt - Mittersill&lt;/li&gt;&lt;li data-route=&quot;F&#252;rth&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;F&#252;rth&lt;/li&gt;
&lt;li data-route=&quot;Kaprun&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Kaprun&lt;/li&gt;
&lt;li data-route=&quot;Salzburg Flughafen&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Salzburg Flughafen&lt;/li&gt;
&lt;li data-route=&quot;Taxenbach&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Taxenbach&lt;/li&gt;
&lt;li data-route=&quot;test patrick&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;test patrick&lt;/li&gt;
&lt;li data-route=&quot;Zell am See&quot;&gt;&lt;span class=&quot;fa fa-map-marker&quot;&gt;&lt;/span&gt;Zell am See&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月9日 00:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定