Bootstrap navbar dropdown not working only from one page - php

I have a navigation bar with 3 button, saying Home, Films and Genres. Clicking the Genre should open the dropdown with the list of all Genres generated from database and on click to each genre, genrepage containing the list of films of selected genre shows up. But the Genre button (with dropdown) is not working when I am trying to access it from home page. But it works fine for any other pages. I am using Bootstrap 3.3.7 navbar and any help will be appreciated.
My navbar.php
<?php
/*
array of pages
this builds the navigation list
format:
filename => URL name
*/
$navArray = array(
'index' => 'Home',
'film' => 'Films',
'genre' => 'Genre',
);
?>
//some html code here
<?php
foreach($navArray as $key => $nav){
//assign active class to currently active page
if(strstr($_SERVER['SCRIPT_NAME'], $key))
$active = ' active';
else $active = '';
//if Genre is selected, display dropdown menu
if($nav=='Genre'){
echo '<li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Genre<span class="caret"></span></a>
<ul class="dropdown-menu">';
//generate dropdown from database, giving the list of genres
$queryGenreList="SELECT genre.`name`
from genre;
";
$resultGenreList=$db->query($queryGenreList);
if($resultGenreList->num_rows > 0){
while($row = $resultGenreList->fetch_assoc()){
echo '<li>'.$row["name"].'</li>';
}
}
echo '</ul>
</li>';
}
echo"<li class='nav-item".$active.";'>";
if($nav!='Genre'){
echo"<a class='nav-link' href=".$key.".php>".$nav."</a>";
}
}
?>
</li>

If the bootstrap menu is failing to work when navigating away from the homepage, as it sounds like bootstrap.js isn't running on your subpages for the dropdown to work. Check your network tab > js and see if bootstrap.min.js is being loaded in or view the source.

Related

PHP active section after page refresh

I am trying to create a one page navigation menu in PHP. The reason I want it to be in PHP is because I can add other code later and I want the user to be on the same page even after a refresh.
I have an unordered list with generated list items. The list items are generated with an array.
(I want the key/values separated because the actual code will be different. This is just an example.)
$nav = array(
"home" => "home",
"about" => "about",
"contact" => "contact"
);
echo '<ul>';
foreach( $nav as $id => $name )
{
echo '<li>'.$name.'</li>';
}
echo '</ul>';
// outputs:
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
Then I want some divs to be the sections
foreach( $nav as $s_id => $s_name)
{
echo '<div id="'.$s_id.'">Some section text</div>';
}
Now I need to know how can I set the display to either block or none based on current id active?
While I personally think this is more of a job for JavaScript, in the question and a comment above you specify that you want to do this in PHP. In that case those anchor tags will need to be actual links to the page. For the sake of this example I'll assume the page is called index.php. (If it's something different, just use whatever your page is.)
Update the anchors to be links to the page with query string parameters:
<li>home</li>
<li>about</li>
<li>contact</li>
Then in your PHP code you'd determine which one is "active" by the active query string parameter. Start with a default of "home" in case no parameter is specified (it's the user's first time on the page):
$active = "home";
if (isset($_GET["active"])) {
$active = $_GET["active"];
}
Then when rendering the <div> elements you can specify their style:
foreach( $nav as $s_id => $s_name)
{
echo '<div id="'.$s_id.'" style="display:'.($s_id == $active ? "block" : "none").'">Some section text</div>';
}

why is my featured image from each page not displaying in my menu

First time onstructing a wordpress theme and im trying to group a list of pages to display in on the front page ive added featured images into the themes support and all the pages and menus are set correctly with featured image.
This is the code to display the menu i want it to display the name and the featured image of the page
<div class="row">
<?php
$navMenu = wp_get_nav_menu_items(products); /*/Pass Nav Menu_id or Name*/
foreach ($navMenu as $menu) {
if($menu->menu_item_parent == 0)
{
echo '<div class="col-md-4">' . get_the_post_thumbnail($menu->ID) . '<p>' . $menu->title . '</p></div>';
}
}
?>
</div>
the names of the pages in the menu are showing with their links but the featured image is not appearing
Problem is in
get_the_post_thumbnail($menu->ID)
Because $menu->ID is id of the menu item, not the post/page/category where it is pointing to.
I think you're looking for object_id:
get_the_post_thumbnail($menu->object_id)

CakePHP: How to make a dynamic navigation bar

I have the Navigation Bar in my website like:
Here my code to make it:
<ul class="breadcrumb">
<li>
<i class="icon-home"></i>
Home
<i class="icon-angle-right"></i>
</li>
<li>
<?php echo $this->Html->link($title_for_layout,array('controller'=>'controllers','action'=>'index','full_base'=>true));?>
</li>
</ul>
In this picture, I am working with Staffs Controller. So my link is "Home > Staffs". If I do with Products Controller link will be "Home > Products".
But if I work with Staffs Controller and Action is Add. Means "Staffs/add". I want show it on navigation bar like "Home > Staffs > Add". So How can I do this?
If I have done the #1. The current link will be "Home > Staffs > Add". When I want to back to the index of Staffs. I click to "Staffs" in "Home > Staffs > Add". I have to make the link inside like
$this->Html->link($title_for_layout,array('controller'=>'staffs','action'=>'index')
This link is ok when working with Staffs controller. When I change to Product, Customer it will be broken. How can I make it.
I am using CakePHP 1.3
Sorry I ask simple things like this. I am new in Cake PHP. Thank for help and reading.
You sort of need to address this in a structured way to start with. You need to gather an array of all the links you're going to display in your breadcrumbs, then go through and output it all. Try something like this:
$links = array();
// Add the home URL
$links[] = array(
'icon' => 'icon-home',
'title' => 'Home',
'url' => 'index.html'
);
// Add the controller
$links[] = array(
'title' => ucwords($this->params['controller']),
'url' => $this->Html->url(array('controller' => $this->params['controller'], 'action' => 'index', 'full_base' => true))
);
// Now, conditionally add the next parts where necessary
$param1 = isset($this->params['pass'][0]) ? $this->params['pass'][0] : null;
if($param1) {
$links[] = array(
'title' => ucwords($param1),
'url' => $this->Html->url(array('controller' => $this->params['controller'], 'action' => $this->action, $param1))
);
}
Now you've got a structured array containing the three links you're going to output, so you can output them easily like this:
<ul class="breadcrumb">
<?php
$size = count($links);
foreach($links as $i => $link) : ?>
<li>
<?php
// Output icon if it's set
if(isset($link['icon']))
echo '<i class="' . $link['icon'] . '"></i>'; ?>
// Output the link itself
echo $this->Html->link($link['title'], $link['url']);
// Output the caret if necessary (it's not the last)
if($i < $size - 1)
echo '<i class="icon-angle-right"></i>';
?>
</li>
<?php endforeach; ?>
</ul>
Note:
You might want to find a better way to get the title for your method, at the moment I'm just capitalizing the parameter/controller name.
I haven't tested this, but it should work in theory.
There's probably an inbuilt helper for generating breadcrumbs - have a look.
staff* (singular)

Can I show a list of contacts within category, on the 'List all contact categories' page in Joomla?

So in a nutshell, you have two options for displaying Contacts in Joomla:
Show all Joomla Contact Categories.
Show all Joomla Contacts in a single Category.
I want to use the first option, but merge a list underneath each Category showing the list of contacts within that category, and a link to their profile.
The simplest way I thought of this was to edit a template override of the file com_contact/categories/default_items.php
I found a point where I want the list to appear, and then copied and pasted the code from the Category view (that generates the list of contacts).
<ul>
<?php // Add list of contacts for each category
foreach ($this->items as $i => $item) : ?>
<li>
<a href="<?php echo JRoute::_(ContactHelperRoute::getContactRoute($item->slug, $item->catid)); ?>">
<?php echo $item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
But I am assuming I can't just copy and paste, as there needs to be an extra node added to $this->items.
At the moment, no list is being generated, just the <ul> outside the foreach loop.. but also interestingly, the <li> and the <a> IS being generated.. but linking to the current page I'm on (Probably because $item->slug is still being seen as the category).
So can anyone point me in the right direction as to how to reference the contacts within a category? All I'm after is the name and the slug/URL.
UPDATE:
I saw this in the same file (default_items.php) and although I realise it's referring to child categories... would this be a place to start for the actual contacts within the categories?
<?php if (count($item->getChildren()) > 0) :?>
<div class="collapse fade" id="category-<?php echo $item->id;?>">
<?php
$this->items[$item->id] = $item->getChildren();
$this->parent = $item;
$this->maxLevelcat--;
echo $this->loadTemplate('items');
$this->parent = $item->getParent();
$this->maxLevelcat++;
?>
</div>
<?php endif; ?>
BUMP - Does anyone have any experience with this? Or being able to call individual contacts when viewing a category? How are they linked?
For Category view in file default_children.php after tag <li... add code:
<?php
// Get Category Model data
$categoryModel = JModelLegacy::getInstance('Category', 'ContactModel', array('ignore_request' => true));
$categoryModel->setState('category.id', $child->id);
$categoryModel->setState('list.ordering', 'a.name');
$categoryModel->setState('list.direction', 'asc');
$categoryModel->setState('filter.published', 1);
$contacts = $categoryModel->getItems();
?>
For Custom Fields add this after previus code:
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
foreach($contacts as $contactItem) {
$currentContFields[] = FieldsHelper::getFields('com_contact.contact', $contactItem, true);
}

Adding custom menu item to WordPress menu

I have a WordPress menu that has a few menu items I added through the standard (drag and drop) WordPress admin menu feature. Recently I had to add another item to the menu that generates a dynamic href link. I achieved that using the following code in my functions.php file:
//add my profile menu item dynmacially to the members menu (generate
user name based on current user logged in)
add_filter('wp_nav_menu_items','add_profilelink_in_menu', 10, 2);
function add_profilelink_in_menu( $items, $args ) {
if( $args->theme_location == 'secondary') {
global $current_user;
//converts user id to username
$user_info = get_userdata($current_user->ID);
$items .='<li id="menu-item-2091" class="menu-item menu-item-2091">
Profile
</li>';
}
return $items;
}
My problem is that this menu item is added to the end of the menu and the regular WordPress Menu classes such as 'current-menu-item' don't get applied to this item. Is there a way for me to control the position of where this menu item is added to (For example: add this item after the first two items?)
and how can I get WordPress to treat this dynamically generated menu item as a regular menu item and have it add all the classes that it adds the other menu items (created through the WordPress menu feature)?
Thanks for any help.
Here's the logic that you can base using jquery
//suppose your menu is this
<ul id="secondary_nav">
<li id="li_unique_id_1">menu 1</li>
<li id="li_unique_id_2">menu 2</li>
<li id="li_unique_id_4">menu 4</li>
</ul>
//the jquery workaround
//place this in your footer
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script type='text/javascript'>
$(function(){
<?php
global $current_user;
//converts user id to username
$user_info = get_userdata($current_user->ID);
?>
$("<li id='menu-item-2091' class='menu-item menu-item-209'><a href='https://www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>").insertAfter("#secondary_nav #li_unique_id_2");
});
</script>
You can also use insertBefore function
Did you check Wordpress menu option in the themes->menu for this ? You can easily add menu from there also you can set custom menu from there.Hope it will help you.

Categories