How to hide Search Bar along with Wordpress Menu - php

I have a wordpress menu in php, and everything works, however i have a multisite wordpress that has been created, if there is not menu in the Wordpress installation, i want it not to display anything, including the search, my code displays when needed, and when not needed it doesn't display anything but it still displays the search box which is in the menu. Here is my code
<?php $menuClass = 'nav';
$menuID = 'primary-navigation';
$primaryNav = '';
if (function_exists('wp_nav_menu')) {
$primaryNav = wp_nav_menu( array(
'theme_location' => 'primary-nav',
'container' => '',
'fallback_cb' => '',
'menu_class' => $menuClass,
'menu_id' => $menuID,
'echo' => false ));
};
?>
<nav>
<div class="navmenu">
<div class="wrap">
<div id="primary-nav">
<?php echo($primaryNav); ?>
<div id="header-search" role="search"><form action="bloginfo("url");" method="get" id="search-form"><label><input type="text" name="s" id="site_search" placeholder="Search this site..." /></label><input type="submit" id="search-submit" value="Search" /></form></div>
</div>
</div>
</div>
</nav>

EDIT:
Ok, you need to first look if the nav menu exist
<?php
$primaryNav = '';
if ( has_nav_menu( 'primary-nav' ) ) {
$primaryNav = wp_nav_menu( array(
'theme_location' => 'primary-nav',
'container' => '',
'fallback_cb' => '',
'menu_class' => 'primary-navigation',
'menu_id' => 'nav',
'echo' => false ));
}
$search_out = '<div id="header-search" role="search">
<form action="bloginfo("url");" method="get" id="search-form">
<label><input type="text" name="s" id="site_search" placeholder="Search this site..." /></label>
<input type="submit" id="search-submit" value="Search" />
</form>
</div>';
?>
<nav>
<div class="navmenu">
<div class="wrap">
<div id="primary-nav">
<?php ($primaryNav!='') ? _e($primaryNav . $search_out) : '';
?>
</div>
</div>
</div>
</nav>
First you check if there is a nav menu in 'primary-nav' location, and if there is, you print it out. Before that, it's empty. And then you check if it's not empty (!=''). If that condition is true it will echo it out, along with search, if not, it won't. You could probably make an if clause that puts the whole <nav> section appear if there is $primaryNav.
EDIT2:
Your old code might also work, just check that it's not empty, with the code above.

Related

wordpress mega menu items, iterating all items on each level of a menu tree (dropdowns)

I've been re-writing a portion of this mega menu code to better serve menus where we have 3 levels of items. In this case the main menu has a dropdown called "Sports" which then has "basketball" and "football" each of which are dropdowns with their own respective teams so the menu tree looks like this:
-sports
--basketball
---commanders
---chargers
--football
---sharks
---bears
The issue is that this "sports" dropdown then shows everything in a single list and I want to display a column for each sport. My problem lies in the loop where I'm getting li elements because it iterates one by one and gives the football and basketball options the has-children class, which is fine, but I want to iterate on all 2nd level items first (i.e. the football and basketball items) so that I can make those into columns and then just use CSS for the rest.
How can I restructure the list items in order to achieve that properlY?
<?php
/**
* Mega Menu Category - Display recent posts from a parent category.
*/
// Walker to create the left part of mega menu
$sub_walker = new Walker_Nav_Menu;
$have_sub_menu = !empty($sub_items) ? true : false;
if (!isset($sub_items)) {
$sub_items = array();
}
?>
<div class="sub-menu mega-menu mega-menu-a wrap">
<?php if ($have_sub_menu): ?>
<div class="column sub-cats">
<ol class="sub-nav">
<?php foreach ($sub_items as $nav_item): ?>
<?php
ob_start();
// Simulate a simpler walk - $escaped_output is passed-by-ref
$escaped_output = '';
$sub_walker->start_el($escaped_output, $nav_item, 0, $args);
$sub_walker->end_el($escaped_output, $nav_item, $args);
ob_end_clean();
echo $escaped_output; // phpcs:ignore WordPress.Security.EscapeOutput -- Safe markup generated via WordPress default Walker_Nav_Menu::start_el()
?>
<?php endforeach; ?>
<li class="menu-item view-all menu-cat-<?php echo esc_attr($item->object_id); ?>"><a href="<?php echo esc_url($item->url); ?>"><?php
esc_html_e('View All', 'bunyad'); ?></a></li>
</ol>
</div>
<?php endif; ?>
<?php
// Add main item (view all) as default
array_push($sub_items, $item);
$columns = $have_sub_menu ? 4 : 5;
?>
<section class="column recent-posts" data-columns="<?php echo intval($columns); ?>">
<?php foreach ($sub_items as $item): ?>
<div class="posts" data-id="<?php echo esc_attr($item->object_id); ?>">
<?php
echo Bunyad::blocks()->load(
'Loops\Grid',
[
'query_type' => 'custom',
'posts' => $columns,
'cat' => $item->object_id,
'columns' => $columns,
'meta_items_default' => false,
'meta_above' => [],
'meta_below' => ['date'],
'heading_type' => 'none',
'excerpts' => false,
'style' => 'sm',
'space_below' => 'none',
'pagination' => false,
'cat_labels' => false
]
);
?>
</div> <!-- .posts -->
<?php endforeach; ?>
</section>
</div>

How to make TbModal work properly in PhP (Yii)

I am looking to make a simple Pop-Up window using TbModal (as requested by my superiors). However, I have no clue how to do this. I have never written a modal before and I am not certain if I am doing it completely wrong.
All I need to do, is to create a Pop-up window once a button is clicked. In this window I need to simply add a name, a description and something that is called carousel. However, whenever I try to make it work I get the following error:
Alias "bootstrap.widgets.TbModal" is invalid. Make sure it points to an existing directory or file.
Below is what the button in my view looks like (using a TbButtonColumn):
'buttons'=>array(
'update'=>array(
),
'delete'=>array(
),
'clone'=>array(
'id'=>'clone-dashboard-button',
'label'=>'Clone',
'title'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'option'=>array(
'rel'=>'tooltip',
'data-toggle'=>'tooltip',
'title'=>'Clone'
),
),
),
As well as the modal:
<?php $this->beginWidget('bootstrap.widgets.TbModal', array(
'id'=>'clone-form',
// 'focus'=>'#Dashboard_clone_title',
'htmlOptions'=>array(
'tabindex'=>'-1',
))); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h4>Test</h4>
</div>
<div class="model-body">
<div class="row">
<div class ="col-md-2">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textArea($model,'title', array('rows'=>1, 'cols'=>20,'class'=>'resize-non form-control')); // Title input?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description', array('rows'=>1, 'cols'=>30,'class'=>'resize-non form-control')); //Description ?>
</div>
<div class ="col-md-2">
<?php echo $form->labelEx($model,'carousel'); ?>
<?php echo $form->checkbox($model,'carousel'); ?>
</div>
</div>
</div>
<div class="modal-footer">
<!--Buttons-->
<div class="pull-right">
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'Clone',
'url'=>'Yii::app()->createUrl("path/to/actionClone", array("id"=>$data->id))',
'htmlOptions'=>array(
'onClick'=>"
$(this).attr(\"disabled\", \"disabled\");"
),
)); ?>
<?php $this->widget('booster.widgets.TbButton', array(
'buttonType'=>'button',
'label'=>'Cancel',
'htmlOptions'=>array(
'data-dismiss'=>'modal',
),
)); ?>
</div>
</div>
<?php $this->endWidget(); ?>

Decode JSON like text from CakePHP to Codeigniter [duplicate]

This question already has answers here:
How to use php serialize() and unserialize()
(10 answers)
Closed 3 years ago.
I'm tasked to change the framework from CakePHP to Codeigniter. In the database there is this JSON like text in one column in order to define the values of 4 select inputs. The text looks like -
a:4:{i:58;s:1:"1";i:59;s:1:"0";i:60;s:1:"0";i:61;s:1:"0";}
Basically it says a(array size which is 4), i(being the id) and in s(i dont know about the first integer but all of its values is just 1 the second integer with the "" means it is the 0=>'None', 1=>'Yes', 2=>'NaN').
This is the previous programmer's code in CakePHP
<?php
$options_services = array(0=>'None', 1=>'Yes', 2=>'NaN');
?>
<div class="box-body">
<div class="form-group ">
<div class="row">
<div class="col-md-6">
<label> Select Service Category</label>
<div>
<?php echo $this->Form->input('catservice_id', array('options'=>$catservices,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select','empty'=>'Please Select Category')); ?>
</div>
</div>
<div class="col-md-6">
<label> Title <span class="required">*</span></label>
<div>
<?php echo $this->Form->input('title', array('div' => false, 'label' => false, 'class' => 'form-control')); ?>
</div>
</div>
</div>
</div>
<div class="form-group ">
<div class="row">
<?php if(!empty($service_list)): ?>
<?php foreach($service_list as $key=>$values): ?>
<div class="col-md-3">
<label><?php echo $values;?> </label>
<div>
<?php echo $this->Form->input('cleaning.'.$key, array('options'=>$options_services,'div' => false, 'label' => false, 'class' => 'form-control','type'=>'select')); ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<?php
echo $this->Html->link('Back', array('controller' => 'services', 'action' => 'index'), array('escape' => false, 'class' => 'btn btn-info'));
?>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
i certainly means id
i want to use the JSON like text to be able to use this
I tried json_decode in Codeigniter but it doesnt work. You guys have any idea how I can decode this? Would help me a lot. Thank you!
I got it thanks to #GetSet help.
Using unserialize
See this question : How to use php serialize() and unserialize()

Make icons appear if variable is set

I created a config file for a template I am making. I am trying to make the social icons only appear if the variable is set so if they don't enter information for that icon it does not show in the line of icons.
I know that I can do it with an if statement but I would prefer not to have to make 12 if statements (one per icon)
here is the config section for the icons
/* ==========================================================================
Social Media Acct. Information Below
========================================================================== */
/*** INLCUDE FULL URL FOR ALL FIELDS ***/
// Facebook
define('FACEBOOK_URL', '#');
// Twitter
define('TWITTER_URL', '#');
// Linkedin
define('LINKEDIN_URL', '#');
// Pinterest
define('PINTEREST_URL', '#');
// Google Plus
define('GOOGLE_URL', '#');
// Dribbble
define('DRIBBBLE_URL', '#');
// Youtube
define('YOUTUBE_URL', '#');
// Vimeo
define('VIMEO_URL', '#');
// Flickr
define('FLICKR_URL', '#');
// Yelp
define('YELP_URL', '#');
// Stumble Upon
define('STUMBLE_URL', '#');
//Specify icon color below (white or dark)
define('ICON_COLOR', 'white');
and here is the html for each icon
<div id="social_icons">
<div class="icon_container" id="facebook_icon"></div>
<div class="icon_container" id="twitter_icon"></div>
<div class="icon_container" id="linkedin_icon"></div>
<div class="icon_container" id="google_icon"></div>
<div class="icon_container" id="pinterest_icon"></div>
<div class="icon_container" id="dribble_icon"></div>
<div class="icon_container" id="youtube_icon"></div>
<div class="icon_container" id="vimeo_icon"></div>
<div class="icon_container" id="flickr_icon"></div>
<div class="icon_container" id="yelp_icon"></div>
<div class="icon_container" id="stumbleupon_icon"></div>
</div>
I want to be able to accomplish this without placing an if statement around each link. Is there a way? Thanks!
EDIT Here is the new code snippets based on #thtjuan's answer if this helps answer the full question.
PHP
$social_links = array(
'facebook' => '#',
'twitter' => '#',
'linkedin' => '#',
'google' => '#',
'pinterest' => '#',
'dribbble' => '#',
'youtube' => '#',
'vimeo' => '#',
'flickr' => '#',
'yelp' => '#',
'stumbleupon' => '#'
);
HTML
<div id="social_icons">
<?php foreach( $social_links as $icon => $url ): ?>
<div class="icon_container" id="<?php echo $icon?>_icon"></div>
<?php endforeach;?>
</div>
Create an array of links and then use a for loop to print them all:
e.g.
$links = array(
'facebook' => '#',
'twitter' => '#'
);
and then:
<div id="social">
<?php foreach( $links as $icon => $url ): ?>
<div class="icon_container" id="<?php echo $icon?>_icon"></div>
<?php endforeach;?>
</div>
Note that any links that are not in the $links array will not show up at all on your page. If you must have all the entries in the array, then you can keep the ones without a url hidden by doing this:
<div id="social">
<?php foreach( $links as $icon => $url ): ?>
<?php if( strlen($url) && $url != '#' ): ?>
<div class="icon_container" id="<?php echo $icon?>_icon"></div>
<?php endif; ?>
<?php endforeach;?>
</div>

Automatically Update Bootstrap Layout With PHP

I'm trying to generate a layout with php and jQuery which lists certain profiles and organizes them in a certain way.
Specifically, I have an array of program titles and I'd like to list all of the program titles on the page as headers with two program titles per row. Then, I have an array of names which each contain an array of program names. I'd like to list all of the users that have a certain program in their array underneath the relevant program title. Additionally, the names start off hidden and each program title has a button underneath it which hides and shows tutors. I've created this layout using Bootstrap here:
<?php
include ('objects.php');
function tutorCreator($tutorName, $boxNum, $spanClass){
include ('objects.php');
echo "<div class='" . $spanClass . " tutorBox" . $boxNum . "'>";
}
?>
<div class="row-fluid">
<span class="programHeader span5">DreamWeaver</span>
<span class="programHeader offset1 span5">GIMP</span>
</div>
<div class="row-fluid">
<span class="span2 showTutors">Show tutors</span>
<span class="span2 offset4 showTutors">Show tutors</span>
</div>
<div class="row-fluid">
<?php
tutorCreator('Kevin Marks', '1', "span4");
tutorCreator('Kevin Marks','2', "span4 offset2");
?>
</div>
<div class="row-fluid">
<?php
tutorCreator('Helen Larson', "1", "span4");
tutorCreator('Helen Larson','2', "span4 offset2");
?>
</div>
<div class="row-fluid">
<span class="programHeader span5">Google Analytics</span>
<span class="programHeader offset1 span5">HootSuite</span>
</div>
<div class="row-fluid">
<span class="span2 showTutors">Show tutors</span>
<span class="span2 offset4 showTutors">Show tutors</span>
</div>
<div class="row-fluid">
<?php
tutorCreator('Ken Gato', '3', "span4");
tutorCreator('Julien Mans', '4', "span4 offset2");
?>
</div>
<div class="row-fluid">
<?php
tutorCreator('Ommed Kosh', '3', "span4");
?>
</div>
The classes added to the tutor names are for the purpose of being shown when the appropriate "show tutors" button is listed and to help with the layout.
The objects.php file contains the arrays with the list of tutors and the list of programs. This layout works fine. However, I'm trying to change the php code so that this layout is automatically updated each time I add a program or a tutor to the arrays in objects.php. I've been trying to figure it out for a few days and I just can't get an automatically updated layout which works well. I realize this is probably too broad a question but I've hit a road block so any help would really be appreciated. You can see a fuller version of the current layout here: http://www.tutorgrams.com/tutors. Thanks for any help.
Here is the objects.php file (with some tutors and programs removed for brevity):
<?php
$programs = array();
$programs['DreamWeaver'] = array(
'name' => 'DreamWeaver');
$programs['GIMP'] = array(
'name' => 'GIMP');
$programs['Google Analytics'] = array(
'name' => 'Google Analytics');
$programs['HootSuite'] = array(
'name' => 'HootSuite');
$tutors['Helen Larson'] = array(
'name' => 'Helen Larson',
'programs' => array('DreamWeaver', 'GIMP')
);
$tutors['Kevin Marks'] = array(
'name' => 'Kevin Marks',
'programs' => array('DreamWeaver', 'GIMP')
);
$tutors['Ommed Kosh'] = array(
'name' => 'Ommed Kosh',
'programs' => array('Google Analytics')
);
$tutors['Julien Mans'] = array(
'name' => 'Julien Mans',
'programs' => array('HootSuite')
);
$tutors['Ken Gato'] = array(
'name' => 'Ken Gato',
'programs' => array('Google Analytics')
);
?>
How does this look?
<?php
$numProgs = count($programs);
$progs = array_keys($programs);
$i = 0; ?>
<?php while($i < $numProgs): ?>
<div class="row-fluid">
<span class="programHeader span5"><?php echo $progs[$i]; ?></span>
<span class="programHeader offset1 span5"><?php echo $progs[$i + 1]; ?></span>
</div>
<div class="row-fluid">
<span class="span2 showTutors">Show tutors</span>
<span class="span2 offset4 showTutors">Show tutors</span>
</div>
<div class="row-fluid">
<?php foreach($tutors as $name => $data) {
$tutProgs = $data['programs'];
if(in_array($progs[$i], $tutProgs)) {
tutorCreator($name, $i, "span4");
}
} ?>
</div>
<div class="row-fluid">
<?php
foreach($tutors as $name => $data) {
$tutProgs = $data['programs'];
if(in_array($progs[$i + 1], $tutProgs)) {
tutorCreator($name, $i + 1, "span4 offset2");
}
}
?>
</div>
<?php $i += 2; ?>
<?php endwhile; ?>
It could probably be optimised a little but it seems to do the trick for me. The only thing is I don't have your themes so It might not look right yet.

Categories