So here is what i'm trying to do. I have created a custom taxonomy called "country" and within that custom taxonomy I have categories of different countries like, USA, France, Germany, England and so on. I created a ACF(Advance custom field) Field Group that lets me add a image to each country, I have this called "flag". So when I make a post and I check the country under the custom taxonomy I need to pull that image (flag) associated with that category and display it on my page.
I have seen lots of post on what im looking to do but I cant seem to get any to work. Here is the code im TRYING to use.
<?php
$attachment_id = get_field( 'flag', 'country_' . $queried_object->term_id );
$thumb = wp_get_attachment_image_src( $attachment_id );
var_dump($attachment_id);
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>
Its bring back a "NULL" and not my image id.
Any ideas on what i'm doing wrong or any advice you and lend me?
have you defined $queried_object?
EDIT:
TRY THIS OUT
<?php
$term =$wp_query->queried_object;
$attachment_id = get_field( 'flag', 'country_' . $term->term_id );
$thumb = wp_get_attachment_image_src( $attachment_id );
var_dump($attachment_id);
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>
Related
I seem to be struggling with something in woocommerce.
I have created a single product page template which currently shows the product's featured image at the top of the page in the banner.
However, instead of the product featured image, I would like the banner to display the product category image of the category the product belongs to.
They will only ever belong to one category, so having a fallback isn't that important (I think?!)
To get the product thumbnail I'm using this:
<?php
$image = get_field('product_single_image', 'options');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>">
<?php endif; ?>
So it looks like I could use the same code but replace the product_single_image with get_category_thumbnail or something...
But I think I need to find the category first? I'm not sure where to go from here.
Any help would be massively appreciated.
The best way to get current category ID on single product page and image
global $wp_query;
$terms_post = get_the_terms( $post->cat_ID , 'product_cat' );
foreach ($terms_post as $term_cat) {
$term_cat_id = $term_cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $term_cat_id, 'thumbnail_id', true );
$image_url = wp_get_attachment_url( $thumbnail_id );
echo '<img src="' . $image_url . '">';
}
I'm actually working on a wordpress website with a Dessign.net theme (the pixel one) which got a beautiful full-page slider on the front page.
The slider shows the featured image of selected posts (post for which i've checked "show in slideshow" in the meta box field on edit page).
Those featured image are used in the same way for different view on the site (eg. thumbnails). I need them for the thumbnails, but i'ld like another image (still relative to selected posts) for the home-page slider.
I've found that "Dynamic Featured Image" plugins for wordpress but now i can't achieve to get the second featured image url in the slider's loop.
Here's the part of code for the slider, as it was with the theme:
<ul>
<?php
$slider_arr = array();
$x = 0;
$args = array(
//'category_name' => 'blog',
'post_type' => 'post',
'meta_key' => 'ex_show_in_slideshow',
'meta_value' => 'Yes',
'posts_per_page' => 99
);
query_posts($args);
while (have_posts()) : the_post();
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' );
//$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'large' );
$img_url = $thumb['0'];
?>
<li data-background="<?php echo $img_url; ?>" onclick="location.href='<?php the_permalink(); ?>';" style="cursor:pointer;">
</li>
<?php array_push($slider_arr,get_the_ID()); ?>
<?php $x++; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
Now i've tried to put the code found on the plugin github page:
if( class_exists('Dynamic_Featured_Image') ) {
global $dynamic_featured_image;
$thumb = $dynamic_featured_image->get_featured_images( );
//You can now loop through the image to display them as required
}
in place of $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' );
But $thumb return array as a string
I tried a few different things but i'm not fluent in php.
Hope this is understandable.
I had to recently look for answers on something like this myself. The plugin author is great at explaining how to actually set up the plugin, but doesn't really say how to get the images, leaving it up to the developer. So, I feel you.
If I understand you correctly, you need to get the featured image from the plugin, not the featured image included in WordPress.
<?php global $dynamic_featured_image;
$featured_images = $dynamic_featured_image->get_featured_images( get_the_ID() );
//You can now loop through the image to display them as required
foreach($featured_images as $featured_image) {
echo "<a href='".get_the_permalink()."' class='slide'>";
echo "<span> <img src='".$featured_image['full']."' /> </span>";
echo "</a>";
} ?>
In this plugin, you can create infinite amounts of featured images per post/page. This code above is only for grabbing the first image created by the plugin. It's $featured_image['full'] that calls the image itself.
You can change the type of image shown to other sizes as well, including any custom sizes you create. The code to use those sizes can be found on this post.
This is my first question.
I have a custom taxonomy set up in a theme inside a functions.php file.
I've added some extra meta fields for the custom taxonomy (category), also set through the functions.php file.
I am using the update_option() function.
Here is the part that is saving the options to the DB:
<?php
// save extra category extra fields hook
add_action ( 'edited_artists', 'save_extra_category_fileds');
// save extra category extra fields callback function
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}
?>
In my template file I am calling them like this:
<?php
$terms = wp_get_post_terms( $post->ID, 'artists');
foreach ($terms as $term){
$term_id = $term->term_id;
$term_name = $term->name;
$term_taxonomy_id = $term->term_taxonomy_id;
$term_slug = $term->slug;
//do you term meta stuff here
//print_r($term);
}
?>
This is where I use them (among other things), and it is of course inside the LOOP:
<div class="single-sculpture-artist-info">
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
<img src="<?php echo $category_meta['artists_photo'] ?>" alt="<?php echo $term_name; ?>">
</a>
<h3>
<?php echo $term_name; ?>
</h3>
<p><?php echo $category_meta['artists_city_province'] ?></p>
<p><?php echo $category_meta['artists_bio_excerpt'] ?></p>
</div>
All of this code works perfectly.
I started adding the content, but then it suddenly started to fail. I think it started when I tried to use one of the category (taxonomy) names I used while developing this whole system (my guess is that it was cached somewhere or something), but then I tried using it with different name, and adding some other which were not there before, and it fails as well. My best guess is that somehow the options table is overloaded with data (limit or something).
Is that even possible? I dont have a lot, 56 working posts in that taxonomy, and 34 categories(taxonomy terms).
I tried my best to get my head around it but couldn`t find what was the problem.
When I insert like 2 or 3 more posts, it starts messing around. So, this:
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
outputs correct link, but, this:
<img src="<?php echo $category_meta['artists_photo'] ?>"
doesnt. It outputs data from some other category (from the same CPT). I can provide additional info upon request.
Not answering to your question but do you know there’s a big change happening with WP 4.2+ where they’re going to be splitting taxonomy terms, so that taxonomy terms don’t share the same term id if their slugs match.
Please take a look at the links below for some details on how this can be addressed
https://make.wordpress.org/core/2015/02/16/taxonomy-term-splitting-in-4-2-a-developer-guide/
https://developer.wordpress.org/plugins/taxonomy/working-with-split-terms-in-wp-4-2/
Through the help of my network I found the solution to the problem. The line where I am calling:
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
I switched to this:
<?php
$category_meta = get_option( "category_$term_id");
?>
This seems to have resolved the problem. However, here are some other solutions that might be more relevant in the similiar situation:
https://en.bainternet.info/tax-meta-class-faq/
also, be sure to read the things Joe Wilson posted above. Thanks again.
I want to hyperlink a specific word in the post to the full image which is stored on wp-content/uploads.
I add a line of words "View Full Size" on single.php so it is displayed on the bottom of each created post and I want to hyperlink those words to the full size image attached on that post.
How to do it with PHP?
I've used this snippet a couple times, It grabs the featured image, I've added the else option so you can set a default image if needed. I hope this is what you where looking for. Any problems I'll be glad to help.
<?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); $alt = get_post_meta(get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true);?>
View Full Size
<?php else: ?>
<?php endif;?>
I am in a situation with a theme where I can't rely on custom-fields in wordpress and need to use the "featured image" feature instead.
This is the code that calls the custom field
<?php $thumbnail_image = get_post_meta($post->ID, 'post-image', true); ?>
I can't find any documentation on "featured image" in the Wordpress codex, but is it possible to call the url of the featured image of a page in this example?
This should get you going
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
Edit:
$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id() );
$image_src = $image_attributes[0];