WordPress:在两个菜单项之间插入字符

WordPress: insert char in between two menu items

我正在开发我的自定义 wordpress 主题,但我很难按照我想要的方式设计 header 第一个菜单。

所以,在 header.php 中,我以这种方式插入菜单,这样链接就不会被 table 标签包围:

    <?php 
          $menuParameters = array(
              'container'         =>  false,
              'echo'              =>  false,
              'items_wrap'        =>  '%3$s',
              'depth'             =>  0,
          );
          $output = strip_tags(wp_nav_menu($menuParameters), '<a>');
          //This is my first try with regex, but i didnt manage to
          //only insert the character in between two links and not
          //one at the and as well.
          //$output = Preg_replace( "(\n)", "[=10=]|\r\n", $output);
          echo $output;
    ?>

所以这段代码给了我一个看起来像这样的菜单:

    <a href="http://localhost/page-1/">Page 1</a>
    <a href="http://localhost/page-2/">Page 2</a>
    <a href="http://localhost/page-3/">Page 3</a>
    <a href="http://localhost/page-4/">Page 4</a>
    <a href="http://localhost/page-5/">Page 5</a>

仅供理解,这是我想要得到的输出:

    <a href="http://localhost/page-1/">Page 1</a>
    |
    <a href="http://localhost/page-2/">Page 2</a>
    |
    <a href="http://localhost/page-3/">Page 3</a>
    |
    <a href="http://localhost/page-4/">Page 4</a>
    |
    <a href="http://localhost/page-5/">Page 5</a>

提前感谢您的回答!

<a href="http://localhost/page-1/">Page 1</a><a href="http://localhost/page-2/">Page 2</a><a href="http://localhost/page-3/">Page 3</a><a href="http://localhost/page-4/">Page 4</a><a href="http://localhost/page-5/">Page 5</a>

wp_nav_menu() 允许参数 afterlink_after 在 link 文本或 link 标记之后添加文本,因此您可以更新您的参数:

$menuParameters = array(
          'container'         =>  false,
          'echo'              =>  false,
          'items_wrap'        =>  '%3$s',
          'depth'             =>  0,
          'after'             => "<span>|</span>"
);

https://developer.wordpress.org/reference/functions/wp_nav_menu/

编辑:如果你不想在最后一个 link 之后显示分隔符,你可以使用一些 CSS 伪元素:

.mymenu span:last-child{ display: none; }

https://jsfiddle.net/k593ezoL/1