I am using ACF on WordPress.
I have made a repeater field. Every fields work fine, except links.
The code below shows the name of URL, but the name has no link!
<?php if( have_rows('dl_box') ): ?>
<ul>
<?php while( have_rows('dl_box') ): the_row();
// vars
$content = get_sub_field('dl_link_name');
$link = get_sub_field('dl_url');
?>
<li>
<span class="link">
<?php if( $link ): ?>
<a href="<?php echo $url; ?>">
<?php endif; ?>
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</span>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
I think it because of this line
<a href="<?php echo $url; ?>">
but I do not know how to fix it.
Modify the markup as follows. You are attempting to access variables that haven't been declared, and the logic is out of sequence:
<li>
<span class="link">
<?php
// $link is the URL (from "dl_url")
// If there is a URL, output an opening <a> tag
if( $link ) {
echo '<a href="' . $link . '">';
}
// $content is the name (from "dl_link_name")
// always output the name
echo $content;
// If there is a URL, need to output the matching closing <a> tag
if( $link ) {
echo '</a>';
}
</span>
</li>
Note:
I have learned to dislike markup / logic like that - it doesn't make a lot of sense. I'd rather do something like this - it's simpler, easier to read, and more compact:
<li>
<span class="link">
<?php
// if there is a url, output the ENTIRE link
if ( $link ) {
echo '' . $content . '';
// otherwise just output the name
} else {
echo $content;
} ?>
</span>
</li>
Related
I am customizing the wordpress woocommerce product category page. I successfully added an image inline with the product name. My issue arises when I try to make the images linkable. The formatting of the image gets lost as they display bigger, not inline with the product name and with a border. More details below, can anyone help?
This is the current code:
global $product;
$link = get_field('product_fiche');
$label = get_field('energy_label');
$full = get_field('full_energy_label');
?>
<div class="price-wrapper <?php if($link || $label) { echo 'has-col'; } ?>">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
<?php if(is_product_category() && $link || $label) : ?>
<span class="product-action">
<?php if($link) { echo '<a class="link" href="'.$link.'" target="_blank">Product Fiche</a>'; } ?>
<?php if($label) { echo '<img class="energy-label" src="'.$label.'" alt="energy label" />'; } ?>
</span>
<?php endif; ?>
</div>
This is how the category pages appear: (please note how the icons appear inline with the product name)
enter image description here
This is the updated code, I formatted the addition in bold.
global $product;
$link = get_field('product_fiche');
$label = get_field('energy_label');
$full = get_field('full_energy_label');
?>
<div class="price-wrapper <?php if($link || $label) { echo 'has-col'; } ?>">
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
<?php if(is_product_category() && $link || $label) : ?>
<span class="product-action">
<?php if($link) { echo '<a class="link" href="'.$link.'" target="_blank">Product Fiche</a>'; } ?>
<?php if($label) { echo '****<img class="energy-label" src="'.$label.'" alt="energy label" />****'; } ?>
</span>
<?php endif; ?>
</div>
The above code leads to the below:
enter image description here
I have a foreach loop to display all the tags of my project. Each Tag must have a colour associated with it, and I am using ACF to create this field on the tags dashboard.
However, when I try to display the value from color picker inside my foreach loop it just doesn't work.
I just can't see what I am doing wrong.
Here is my php code:
<?php
$tagslist = get_tags();
foreach($tagslist as $tag) {
?>
<li>
<p><?php echo get_field('tag_color'); ?></p>
<a class="tag-list_item theme-<?php echo $tag->slug; ?>" data-tag="<?php echo $tag->term_id; ?>" href="<?php echo get_tag_link($tag->term_id); ?>" stye>
<?php echo $tag->name; ?>
</a>
</li>
<?php }
?>
The name of the field that I made is "tag_color".
You can use below code to get field from tags:
<?php
$tagslist = get_tags();
foreach($tagslist as $tag) {
?>
<li>
<p><?php echo get_field('tag_color', 'post_tag_'.$tag->term_id); ?></p>
<a class="tag-list_item theme-<?php echo $tag->slug; ?>" data-tag="<?php echo $tag->term_id; ?>" href="<?php echo get_tag_link($tag->term_id); ?>" stye>
<?php echo $tag->name; ?>
</a>
</li>
<?php } ?>
Below is the reference link:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
I am trying to give an alternate class to each LI foreach.
i.e.
<li class="odd">
text
</li>
<li class="even">
text
</li>
<li class="odd">
text
</li>
<li class="even">
text
</li>
This is my code:
<ul>
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Please can anyone help...
Thanks
try :
<?php $count = 0; // need to set first value
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php if($extraField->value): ?>
<li class="<?php echo (++$count % 2) ? "odd" : "even"; ?> type <?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
Text here
</li>
<?php endif; ?>
<?php endforeach; ?>
Link to pre/post increment docs if needed
I would do it that way .. it's key independent and you better see what the li-class will look like.
<ul>
<?php
foreach ($this->item->extra_fields as $key=>$extraField) {
if($extraField->value) {
$toggle = ($toggle=="odd"?"even":"odd");
echo '<li class="',$toggle,' type',ucfirst($extraField->type),' group', $extraField->group,'">';
echo 'Text here';
echo '</li>';
}
}
?>
</ul>
jQuery can easily do this if that's an option for you. It's :odd and :even selectors should do just what you're looking for without too much hassle.
I know this is old, but I humbly present a cleaner solution:
<ul>
<?php
$odd = true;
foreach ( $this->item->extra_fields as $key => $extraField ) {
// Output template.
$template = '<li class="%1$s">%2$s</li>';
// Create our classes.
$classes = [
( $odd ) ? 'odd' : 'even',
'type' . ucfirst($extraField->type),
'group' . $extraField->group
];
// Create our class string.
$class_string = implode( ' ', $classes );
// Print our content out.
printf( $template, $class_string, 'Text here' );
// Flip the classname for next time.
$odd = ! $odd;
}
?>
</ul>
It could of course be cleaner still, by using fewer variables. This is just my style.
I'm trying to create a link based on a URL variable using only the last five digits of its respective line of XML data.
For example, if the XML link is http://events.stanford.edu/events/213/21389 how can I create this link a href="e/?i=21389?
Here's my page, XML and code:
<?php
// Build the XML file path, using URL variable $c (above)
$c = $_GET['c'];
$p ="http://events-prod.stanford.edu/xml/byCategory/";
$e = "/mobile.xml";
$file = "$p$c$e";
$xml = simplexml_load_file($file);
?>
<h1><?php echo $xml->title; ?></h1>
Home
</div><!-- /header -->
<div data-role="content">
<?php // Only display if there are events ?>
<?php if (isset($xml->Event->title)) { ?>
<ul data-role="listview">
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->locationText; ?></p>
</a>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<?php echo '<p>There is currently nothing scheduled for ', $xml->title, '.</p>';?>
<?php } ?>
I'm using short tags, because I think you'll agree it's easier now to read the code as oppose to before.
<?
$controller ="http://events-prod.stanford.edu/xml/byCategory/";
$category_id = $_GET['c'];
$category_id = 0;
$xml = "/mobile.xml";
$url = $controller . $category_id . $xml;
$xml_object = simplexml_load_file($url);
?>
<div>
<h1><?= $xml_object->title ?></h1>
Home
</div>
<div data-role="content">
<? if (!empty($xml_object->Event->title)): ?>
<ul data-role="listview">
<? foreach($xml_object->Event as $event): ?>
<li>
<?
$pattern = '/[0-9]+$/';
$matches = array();
preg_match($pattern, $event->link, $matches);
$my_link = 'e/?i=' . $matches[0];
?>
<a href="<?= $my_link ?>">
<? if (!empty($event->Media->url)): ?>
<img src="<?= $event->Media->url ?>" alt="<?= $event->title ?> thumbnail" />
<? endif; ?>
<h3><?= $event->title ?></h3>
<p><strong><?= $event->beginDate ?> at <?= $event->beginTime ?></strong></p>
<p><?= $event->locationText ?></p>
</a>
</li>
<? endforeach; ?>
</ul>
<? else: ?>
<? echo '<p>There is currently nothing scheduled for ', $xml_object->title, '.</p>'; ?>
<? endif; ?>
</div>
I created an unordered list with the following html:
<ul id="infoBox">
<li class="facebook"><?php the_title(); ?> on Facebook</li>
<li class="twitter">Follow <?php the_title(); ?> on Twitter</li>
<li class="youtube">Watch <?php the_title(); ?> on Youtube</li>
</ul>
<?php echo get_field('value'); ?> is grabbing a string from the backend of my site. Sometimes, I do not have a string to display, so I want to create a conditional statement in jquery and/or php that basically says: If there is no field to get (if the field is left empty on the backend), do not display the list item at all. For instance, if a band doesn't have a youtube page, do not display the list item with a class of 'youtube' at all.
Any idea how I would go about this?
<ul id="infoBox">
<?php $response = get_field('facebook_page');
if(!empty($response)): ?><li class="facebook"><?php the_title(); ?> on Facebook</li><?php endif; ?>
<?php $response = get_field('twitter_page');
if(!empty($response)): ?><li class="twitter">Follow <?php the_title(); ?> on Twitter</li><?php endif; ?>
<?php $response = get_field('youtube_page');
if(!empty($response)): ?><li class="youtube">Watch <?php the_title(); ?> on Youtube</li><?php endif; ?>
</ul>
Would this work for you?
if(get_field('value') == '' || is_null(get_field('value'))
echo 'no value';
else
echo 'there is a value';
I hope understand your question and could help.
<ul id='infoBox'>
<?php
$name = the_title();
$potentialItems = array('facebook' => "$name on facebook",
'youtube' => "Watch $name on youtube",
'twitter' =? "Follow $name on twitter");
foreach($potentialItems as $k=>$v)
{
$gf = get_field($k.'_page');
if($gf)
{
echo "<li class='$k'><a href='$gf'>$v</a></li>";
}
}
?>
</ul>