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

时间:2018-09-15 08:59:56

标签: php wordpress wordpress-theming custom-wordpress-pages

我正在研究自定义的wordpress主题,并且正在努力按照自己想要的方式设计标题中的第一个菜单。

因此,在header.php中,我以这种方式插入菜单,以使链接不会被表标签包围:

    <?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)", "$0|\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>

谢谢您的回答!

2 个答案:

答案 0 :(得分:0)

<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>

答案 1 :(得分:0)

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

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

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

编辑:如果您不想在最后一个链接后显示分隔符,则可以使用一些CSS伪元素:

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

https://jsfiddle.net/k593ezoL/1