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>
Related
I built a custom block using ACF acf_register_block_type(); function
it works fine in the WordPress dashboard (block editor), it shows the field data and all good..
but in the front-end (view page) the fields are NULL
any help on how to display field data in the front-end??
functions.php code:
add_action('acf/init', 'my_acf_init_block_types');
function my_acf_init_block_types()
{
// Check function exists.
if (function_exists('acf_register_block_type')) {
// register a hero block.
acf_register_block_type(array(
'name' => 'banner',
'title' => __('Banner'),
'description' => __('A custom hero block.'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'home-sections',
'icon' => 'cover-image',
'keywords' => array('basic', 'banner')
));
}
}
function my_acf_block_render_callback($block)
{
// convert name ("acf/hero") into path friendly slug ("hero")
$slug = str_replace('acf/', '', $block['name']);
// include a template part from within the "template-parts/block" folder
if (file_exists(get_theme_file_path("/template-parts/blocks/content-{$slug}.php"))) {
include(get_theme_file_path("/template-parts/blocks/content-{$slug}.php"));
}
}
function wpdocs_mytheme_block_styles()
{
wp_enqueue_style('admin-css', get_stylesheet_directory_uri() . '/build/css/styles.min.css', '', '0.0.3');
wp_enqueue_script('admin-js', get_template_directory_uri() . '/build/js/scripts.min.js', array('jquery'), '0.0.3', true);
}
add_action('enqueue_block_editor_assets', 'wpdocs_mytheme_block_styles')
banner.php code:
<?php
$title = get_field('main_title');
$img = get_field('background_image');
$button = get_field('button');
?>
<section id="banner-section" style=" background-image:linear-gradient( rgba(67, 74, 122, 0.5), rgba(67, 74, 122, 0.5) ), url('<?= $img['url']; ?>');">
<div class="container">
<div class="row justify-content-center">
<div class="col-12 col-lg-10 text-center">
<h1 class="basic-slider-heading"> <?php echo $title ?> </h1>
<?php
if ($button) :
$button_url = $button['url'];
$button_title = $button['title'];
$button_target = $button['target'] ? $button['target'] : '_self';
?>
<button class="btn btn-shadow-malibu btn-bg-malibu-gradient bg-malibu-accent">
<?= $button_title ?>
</button>
<?php endif; ?>
</div>
</div>
</div>
</section>
var_dump( get_field('main_title'));
Note: I'm using WordPress v5.7.2 and ACF PRO v5.9.8
This is the documentation of get_field: https://www.advancedcustomfields.com/resources/get_field/
Note that there is an optional second parameter
$post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.
So, if your function works at a place and not at the other, then there are environmental differences between the two pages. It is possible that on the UI no post is being technically selected, in which case you need to pass the id of the post whose field you intend to get.
I am trying to edit the link to one of the items in the footer in the following tpl file:
<?php if ($informations) : ?>
<div class="col-lg-4 col-md-4 col-xs-6">
<div class="module clearfix">
<h3 class="modtitle"><?php echo $text_information; ?></h3>
<div class="modcontent" >
<ul class="menu">
<?php foreach ($informations as $information) { ?>
<li><?php echo $information['title']; ?></li>
<?php } ?>
</ul>
</div>
</div>
</div>
it seems that they are looping through an array i guess where it says foreach ($informations as $information) { ?, where should i find $informations variable or how can i access the content of these variables?
The $informations defined in this file:
catalog/controller/common/footer.php
line 25, Opencart 2.2.0.0
this part:
$data['informations'] = array();
foreach ($this->model_catalog_information->getInformations() as $result) {
if ($result['bottom']) {
$data['informations'][] = array(
'title' => $result['title'],
'href' => $this->url->link('information/information', 'information_id=' . $result['information_id'])
);
}
}
how can i access the content of these variables?
When you create or edit an information page in admin panel, there's a checkbox labeled Bottom, if you check this option, this information page will be in $informations array.
If you trying to change a link via code, you can modify above code:
foreach ($this->model_catalog_information->getInformations() as $result) {
if ($result['bottom']) {
if($result['title'] == 'About Us'){
$link = "myCustomLink";
} else {
$link = $this->url->link('information/information', 'information_id=' . $result['information_id']);
}
$data['informations'][] = array(
'title' => $result['title'],
'href' => $link
);
}
}
Or by information id:
if($result['information_id'] == 4){
I've been following this post
http://legomenon.io/article/drupal-7-creating-theming-custom-block-content
so i made them accordingly,
i have these, btw name my my custom module is efq_story_list
HOOK_THEME
function efq_story_list_theme() {
return array(
'efq_story_list_function_items' => array(
'variables' => array('items' => NULL),
'template' => 'templates/efq_story_list--block'
),
);
}
FUNCTION GENERATING THE LIST
function __efq_story_list_block_content() {
#SOME CODE HERE and variable $items has the list
return theme("efq_story_list_function_items", array("items" => $items));
}
HOOK_BLOCK_VIEW
The name of the block is opinion
function efq_story_list_block_view($delta = ''){
switch ($delta) {
case 'opinion':
$block['content'] = __efq_story_list_block_content();
break;
}
return $block;
}
Then on my templates/efq_story_list--block i have the file efq_story_list--block.tpl.php.. then i flush all the cache,
but now I get WSOD. I tried this in local machine and its working, so I am wondering what else can I miss in my production site.. Thank you very much.. I am quite new to drupal so I dont know where to look anymore...
EDIT # 1
the content of the template file is
<?php $items = $variables['items']; ?>
<div class="row">
<?php foreach($items as $item): ?>
<div class="col-md-3 news_list panel">
<div class='thumbnail_wrapper'>
<img src="print render($item['image_url']);" />
</div>
<div class="meta small_sprite">
<span class="generic icon source_<?php print render($item['source_class']); ?>"></span>
<span><?php print render($item['source']); ?></span>
</div>
<div class="caption">
<?php print render($item['title']); ?>
</div>
</div>
<?php endforeach;
</div>
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.
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.