I've been racking my brain on this one.
Background:
The following code works, and is grabbing the correct taxonomy term information for the relevant WordPress post.
Problem:
ACF is outputting terms in order of newest to oldest.
WordPress's $game->name; is outputting terms in order of oldest to newest.
This basically means that the tooltip is not matching the image from get_field('icon');
$games = get_the_terms(get_the_ID(), 'games')
foreach ($games as $game) {
$term = array_pop($games);
if ( get_field('icon', $term) ) {
echo '<img src="' . get_field('icon', $term ) . '" alt="' . $game->name . '" data-placement="bottom" data-toggle="tooltip" title="' . $game->name . '" />';
}
}
I have so far tried:
Updating $games to $games = get_the_terms(get_the_ID(), 'games', array( 'order' => 'DESC') ) (no impact).
Reversing the order of the array for $games using array_reverse.
Suggestions would be enormously appreciated.
You appear to be deliberately doing this in your code by using array_pop??
Simply use the $game variable from your foreach loop:
$games = get_the_terms(get_the_ID(), 'games')
foreach ($games as $game) {
//$term = array_pop($games);
if ( get_field('icon', $game) ) {
echo '<img src="' . get_field('icon', $game ) . '" alt="' . $game->name . '" data-placement="bottom" data-toggle="tooltip" title="' . $game->name . '" />';
}
}
Related
I'm trying to separate the Featured Image from the Products Variation Gallery.
I was able to split the Featured Image, thanks to a custom shortcode placed in function.php, and I even managed to do an on-click action - Lightbox as well as a custom size option.
However, for the past few days I've been having trouble setting up the same feature but for the variation products gallery.
The code that I used to output the featured photo is this:
global $product;
$title = $product->get_name();
$image_id = $product->get_image_id();
$image_url = wp_get_attachment_url( $image_id );
$image_src = wp_get_attachment_image_src( $image_id, array( 740, 360 ) );
echo '<center><a href="' . $image_url . '" data-lightbox="variation-product" data-title="' . $product->get_name() . '">';
echo '<img src="' . $image_src[0] . '" width="' . $image_src[1] . '" height="' . $image_src[2] . '" alt="' . $product->get_name() . '">';
echo '</a></center>';
And this one is for the variation products gallery:
global $product;
$attachment_ids = $product->get_gallery_image_ids();
foreach ( $attachment_ids as $attachment_id ) {
$image_url = wp_get_attachment_url( $attachment_id );
$image_src = wp_get_attachment_image_src( $attachment_id, array( 740, 360 ) );
echo '<center><a href="' . $image_url . '" data-lightbox="variation-product" data-title="' . $product->get_name() . '">';
echo '<img src="' . $image_src[0] . '" width="' . $image_src[1] . '" height="' . $image_src[2] . '" alt="' . $product->get_name() . '">';
echo '</a></center>';
}
And this is how it looks. This is the main problem. I tried to limit the picutres like this:
$i = 0;
foreach ( $attachment_ids as $attachment_id ) {
if ($i == 1) break;
/* the rest of the code */
$i++;
}
and this is the final result. You wonder where the problem is? Well... There is.
This variant of the code completely removes all variation images. The result I am trying to achieve is:
When we have more than one variable image, only the first one is shown on the product page. When the first variation photo is opened in the Lightbox, all other photos should be visible.
How can I do that? The theme that I'm using is Woodmart, by XTemos.
Easy one, but I can't find an answer.
I wanna to list sub-categories to create tabs in code below. This one works fine in my output. But the first tab needs a <li class= "selected" > to show the content on page load .
How to force the first echo result to include a class on <li> element ?
.
.
.
[ Output ]
Actual: .......... Category 1 | Category 2 | Category 3
Needed: .......... Category 1* | Category 2 | Category 3
*class="selected"
<ul class="cd-tabs-navigation">
<?php
$argumentos = array(
'hide_empty' => 1,
'child_of' => 44,
);
$categories = get_categories($argumentos);
foreach($categories as $category) {
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
?>
</ul>
.
.
[ EDIT ]
The solution by Lal. Thank you everyone!
foreach($categories as $category) {
if(++$i==1)
echo '<li class="selected"><a class="selected" data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
else
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
I would suggest you to use a counter.. Not sure if this is the right way to do it..
Change your foreach as follows
$i=0;
foreach($categories as $category) {
if($i==0)
echo '<li class="selected"><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
else
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
$i++;
}
OR
As suggested in the answer here, you could use array_shift() to process the first item in the array separatley.
That is, do it as below
$first = array_shift($categories);
echo '<li class="selected"><a data-content="' . $first ->slug . '" href="#' . $first ->slug . '">' . $first ->name . '</a></li>';
foreach($categories as $category) {
echo '<li><a data-content="' . $category->slug . '" href="#' . $category->slug . '">' . $category->name . '</a></li>';
}
Read more about array_shift() in the docs
Lal's solution will work fine (as long as you increment $i), but here's a simpler way to do it without an extra variable for the counter :
foreach($categories as $index => $category){
if(!$index) echo "This is the first element";
else echo "This is not the first element";
}
(not sure if this should be an edit or an answer since it's basically the same method but with a slightly simpler synthax, I'll post as an answer for now)
You can use this syntax for foreach. Using this syntax you have in $k the index of the iteration, while in $v the value of the array you're iterating. So if $k == 0 you're in the first iteration.
$a = array("first", "second", "third", "fourth");
foreach ($a as $k=>$v) {
echo $k . "--->" . $v ;
if($k == 0){
echo "First iteration!";
//Do your stuff
}
echo "<br>";
}
extendeing may last question about how to change the display of meta on single custom post type, with a great thanks to TimRDD for his helpful answer, now i have another question.
the working generating code
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}
}
}
}
?>
displays the terms in a single row but without commas between them such as :
(- Proceeding2015 - keywordsbusinessconsumerresearch).
I need your help please to put (:) after every set of terms and commas between terms to display them such as :
(- proceeding : 2015 - keywords : business, consumer, research).
You're code is ok, you just need to modify the output a bit. Try this code:
//get all taxonomies and terms for this post (uses post's post_type)
foreach ((array) get_object_taxonomies($post->post_type) as $taxonomy) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo ': (- ' . $taxonomy . ': ';// I modify the output a bit.
$res = '';
foreach ($object_terms as $term) {
$res .= '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf(__("View all posts in %s"), $term->name) . '" ' . '>' . $term->name . '</a>, ';
}
echo rtrim($res,' ,').')';// I remove the last trailing comma and space and add a ')'
}
}
Hope it works.
I've not tested this code, but I have linted it. Based on your description, this should do it.
<?php
//get all taxonomies and terms for this post (uses post's post_type)
I moved this out of the fornext.
$taxonomies = get_object_taxonomies($post->post_type);
foreach ( $taxonomies as $taxonomy ) {
I moved this into an if statement. If the assignment fails (nothing is returned) it should fail the if and skip all of this.
if ($object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'))) {
$holding = array();
foreach ($object_terms as $term) {
Instead of outputting it immediately, I am building an array.
$holding[] = '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
} // foreach ($object_terms as $term)
Here is where we do that output. I'm using the explode() function. This will output each element of the array and put a ', ' after all of them except for the last one.
echo '- '.$taxonomy . ': ' .explode(', ',$holding) . ' ';
} // if ($object_terms)
} // foreach ( $taxonomies as $taxonomy )
?>
I do hope I got it right.
Cheers!
=C=
I found code for displaying the meta terms of a custom post type.
but it shows the metas as a vertical list.
I want the code to show CPT meta in a row, instead.
any help please?
the code:
<?php
//get all taxonomies and terms for this post (uses post's post_type)
foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
if ($object_terms) {
echo '- '.$taxonomy;
foreach ($object_terms as $term) {
echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) .
'" title="' . sprintf( __( "View all posts in %s" ), $term->name ) .
'" ' . '>' . $term->name.'</a><p> ';
}
}
}
?>
It's because of the HTML markup in your foreach loop, try removing the paragraph tags to something like:
foreach ($object_terms as $term) {
echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
}
I'm trying to highlight a menu item depending if it's on the current page. The code below is currently highlighting every menu item because it's within the foreach loop. How can I highlight the taxonomy term if it's on a certain page id and ONLY that term (not every one)?
<?php $args = array( 'taxonomy' => 'blog' );
$terms = get_terms('blog', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
echo $postid;
foreach ($terms as $term) {
$i++;
$absolute = get_bloginfo('url');
$postid = get_the_ID();
if($postid == "561") {
$term_list .= '<a class="menu-active" style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
} else {
$term_list .= '<a style="padding-left:30px;width:88%!IMPORTANT;" href="' . $absolute . '/blog/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
} } ?>
<?php echo $term_list; } ?>
You will have to query the taxonomy terms belonging to the current page and compare their id-s in the foreach loop.
The get_the_ID() function returns the id of the current post, not the current taxonomy.
Here is a link to a wordpress related question which shows how you can query current taxonomy terms:
https://wordpress.stackexchange.com/questions/20431/how-to-get-taxonomy-term-of-the-current-page-and-populate-queries-in-the-templat