I am trying to integrate boostrap and I am facing something I just don't understand very well... PHP. Basically What I need is to add class="nav" to a ul and class="dropdown-menu" to the submenu ul but it's repeating twice the nav class. Here is the code. Thanks so much for the help!
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<div class="navbar">
<!-- menu start -->
<ul class="nav" id="nav">
<li class="level0"> <a class="dropdown-toggle" href="#" class=""><span>Dashboard</span></a></li>
<li onmouseover="Element.addClassName(this,'over')" onmouseout="Element.removeClassName(this,'over')" class=" active parent dropdown level0"> <a class="dropdown-toggle" href="#" onclick="return false" class="active"><span>Sales</span></a>
<ul class="nav">
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Orders</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Invoices</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Shipments</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Credit Memos</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Transactions</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Recurring Profiles (beta)</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Billing Agreements</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Terms and conditions</span></a></li>
<li onmouseover="Element.addClassName(this,'over')" onmouseout="Element.removeClassName(this,'over')" class=" parent dropdown last level1"> <a class="dropdown-toggle" href="#" onclick="return false" class=""><span>Tax</span></a>
<ul class="nav"> <!--Need to add .dropdown-menu here-->
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Manage Tax Rules</span></a></li>
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Manage Tax Zones & Rates</span></a></li>
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Import / Export Tax Rates</span></a></li>
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Customer Tax Classes</span></a></li>
<li class="last level2"> <a class="dropdown-toggle" href="#" class=""><span>Product Tax Classes</span></a></li>
</ul>
</li>
</ul>
</li>
<!--THE PHP-->
<?php
public function getMenuLevel($menu, $level = 0)
{
$html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
. 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
. (!$level && !empty($item['active']) ? ' active' : '') . ' '
. (!empty($item['children']) ? ' parent dropdown' : '')
. (!empty($level) && !empty($item['last']) ? ' last' : '')
. ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
. (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
. (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
. ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
. $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
if (!empty($item['children'])) {
$html .= $this->getMenuLevel($item['children'], $level + 1);
}
$html .= '</li>' . PHP_EOL;
}
$html .= '</ul>' . PHP_EOL;
return $html;
}
I can't test it well, since I don't have a space to put all those fields and some other parts, but I see you have a recursively called the function. This will cause a second 'id="nav"'.
public function getMenuLevel($menu, $level = 0)
{
/* declared id="nav" here */
$html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
. 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
. (!$level && !empty($item['active']) ? ' active' : '') . ' '
. (!empty($item['children']) ? ' parent dropdown' : '')
. (!empty($level) && !empty($item['last']) ? ' last' : '')
. ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
. (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
. (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
. ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
. $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
if (!empty($item['children'])) {
/* this line calls the function again which will cause id="nav" to happen again. */
$html .= $this->getMenuLevel($item['children'], $level + 1);
}
$html .= '</li>' . PHP_EOL;
}
$html .= '</ul>' . PHP_EOL;
return $html;
}
One guess to fix it might be to better check what is $level, because I think your !$level might be giving a wrong answer.
public function getMenuLevel($menu, $level = 0)
{
$idnav = '';
if ($level === 0)
$idnav = 'id="nav"';
$html = '<ul class="nav" ' . $idnav . '>' . PHP_EOL;
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
.....
One other idea could be a nested loop. Maybe something like below:
<?php
public function getMenuLevel($menu, $level = 0)
{
$html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
/*notice*/
while (!empty($item['children'])) {
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
. 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
. (!$level && !empty($item['active']) ? ' active' : '') . ' '
. (!empty($item['children']) ? ' parent dropdown' : '')
. (!empty($level) && !empty($item['last']) ? ' last' : '')
. ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
. (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
. (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
. ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
. $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
}
/* here is the tricky part */
$html .= '</li>' . PHP_EOL;
$html .= $this->getMenuLevel($item['children'], $level + 1);
}
$html .= '</ul>' . PHP_EOL;
return $html;
}
?>
You can work out the details of the tricky part with trial and error to make the result end the list correctly.
Related
I am having an issue in incrementing the class="accordion-collapse collapse show so that the first accordion tab should open and the closed. This is how I have doneclass="accordion-collapse collapse '.if($i++){.'show'.}.'" . I am do it in a while loop.
if ( $the_query->have_posts() ) {
echo '<div class="accordion" id="accordionPanelsStayOpenExample">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading'.get_the_ID().'">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse'.get_the_ID().'" aria-expanded="true" aria-controls="panelsStayOpen-collapse'.get_the_ID().'">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse'.get_the_ID().'" class="accordion-collapse collapse '.($i++.'show'.).'" aria-labelledby="panelsStayOpen-heading'.get_the_ID().'">
<div class="accordion-body">
'
.the_content() . '
</div>
</div>';
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}
I am expecting the first according to open and the rest closed
Try this :-
EDITED.
<?php
$flag = true;
if ($the_query->have_posts()) {
echo '<div class="accordion" id="accordionPanelsStayOpenExample">';
while ($the_query->have_posts()) {
$the_query->the_post();
$show_class = $flag == true ? 'show' : '';
$collapsed = $flag == true ? '' : 'collapsed';
echo '<div class="accordion-item">';
echo ' <h2 class="accordion-header" id="panelsStayOpen-heading' . get_the_ID() . '">
<button class="accordion-button '.$collapsed.'" type="button" data-bs-toggle="collapse" data-bs-target="#panelsStayOpen-collapse' . get_the_ID() . '" aria-expanded="true" aria-controls="panelsStayOpen-collapse' . get_the_ID() . '">'
. get_the_title() . '
</button>
</h2>';
echo '<div id="panelsStayOpen-collapse' . get_the_ID() . '" class="accordion-collapse collapse ' . $show_class . ' " aria-labelledby="panelsStayOpen-heading' . get_the_ID() . '">
<div class="accordion-body">
'
. the_content() . '
</div>
</div>';
$flag = false;
}
echo '</div>';
echo '</div>';
} else {
// no posts found
}
this is my code :
$output .=
'<li class="recentcomments recent-comment">
<div class="wbrc-header">
<span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
<div class="wbrc-header-AD">
<span class="wbrc-header-author">' . $author . '</span>
<span class="wbrc-header-date">' . $date . '</span>
</div>
</div>
<div class="wbrc-body">
<span class="wbrc-body-comment">' . $comment_text . '</span>
</div>'.
if( ($key + 1) != $comments->count() ) {
echo '<hr>';
} .
'</li>';
in this line - i want add "hr tag" in all "li tag" - except last li - but this line have error - what i must do?
if( ($key + 1) != $comments->count() ) {
echo '<hr>';
}
You need to move your condition outside the concatenation.
// here i am using ternary operator for your condition.
$condition = (($key + 1) != $comments->count() ? '<hr>' : '');
Then, you can use like that:
$output .=
'<li class="recentcomments recent-comment">
<div class="wbrc-header">
<span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
<div class="wbrc-header-AD">
<span class="wbrc-header-author">' . $author . '</span>
<span class="wbrc-header-date">' . $date . '</span>
</div>
</div>
<div class="wbrc-body">
<span class="wbrc-body-comment">' . $comment_text . '</span>
</div>'.$condition. // change this part
'</li>';
Using IF condition with concatenation (.) will produce "syntax error, unexpected 'if'".
im generating a bootstrap navbar using codeigniter, its working fine but i want the admin link to be aligned to the right. here is my code:
function get_menu ($array, $child = FALSE)
{
$CI =& get_instance();
$str = '';
if (count($array)) {
$str .= $child == FALSE ? '<ul class="nav navbar-left navbar-custom">' . PHP_EOL : '</ul>' . PHP_EOL;
foreach ($array as $item) {
$active = $CI->uri->segment(1) == $item['slug'] ? TRUE : FALSE;
if (isset($item['children']) && count($item['children'])) {
$str .= $active ? '<li class="dropdown active">' : '<li class="dropdown">';
$str .= '<a class="dropdown-toggle" data-toggle="dropdown" href="' . site_url(e($item['slug'])) . '">' . e($item['title']);
$str .= '<b class="caret"></b></a>' . PHP_EOL;
$str .= get_menu($item['children'], TRUE);
}
else {
$str .= $active ? '<li class="active">' : '<li>';
$str .= '' . e($item['title']) . '';
}
// Closing tags
$str .= '</li>' ;
}
//add dashboard link to the right of menu for login
$str .= '<li>' . '<p class="navbar-text navbar-right"><a
`href="admin/dashboard" class="navbar-link"> ADMIN</a></p></li>' . PHP_EOL;
$str .= '</ul>' . PHP_EOL;
}
im getting a navbar with required links but its all inline. any help is aappreciated. Cheers
This is because of where you are adding the navbar-right class. You have to add it to an ul element. Change your second to last string:
$str .= '<li>' . '<p class="navbar-text navbar-right"><a `href="admin/dashboard" class="navbar-link"> ADMIN</a></p></li>' . PHP_EOL;
To something along these lines:
<ul class="nav navbar-nav navbar-right">
<li> ADMIN</li>
</ul>
And do not add it inside of your current ul element, that will also not work; you need to close the navbar-left class then add the navbar-right.
Looking something like this:
UPDATE
I think I got it, you do not have the nav element around your navbar, and that is probably what is causing the issues with alignments...
<?php
function get_menu ($array, $child = FALSE)
{
$CI =& get_instance();
$str = '';
if (count($array)) {
$str .= $child == FALSE ? '<nav class="navbar navbar-default"> <ul class="nav navbar-nav navbar-custom">' . PHP_EOL; // create the <nav> element
foreach ($array as $item) {
$active = $CI->uri->segment(1) == $item['slug'] ? TRUE : FALSE;
if (isset($item['children']) && count($item['children'])) {
$str .= $active ? '<li class="dropdown active">' : '<li class="dropdown">';
$str .= '<a class="dropdown-toggle" data-toggle="dropdown" href="' . site_url(e($item['slug'])) . '">' . e($item['title']);
$str .= '<b class="caret"></b></a>' . PHP_EOL;
$str .= get_menu($item['children'], TRUE);
}
else {
$str .= $active ? '<li class="active">' : '<li>';
$str .= '' . e($item['title']) . '';
}
// Closing tags
$str .= '</li>' ;
}
$str .= '</ul>' . PHP_EOL;
//add dashboard link to the right of menu for login
$str .= '<ul class="nav navbar-nav navbar-right">
<li> ADMIN</li>
</ul>' . PHP_EOL;
// add the nav closing tag
$str .= '</nav>' . PHP_EOL;
}
?>
I'm developing a News website in Wordpress, and I need to show in a bootstrap carousel the first three posts, my problem is that I need to add the "active" class only at the first of the three elements, but really don't know how to. Here's my code:
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
?>
I've already tried a answer found on this site (this one):
$isFirst = true;
foreach ($recent_posts as $recent) {
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ": <strong>" .$recent["post_title"] . '</strong></a></div>';
$isFirst = false;
?>
but it just printed me the "active" words.
Thanks for your help
You need to set $i so that you can count how many times you have gone through the loop and do some logic with it, like in my example below. Instead of having two lines of code that are nearly identical like I have done below though, you should be able to do the if conditional right around the class active. I didn't do that so you could clearly see the conditional and the count of the loops through the array.
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
$i = 0;
foreach ($recent_posts as $recent) {
if ($i == 0) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
} else {
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
$i++;
}
?>
This is my first time using stackoverflow, so I apologize if I did anything incorrectly. The title says it all: I need to insert a target="_blank" (or make two of these six links open in a new window/tab) into this php echo statement without removing any of the php code. The second and fourth list items are the links I'm trying to change. I'm using TomatoCart if that helps at all.
<?php
echo '<li ' . ($osC_Template->getGroup() == 'index' && $osC_Template->getModule() == 'index' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_DEFAULT, 'index'), $osC_Language->get('home')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link('http://www.acehardwaretexas.com/files/circular/index.html'), $osC_Language->get('promos')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_INFO, 'articles&articles_id=1'), $osC_Language->get('services')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link('https://www.acehardware.com/acerewards'), $osC_Language->get('rewards')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_INFO, 'articles&articles_id=2'), $osC_Language->get('hours')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'contact' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_INFO, 'contact'), $osC_Language->get('contact_us')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>';
?>
You could run str_replace over the osc_link_object function and replace <a with <a target="_blank"
For example, the second link could be
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . str_replace("<a ", "<a target='_blank' ", osc_link_object(osc_href_link('http://www.acehardwaretexas.com/files/circular/index.html'), $osC_Language->get('promos'))) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
Replaced link objects with anchor tags.
<?php
echo '<li ' . ($osC_Template->getGroup() == 'index' && $osC_Template->getModule() == 'index' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="index">home</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="http://www.acehardwaretexas.com/files/circular/index.html">promos</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span<a target="_blank" href="articles&articles_id=1">services</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="https://www.acehardware.com/acerewards">rewards</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="articles&articles_id=2">hours</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'contact' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="contact">contact_us</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>';
?>