am using Advanced Custom Fields for many things on my site. One thing in particular is for a staff profile page. I have a select field where the staff can add social icons or an email icon. The repeater field is 'social' and if they choose to 'add a row' there is 'social channel' select field and a 'social_link' test field. My current code is this:
<?php if ( have_rows('social')): ?>
<div class="staff-social">
<?php while ( have_rows('social')) : the_row() ?>
<li><img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" /></li>
<?php endwhile; ?>
</div><!--end staff-social-->
<?php endif; ?>
I need to prepend a 'mailto:' to my anchor tag if the user selects 'mail' from the 'social_channel' dropdown in the backend. I have tried doing:
<?php while ( have_rows('social')) : the_row() ?>
<li>
<?php $select = get_sub_field_object('social_channel');
$choices = $select['choices'];
foreach ($choices as $choice) {
if ($choice == 'mail') {
echo '<a href="mailto:'.the_sub_field('link').'">';
} else echo '<a href="'.the_sub_field('link').'">';
} ?>
<img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" />
</a>
</li>
<?php endwhile; ?>
But this of course spits something out for all choices, whether or not they are selected by the user in the backend. Can anyone help me with this? I think this is pretty basic PHP but I'm not sure how to do it. Any help will be much appreciated!
Your Select field should return just a single string, not an array, (make sure you set the 'social_channel' field to NOT allow multiple values) so change your code to this:
<?php while ( have_rows('social')) : the_row() ?>
<li>
<?php $select = get_sub_field('social_channel');
if($select == 'mail'){
$linkURL = 'mailto:'.get_sub_field('link');
}else{
$linkURL = get_sub_field('link');
} ?>
<img src="<?= get_template_directory_uri(); ?>/img/footer-<?php the_sub_field('social_channel') ?>.svg" alt="social icon" />
</li>
<?php endwhile; ?>
Related
I've created custom post type as single-service.php and I have several custom taxonomies under service posts like education, recruit and health.
I currently created some queries and under each query, I want to create a button which direct the users the contact form page of that service. It is like when you get into a single service page which is categorized under education there will a button directs them to education contact form page likewise for recruit page.
I have tried below link but somehow it does not work. I am not sure whether I am using correct code or where do I make mistake
<?php if (in_category( 'education', $post->ID )) : ?>
<?php echo '<div class="button">Contact us</div>'; ?>
<?php elseif (in_category('recruit', $post->ID)) :?>
<?php echo '<div class="button">Contact us</div>'; ?>
<?php elseif (in_category('health', $post->ID)) :?>
<?php echo '<div class="button">Contact us</div>'; ?>
<?php endif;?>
I solve my problem by creating two custom fields in the template page and linking them with :
<div class="service-button">
<?php $info = get_post_meta(get_the_ID(), '_post_info', true); if (!$info) $info = array(); ?>
<a class="form" style="background-color:<?php echo $info['color'] ?>" href="<?php echo $info['form'] ?>">Contact us</a>
<?php if ($info['website']) : ?>
<a class="website" style="background-color:<?php echo $info['color'] ?>" href="http://<?php echo $info['website'] ?>"><?php echo $info['website'] ?></a>
<?php endif; ?>
</div>
I have been fixing this store, based on magento. The main problem of this is that Some of the products display on the store view has incorrect size (which is attribute)
If you have a look at the picture, on the grid view (list.phtml) the size is US 16/ UK 18 / EUR 48 while on the single view page (view.phtml) it is US 6/ UK 8/ EUR 38.
Have a look at this link
http://www.sequinqueen.com/shop/in-stock.html
I have gone through the code in the view.phtml and appear that the code request the attribute value correctly according to the code below
<?php
$categories =$_product->getCategoryCollection();
foreach($categories as $_category) {
$cat_arr[] = $_category['entity_id'];
}
if($currentCategoryId==102){
// CATEGORY ID = 102 is IN-STOCK PRODUCT CATEGORY
$sizeStock=$_product->getAttributeText('size');
$day="Ready To Ship";
$searchfabric=$_product->getSearchfabric();
$searchcolor=$_product->getSearchcolor();
$make=$_product->getHandmake();
$handmake="HandMake";
$topname=$_product->getTopname();
<?php $i=$j+1;?>
<?php $j=$j+1;?>
<?php if($j<5){?>
<?php if ($i%$_columnCount==1): ?>
<ul class="products-grid">
<?php endif ?>
<?php if($j==1):?>
<li class="item first" style="height:434px; background:#FFF;">
<?php echo $this->getChildHtml("catalog"); ?>
</li>
<li class="item">
<?php echo $topname; ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(162,324); ?>" width="162" height="324" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
<div style="width:100%; border-bottom:none; height:80px;">
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($make==1){ echo "<p>".$handmake."</p>";}else{}?>
<?php if($currentCategoryId==102){?>
<!-- display size -->
<p><?php echo $sizeStock; ?></p>
<?php } ?>
<p> <?php echo $day; ?></p>
</div>
</li>
so it came to my thought that it might be something in the store backend, somewhere around attribute modification.
Here is how the previous developer sorted and arranged the position of the size values I am not sure if this is the cause of problem.
Anybody is familiar with this problem?
Any answer is appreciated.
thank you in advance.
Did you try to reindex product data from "System"->"Index Management" ?
Please use this
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
I want a field to show if it has a category of either 2 values from 2 different sub fields.
Im not quite sure how to do this though with 2 different subfields.
Here is an exmaple im trying to demo to show how I would like it to work:
<?php while(has_sub_field('team_profile')):
$category = get_sub_field('category');
$category_2 = get_sub_field('category_2');
if($category=='copyclearance')
if($category_2=='anothercat'){ ?>
<li class="col-lg-2 teamProfile">
<img src="<?php the_sub_field('profile_image'); ?>" class="img-responsive"/>
<h2><?php the_sub_field('profile_name'); ?></h2>
<p class="jobTitle"><?php the_sub_field('job_title'); ?></p>
</li>
<?php
}
endwhile; ?>
This is my working version with just one category filter (but I need 2):
<?php while(has_sub_field('team_profile')):
$category = get_sub_field('category');
if ($category=='copyclearance') { ?>
<li class="col-lg-2 teamProfile">
<img src="<?php the_sub_field('profile_image'); ?>" class="img-responsive"/>
<h2><?php the_sub_field('profile_name'); ?></h2>
<p class="jobTitle"><?php the_sub_field('job_title'); ?></p>
</li>
<?php
}
endwhile; ?>
So this would work, by looking to see if anything has been posted in either $category or in $category_2 then displaying all of them results. NOT using the 2 to filter specifically i.e only showing posts that are in both $category and $category_2
Instead of nesting your if statements, use the or operator:
if($category=='copyclearance' || $category=='anothercat') {
// ...
}
If you want to do more than two, I recommend using an array:
$valid_cats = array('copyclearance', 'cat2', 'cat3', 'etc');
if(in_array($category, $valid_cats)) {
// ...
}
Been looking around and am stumped. Basically I'm trying to filter out a result from an xml feed and hide it from displaying in the html output. I'm trying to find out the venue "Hornblower Cruises and Events" and if it exists, hide the whole , which is the parent of all the nodes.
Here's the URL: http://www.sandiegomagazine.com/goldstartest/rss3-exclude.php
Here's my code:
<?php
$myfeed = simplexml_load_file('https://www.goldstar.com/api/listings.xml?api_key=6d0d598c69dfecfcf028b0d2b3c9b693b606ad8c&postal_code=92101');
$i = 0;
foreach ($myfeed as $goldstar):
$title=$goldstar->headline_as_html;
$summary=$goldstar->summary_as_html;
$dates=$goldstar->upcoming_dates->event_date->date;
$ourprice=$goldstar->our_price_range;
$fullprice=$goldstar->full_price_range;
$img=$goldstar->image;
$link=$goldstar->link;
$venue_name=$goldstar->venue->name;
$venue_street=$goldstar->venue->address->street_address;
$venue_locality=$goldstar->venue->address->locality;
$venue_region=$goldstar->venue->address->region;
$venue_zip=$goldstar->venue->address->postal_code;
$venue_phone=$goldstar->venue->phone;
$category=$goldstar->category_list->category->name;
// if ($venue_name == 'Hornblower Cruises and Events'){
// unset($myfeed->event);
//echo $myfeed->asxml();
//}
if (++$i > 20) {
// stop after 10 loops
break;
}
?>
<html>
<head></head>
<body>
<div class="gs-item">
<div class="gs-itemcontent">
<h3 class="gs-cat"><?php echo $category; ?></h3>
<h2><?php echo $title; ?></h2>
<h4 class="gs-date">Date: <?php echo $dates; ?> <br/>For more show dates, click here</h4>
<img src="<?php echo $img; ?>" />
<p><?php echo $summary; ?></p>
<div id="gs-callout">
<span class="fullprice">Full Price: <?php echo $fullprice; ?></span>
<br/>
<span class="ourprice">Our Price: <span class="gs-hilite"><?php echo $ourprice; ?></span></span>
<p><a class="gs-button" href="<?php echo $link; ?>" target="_blank">Buy Tickets ยป</a></p>
</div>
<ul class="gs-venue">
<li><strong><?php echo $venue_name; ?></strong> | </li>
<li><?php echo $venue_street; ?></li>
<li><?php echo $venue_locality; ?>, <?php echo $venue_region; ?> <?php echo $venue_zip; ?></li>
<li><?php echo $venue_phone; ?></li>
</ul>
</div>
<div class="gs-clear"></div>
</div>
<? endforeach; ?>
</body>
Help?
Use a keyed foreach
foreach ($myfeed as $key => $goldstar):
And then unset the entire current xml using the key
unset($myfeed->$key);
Or unset just the venue with unset($myfeed->$key->venue);
Alternately, you could just build a new obj instead of trying to edit the existing one. Instantiate a new object before your loop and then only add the pieces to it that you want to keep.
Or, you can just continue the foreach if you find the unwanted venue. The only downside is that you won't have a copy of your ok'd list in $myfeed. So...
if ($venue_name == 'Hornblower Cruises and Events') { continue; }
Firstly you should cast every object that you are getting from your SimpleXML object to the relevant type. For example you should cast string attributes or nodes with putting (string) before reading the field:
$venue_name = (string)$goldstar->venue->name
For your question about filtering, I'd suggest your own way to keep looking for the match string and then decide to add or not.
EDIT
You're trying to see if the venue name is 'Hornblower Cruises and Events' then unset the current event field and echo it as XML? Firstly you should write unset($myfeed) because it's the event field itself. Next asxml would return nothing if you unset $myfeed. And you have HTML mistake in your code. In every loop of your for, you're writing:
<html>
<head></head>
<body>
You should place them before the for. And there is no closing tag for them. Put the closing tags after for. If you want to just get the fields with not venue name 'Hornblower Cruises and Events', put the <?php if($venue_name != 'Hornblower Cruises and Events') : ?> before <div class="gs-item"> and put <?php endif; ?> after </ul>.
I created an unordered list of custom fields and I wish to hide them if they are empty. For text custom fields I used the code:
<?php if (get_field('phone') != '') { ?>
<li><strong>Phone: </strong><?php the_field('phone'); ?></li>
<?php } ?>
However, I have a custom field which is for images, like this:
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
How can I hide the field if no image was uploaded (obviously, the above code won't work)?
Thanks in advance.
I think it should be
<?php if (get_field('logo') != ''): ?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php endif; ?>
Assuming the_field('logo') will return a falsy value if there are no images
if (the_field('logo')) {
?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php
}
<?php if( get_field('field_name') ): ?>
<p>My field value: <?php the_field('field_name'); ?></p>