Hi

How could I add this symbol "|" between each item for primary links using a phptemplate theme?

First item | Second item

Thanks in advance.

Comments

gabriella’s picture

Maybe you should look at this page http://drupal.org/node/44708
submitted by Dublin Drupaller a few minutes ago

Dublin Drupaller’s picture

In your page.tpl.php file..where the primary links are placed..use this code and edit it to insert you're own delimiter.

I used this before to convert it to an unordered list.

<div id="navigation">
<?php if ($links['primary']) : ?>
<ul id="main-nav">
<?php foreach ($links['primary'] as $link): ?>
<li><?php print $link?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>

In non progammer speak, it splits the primary_links apart and then outputs each one seperately.

Hope that helps

Dub

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

nevets’s picture

This is a variation on Dublin's solution which saves you from keep track of first/last link (to avoid an extra delimiter)
It sets $link_delimiter is set to what you want to visually seperate the links by. $delimiter is set to the html plus link delimiter that seperates the links. In this case it is the closing list tag (to close the previous link), the link delimiter and an opening list tag (for the next link item.) Finally it prints the opening link tag (for the first link) followed by the links seperated by the delimiter and ending with a closing link tag (for the last link)

<div id="navigation">
<?php if ($links['primary']) : ?>
<ul id="main-nav">
<?php
$link_delimiter = '|';  // Change this to what you want the links seperated by
$delimiter = '</li>' . $link_delimiter . '<li>';
print '</li>' . implode($delimiter, $links['primary']) . '</li>';
?>
</ul>
<?php endif; ?>
</div>
introfini’s picture

how can I add a class identifier for the a tag?

like in this situation a.nav

<a href="#" class="nav">Home</a>

Thanks

José Fernandes
Bloomidea

lustratus’s picture

I use this in a phpTemplate theme:


foreach ($primary_links as $link) {
   print preg_replace('/<a (.*?)>(.*?)<\\/a>/i','<a \\1 class="yourClass">\\2</a>',$link); 
}

And more advanced (due to my incomplete understanding of how the $primary_links variable is served, I can't get a for() loop to work):


$i=0;
foreach ($primary_links as $link) {
	 if ($i==0) { 
		print preg_replace('/<a (.*?)>(.*?)<\\/a>/i','<a \\1 class="firstItem">\\2</a>',$link); 
	 } 
	 else { 
		print preg_replace('/<a (.*?)>(.*?)<\\/a>/i','<a \\1 class="otherItems">\\2</a>',$link); 
	 } 
	 $i++;
 }

 
brunodbo’s picture

yep, the code above works for me in 4.7.

I used

<?php
foreach ($primary_links as $link) {
  print $link;
}
?>

that way, you can add whatever html/css to your primary links ($link).

--
Don't hate the media, be the media -- www.indymedia.be

Will White’s picture

If I want to add a class to each link that was based on the text of the link, I have to use some pretty complex regular expression manipulation. Wouldn't it just make sense for the theme to get each individual component of the link (text, path, attributes, ect) for more flexibility? It's frustrating that we have to use memory and time to break apart are string that was just generated earlier in menu_primary_links(). Or is there another way?

wtdtan’s picture

diggersf, are you trying to get an "id" in the surround <li> tags to each link? i've been trying to find some documentation on this as well.
does anyone know how to get the information from the links you created?

markhope’s picture

I'm also looking to output an ID based on the text from the link.
I'm no PHP programmer but I've settled on the following to at least create a unique id for list items:

<?php if (isset($primary_links)): ?>
      <ul id="primary">
      <?php 
      $i=1;
      foreach ($primary_links as $link): ?>
        <li id="primarynav<?php print t($i) ?>"><?php print $link?></li>
      <?php 
      $i++;
      endforeach; ?>
      </ul>
    <?php endif; ?>

This way each link can be styled with CSS. For example the first link in the primary nav is output with an id of "primarynav1"

I'm also using a snippet to give each page a unique body id.
http://drupal.org/node/46024

By using the combined IDs for the section Nav can be highlighted. eg.

#page-benefits-case-studies #primarynav2 {
    background-color: #000;
    }

The drawback is that the stylesheet needs a declaration for each combination.

As I said I'm not a programmer so I'm hoping for a better solution if anybody can suggest one.

markhope’s picture

Khalid B posted this on the themes list...
http://2bits.com/articles/adding-unique-styles-to-primary-and-secondary-...

It's a much better fix for creating unique Ids

Apparently this functionality will be included in 5.0

tated’s picture

These last two fixes both seem to work well. Can anyone help me figure out how to combine Khalid B's fix with the snippet at http://drupal.org/node/62149 which changes primay links to images? I know little-to-no php and just don't know how to make the mechanics work in 4.7. Thanks a million.

redraven’s picture

Thanks for the snippet

I tried this on 4.7 bluemarine and I can't seem to get it to work.

Do I need to call a function in a template.php file? As described at:

http://drupal.org/node/44708

Or is there simply something I am not understanding, maybe a change in how primary links are handled in 4.7?

Thanks for any ideas/advice

stretchwickster’s picture

Dub,

Thank you for posting the primary links snippet in the Customising links node - I found it very helpful as someone who's just starting out with theming. I originally started out with xtemplate and had already sorted out horizontal navigation using a ul in the primary links. However, when I moved over to phpTemplate I was struggling to recreate the horiz. navigation until I came across your snippet - thank you once again.

AlanT’s picture

I was just looking for a way to convert the primary links to an unordered list for a new template that I'm converting to work with Drupal.

Now what I need to know is how to set the id of the active primary link to a specific style?

In the template that I'm converting, it uses the following code:

<div id="navcontainer">

<ul id="navlist">
<li id="active"><a href="#" id="current">Item one</a></li>
<li><a href="#">Item two</a></li>
<li><a href="#">Item three</a></li>
<li><a href="#">Item four</a></li>
<li><a href="#">Item five</a></li>
</ul>
</div>

How do I style the primary link menu to fit this format?

As flexible as Drupal is for the non-programmer, templating is still in the dark ages of using PHP.

- Alan Tutt
http://www.PowerKeysPub.com

- Alan Tutt

Exceptional Personal Development for Exceptional People
http://www.PowerKeysPub.com

yelvington’s picture

AlanT’s picture

Thanks for pointing out the program code that generates the primary links, but that's not at all what I'm looking for. For one, it's PHP, and for another, there is no mention on that page whatsoever about the "current" active link.

I'm not a programmer, nor do I want to learn PHP just to design the layout of my site.

- Alan Tutt
http://www.PowerKeysPub.com

- Alan Tutt

Exceptional Personal Development for Exceptional People
http://www.PowerKeysPub.com

venkat-rk’s picture

That's strange. It happens automatically in my phptemplate bluemarine version.

----
Acknowledging a reply to your question motivates the replier to answer more.

Anonymous’s picture

Hi !
I use this one with Phphtemplate Drupal 4.6.6

<div id="navigation">
<?php if (is_array($primary_links)) : ?>
<ul id="main-nav">
<?php
$link_delimiter = '|';  // Change this to what you want the links seperated by
$delimiter = '</li>' . $link_delimiter . '<li>';
print '</li>' . implode($delimiter, $primary_links) . '</li>';
?>
</ul>
<?php endif; ?>
</div>
stephenhendry’s picture

Has any one got this working with Head? I get the word array for each navigation item when I try implementing it.

<div id="navigation">
<?php if ($links['primary']) : ?>
<ul id="main-nav">
<?php foreach ($links['primary'] as $link): ?>
<li><?php print $link?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
stephenhendry’s picture

print theme('menu_links', $primary_links)

krisvannest’s picture

thanks for that; it also helped me figure out why my primary links weren't showing up after moving a theme from 4.7.0 to 4.7.4 installation (didn't realize the menu code changed that much?). In case it helps other users, my original code was:

<?php print theme('primary', $primary_links) ?>

which worked fine in 4.7.0 but no longer displayed the Primary Links in my 4.7.4 environment. Changing to the following did work again:

	  <?php print theme('menu_links', $primary_links) ?>  

Thanks gazelle et al

Newbee-1’s picture

I'm using this code to display "|" between my primary links but the delimiter is displayed only after the first primary link. eg: Link1 ||| Link2 Link3 Link4

this is my code:

<?php if (is_array($primary_links)) : ?>
<ul id="primary">
<?php
$primary_link_delimiter = '|';
$primary_delimiter = '</li>' . $primary_link_delimiter . '<li>';  

print '<li>' . implode($primary_delimiter, $primary_links) . '</li>';
?>
</ul>
<?php endif; ?>

I have worked on this all day..Can anyone help me? I really don't know what I'm doing wrong.
Here is the html displayed by the code. It shows that the delimiter is printed after each link, but in reality all 7 delimiters are printed after "Home"

<ul id="primary">
<li>
<a href="Home" title="" class="active"><span class="lw1">Home</span></a></li>|<li>
<a href="Profile" title=""><span class="lw1">About</span></a></li>|<li>
<a href="Products" title=""><span class="lw1">Products</span></a></li>|<li>
<a href="Services" title=""><span class="lw1">Services</span></a></li>|<li>
<a href="Projects" title=""><span class="lw1">Projects</span></a></li>|<li>
<a href="Studies" title=""><span class="lw1">Studies</span></a></li>|<li>
<a href="Support" title=""><span class="lw1">Support</span></a></li>|<li>
<a href="Contact" title=""><span class="lw1">Contact Us</span></a></li></ul>

I hope my problem is clear enough. If not, ask me for more detail.Thanks

RainMan-1’s picture

this is the code:

  if (isset($primary_links)):
    print theme('links', $primary_links, ' | ');
  endif;
zanematthew’s picture

i tried adding what you posted above to my code and it does nothing, am i missing something? (using drupal 5)

    <?php if ($primary_links): ?>
        <?php print theme('menu_links', $primary_links, '/'); ?>
    <?php endif; ?>

alternatively i could just add a space and / when i create the menu using drupal gui but i would rather theme it

also I would like to do the same thing in my footer which i have made using a custom block-footer.tpl.php

<div id="F">
<?php
	print theme('links', menu_primary_links(0,$block->delta),'');
?>
</div>
nevets’s picture

You use <?php print theme('menu_links', $primary_links, '/'); ?> but the theme function 'menu_links' only expects one argument and does not expect/use the seperator argument. The theme function 'links' does not expect a seperator for the second argument. You can check out the core theme functions for Drupal 5 at http://api.drupal.org/api/group/themeable/5.

You could override the theme function adding the needed argument or provide your own theme function.

Side note, your use of menu_primary_links looks odd as the second argument should be a parent menu id.

underwearninja’s picture

I'm using the Zen template.php and a custom front page. In my front page I want to print all the links except the first link (because that first link is "home" and you're already there!). How would I go about doing this?

The code I'm using to display the links is:

foreach ($primary_links as $link):

  • 
    			$href = $link['href'] == "<front>" ? base_path() : base_path() . $link['href'];
    			print "<a href='" . $href . "'>" . $link['title'] . "</a>";
    
    			
  • endforeach;

    underwearninja’s picture

    If I just used my brain for another 2 minutes...

    I added a counter variable, and had it skip the loop on the first pass. Problem solved. This would work for any link in your array, just by changing the original number for the counter and changing when you increment the counter.

    $counter = 0;
    foreach ($primary_links as $link):

  • 			
    			if ($counter == 0) {
    				$counter++;
    				continue;
    				}
    			$href = $link['href'] == "<front>" ? base_path() : base_path() . $link['href'];
    			print "<a href='" . $href . "'>" . $link['title'] . "</a>";
    			
    
    			
  • endforeach;

    Edit: Doing this shows /node/112 type links. If anyone knows how to access the array to pull the /custom-url portion, I'd be grateful.

    daja’s picture

    Hi,
    I tried to use this wunderful code for primary links in my personal theme

    <div id="navigation">
    <?php if (is_array($primary_links)) : ?>
    <ul id="main-nav">
    <?php foreach ($primary_links as $link): ?>
    <li><?php
    
    $href = $link['href'] == "<front>" ? base_path() : base_path() . $link['href'];
    print "<a href='" . $href . "'>" . $link['title'] . "</a>";
    
    ?></li>
    
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    </div>
    

    The problem is that I use external links in my primary menu and with this code they don't work correctly. The base_url is added at the beginning of my external link, so I have this at the end in my menu:
    http://www.mysite.com/http://www.externallink.com

    Do you know how to fix it?
    Thanks

    nevets’s picture

    You could try replacing

    $href = $link['href'] == "<front>" ? base_path() : base_path() . $link['href'];
    print "<a href='" . $href . "'>" . $link['title'] . "</a>";
    

    with

    print l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
    
    daja’s picture

    Thanks a lot, your code works perfectly good. Waaaaw!!!!

    yogitha’s picture

    Hi All
    i used the below code to display primary links with images in my(drupal5.x dev)site.
    its working fine But am getting primary links text and image side by side like

    '[image1][text1] [image2][text2] [image3][text3] [image4][text4]'

    but i need primary link text under the image like

    '[image1] [image2] [image3] [image4]
    [text1] [text2] [text3] [text4]'

    Can any give solution for this asap.
    Thanks in advance.
    --------------------------------------------------
    function primary_links_add_icons() {
    $links = menu_primary_links();
    $level_tmp = explode('-', key($links));
    $level = $level_tmp[0];
    $output = "

      \n";

      $l2i['pattern'] = '$()(.*)()$ie';
      $l2i['replacement'] = "\"\\1\".'Only local images are allowed.\\2\\3'";
      if ($links) {

      foreach ($links as $link) {
      $link = l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']);
      $output .= '

    • '. preg_replace($l2i['pattern'],$l2i['replacement'],$link) .'
    • ';
      //$output .= preg_replace($l2i['pattern'],$l2i['replacement'],$link);
      };
      $output .= '

    ';
    }
    return $output;
    }

    imaginet’s picture

    I am trying to change my primary links into clickable images only no text. Can anyone help. I really need this to be fixed.

    Thanks for any help.

    yogitha’s picture

    Hi go thru this link
    http://drupal.org/node/62149
    in that Check
    "4. FOURTH SOLUTION: display icons (images) but no text"
    will be your solution .

    tetramentis’s picture

    I also needed pipe-separated menu items.
    But I didn't want them in the text - instead, I wanted them as CSS borders + padding.
    What I did:
    1. override theme_links function, by commenting out this chunk:

    // Add first and last classes to the list of links to help out themers.
    $extra_class = '';
    if ($i == 1) {
      $extra_class .= 'first ';
    }
    if ($i == $num_links) {
      $extra_class .= 'last ';
    }
    $output .= '<li class="'. $extra_class . $class .'">';
    

    and then adding just below it a single replacement line

    $output .= '<li class="'. $class .'">';
    

    and adding some code to add first/last classes to Anchors:

    // Add 'first' and 'last' classes to links to help out themers.
    if ( $i == 1 ) {
      if (isset($link['attributes']) && isset($link['attributes']['class']))
        $link['attributes']['class'] .= ' first';
      else
        $link['attributes']['class'] = 'first';
    }
    if ( $i == $num_links ) {
      if (isset($link['attributes']) && isset($link['attributes']['class']))
        $link['attributes']['class'] .= ' last';
      else
        $link['attributes']['class'] = 'last';
    }
    

    (I added this block just below the "Automatically add a class to each link and also to each LI" block).

    2. use horizontal padding around Anchors, and use border-right to display pipe-like separator.
    Also, use that 'first' class to display the pipe-like separator before the first link (using border-left).

    Little explanation for the override part: When first/last classes are applied to list-item and not anchor, it's not really possible to portably CSS the left border of the first link, thus I needed to move those classes into anchors.

    -----
    http://bogdan.org.ua/

    shadowdknight’s picture

    Hi,
    Im a newbie,can anyone teach me how to open a primary link in a new window for Drupal 6?
    The link goes to 3rd party application which located in internal server so I can't use extlink module.
    Please Advise,

    Thank you very much

    Jarada’s picture

    Not sure I can find a Drupal 6 version of this, but I've used the following code to theme my links as it provides full support for localisation and path aliasing, while still maintaining full themability. I've included the code on how to add you own classes to links so you can grab hold of them and theme them through the CSS.

    <div id="navigation">
    <?php if (is_array($primary_links)) : ?>
    <ul id="main-nav">
    <?php foreach ($primary_links as $link): ?>
    <li><?php
    
    print l(t($link['title']), drupal_get_path_alias($link['href']),
                  	array('attributes' => array(
                        'class' => 'my_class_name',
                    )));
    
    ?></li>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    </div>