You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress如何筛选显示URL/slug包含-nv/的页面

解决方案:筛选显示URL包含-nv/的页面

方法一:通过WP_Query直接筛选(推荐)

直接在数据库查询阶段过滤不符合条件的页面,比先查询所有页面再过滤更高效。利用页面的post_name(slug字段)做模糊匹配,因为URL中的-nv/对应slug里的-nv关键词:

修改后的完整代码:

?><style>
table {
    counter-reset: rowNumber;
}

table tr:not(:first-child) {
    counter-increment: rowNumber;
}

table tr td:first-child::before {
    content: counter(rowNumber);
    min-width: 1em;
    margin-right: 0.5em;
    color: #cfcfcf;
}</style>
    <table>
  <tr>
    <th>id</th>
    <th>title</th>
    <th>url</th>
  </tr>
<?php 
// 查询仅slug包含-nv的页面
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order'   => 'ASC',
    'post_name__like' => '-nv' // 核心筛选条件
));

foreach ($all_wp_pages as $value){
    $post = get_page($value);
    $title = $post->post_title;
    $id = $post->ID;
    $permalink = get_permalink($id);
?> 
  <tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $title; ?></td>
    <td><?php echo $permalink; ?></td>
  </tr>
<?php }; ?>
    </table>

方法二:循环内判断URL(兼容场景)

如果无法修改查询参数,可在循环中检查页面 permalink 是否包含-nv/,符合条件才输出内容:

修改后的完整代码:

?><style>
table {
    counter-reset: rowNumber;
}

table tr:not(:first-child) {
    counter-increment: rowNumber;
}

table tr td:first-child::before {
    content: counter(rowNumber);
    min-width: 1em;
    margin-right: 0.5em;
    color: #cfcfcf;
}</style>
    <table>
  <tr>
    <th>id</th>
    <th>title</th>
    <th>url</th>
  </tr>
<?php 
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query( array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order'   => 'ASC',
));

foreach ($all_wp_pages as $value){
    $post = get_page($value);
    $title = $post->post_title;
    $id = $post->ID;
    $permalink = get_permalink($id);
    
    // 核心判断:URL包含-nv/才输出
    if (strpos($permalink, '-nv/') !== false) {
?> 
  <tr>
    <td><?php echo $id; ?></td>
    <td><?php echo $title; ?></td>
    <td><?php echo $permalink; ?></td>
  </tr>
<?php 
    }
}; ?>
    </table>

补充说明

  • 方法一性能更优,减少不必要的数据查询,优先推荐使用。
  • 方法二适合无法修改查询参数的特殊场景,逻辑简单直接。

内容的提问来源于stack exchange,提问作者Ivan Dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 09:10:52