I need a gallery block for Gutenberg.
but I have an error:
Warning: Invalid argument supplied for foreach()
This is my code:
Block::make( __( 'Galleria homepage' ))
->add_fields( array(
Field::make( 'media_gallery', 'gallery_home', __( 'Galleria' ))
))
->set_render_callback( function ( $fields, $attributes, $inner_blocks ) { ?>
<div class='swiper-container swiper-home'>
<div class='swiper-wrapper'>
<?php foreach ( $fields['gallery_home'] as $immagine ) { ?>
<div class='swiper-slide'>
<img src="<?php /* echo wp_get_attachment_url($immagine['gallery_home']); */ ?>">
</div>
<?php } ?>
</div>
</div>
<?php
});
Related
My website started showing this strange error recently which I have been unable to resolve. When I try to view a product, it returns a critical error.
I’ve done some troubleshooting and I realized the theme is the culprit. I have also checked the error log and I saw that this specific line of code is causing the error:
Uncaught Error: Call to a member function is_in_stock() on string in /home/u306409103/domains/apdbrestore.com/public_html/wp-content/themes/custom-theme/woocommerce/single-product.php:45 Stack trace: #0
When you check that line, it says:
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
To be specific the error is pointing towards the first line:
<?php if( !$product->is_in_stock() ) : ?>
I know the problem is coming from here but don’t know what next to do. What statement can I use here to do away with this error?
Thanks
Full code
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' ); ?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already sent" issues. */
Finally got the solution to this problem and in case anyone stumbles across this, all I had to do was to update how to get the reference post ID because the existing one is deprecated already.
<?php global $product;
$product = wc_get_product( $post->ID );
$stock_quantity = get_post_meta($post->ID, '_stock', true);
//var_dump($product->get_stock_status());
?>
<?php if ($product->get_stock_status() != 'instock') : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
Make sure that the $product is not a string or a boolean before checking for stock.
<?php if( is_object($product) && !$product->is_in_stock() ) : ?>
Or, you completly stop processing the template by bailing out at the top if no product was returned like this:
if (!is_object($product)) {
return;
}
Try to put this code, I updated your code a bit.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
get_header( 'shop' );
global $product;
?>
<?php
/**
* woocommerce_before_main_content hook.
*
* #hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
* #hooked woocommerce_breadcrumb - 20
*/
do_action( 'woocommerce_before_main_content' );
?>
<?php
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
?>
<div class="product-single">
<div class="container">
<div class="row">
<div class="col-md-7">
<div class="product-single-left product-contain">
<?php if( !$product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="product-single-holder">
<div class="product-single-gallery">
<div class="product-single-image active" data-img="<?php echo $img; ?>" style="background-image: url(<?php echo $img; ?>);"></div>
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) { ?>
<div class="product-single-image" data-img="<?php echo wp_get_attachment_url( $attachment_id ); ?>" style="background-image: url(<?php echo $image_link = wp_get_attachment_url( $attachment_id ); ?>);"></div>
<?php } ?>
</div>
</div>
<div class="product-single-featured">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
</div>
</div>
</div>
<div class="col-md-5">
<div class="product-single-content">
<h3><?php echo get_the_title(); ?></h3>
<div class="product-single-price">
<div class="home-sale-price">
<?php if( $product->get_sale_price() ) : ?>
<div class="home-sale-less">
Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Listed Price - <span>P <?php echo wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
<?php if(get_field('dp_and_pay')): ?>
<div class="product-single-term"><?php echo get_field('dp_and_pay'); ?></div>
<?php endif; ?>
</div>
<div class="product-single-desc">
<?php echo apply_filters( 'the_content', $product->get_short_description() ); ?>
</div>
<i class="fas fa-plus-square"></i>Add to Cart
</div>
<?php while ( have_posts() ) : the_post(); ?>
<?php //wc_get_template_part( 'content', 'single-product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
</div>
</div>
<div class="home-sale home-new">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<div class="header-excerpt">
<?php $salehead = get_field('new_arrivals_content',8); ?>
<h3>Recommmended Items</h3>
<div class="header-excerpt-text"><?php echo $salehead['content']; ?></div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="home-sale-slider">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'order_by' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if( has_post_thumbnail() ) :
$img = get_the_post_thumbnail_url();
else :
$img = placeholder();
endif;
$new_product = wc_get_product(get_the_ID());
?>
<div class="home-sale-holder product-contain">
<?php if( !$new_product->is_in_stock() ) : ?>
<div class="outofstock">
<img src="<?php echo imgfolder('out-of-stock.png'); ?>" alt="Out of Stock">
</div>
<?php endif; ?>
<div class="home-sale-top">
<img src="<?php echo $img; ?>" alt="<?php echo get_the_title(); ?>">
<i class="fas fa-plus-square"></i>Add to cart
</div>
<div class="home-sale-bottom">
<a href="<?php echo get_permalink(); ?>">
<h5><?php echo get_the_title(); ?></h5>
<div class="home-sale-price">
<?php if( $new_product->get_sale_price() ) : ?>
<div class="home-sale-less">
Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_sale_price() ) ) ); ?> </span>
</div>
<div class="home-sale-orig">
<?php echo 'P '.wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?>
</div>
<?php else : ?>
<div class="home-sale-less">Cash - <span>P <?php echo wc_price( wc_get_price_to_display( $new_product, array( 'price' => $new_product->get_regular_price() ) ) ); ?> </span></div>
<?php endif; ?>
</div>
<?php if(get_field('terms')): ?>
<div class="home-sale-term">Terms - <span>P <?php echo get_field('terms'); ?></span></div>
<?php endif; ?>
</a>
</div>
</div>
<?php endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</div>
</div>
</div>
</div>
View All Products
</div>
</div>
<?php
/**
* woocommerce_after_main_content hook.
*
* #hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
*/
do_action( 'woocommerce_after_main_content' );
?>
<?php
/**
* woocommerce_sidebar hook.
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommercve_sidebar' );
?>
<?php get_footer( 'shop' );
I've got a template part in a custom theme and all I'm doing is a WordPress query and in the loop displaying the post thumbnail.
<?php $posts = $args['posts'];
$q_args = [
'post_type' => 'post',
'post__in' => $posts
];
$posts_query = new WP_Query($q_args);
if( $posts_query->have_posts() ) { ?>
<div class="posts-grid">
<?php while( $posts_query->have_posts() ) {
$posts_query->the_post();
$post_id = get_the_ID(); ?>
<div class="post-container">
<a class="post" href="<?php the_permalink(); ?>">
<div class="post-content">
<?= get_the_post_thumbnail($post_id, 'blog-thumbnail') ?>
<?= get_the_date('d M Y'); ?>
<?php the_title('<h3>', '</h3>'); ?>
</div>
</a>
</div>
<?php }
wp_reset_postdata(); ?>
</div>
<?php } ?>
When I go to view the page the loop shows, and everything pulls through correctly, however I get an alert before the thumbnail image
Warning: Attempt to read property "ID" on int in /wp-includes/post-thumbnail-template.php on line 116
Looking at line 116 in that file it's part of the update_post_thumbnail_cache function
function update_post_thumbnail_cache( $wp_query = null ) {
if ( ! $wp_query ) {
$wp_query = $GLOBALS['wp_query'];
}
if ( $wp_query->thumbnails_cached ) {
return;
}
$thumb_ids = array();
foreach ( $wp_query->posts as $post ) {
$id = get_post_thumbnail_id( $post->ID );
if ( $id ) {
$thumb_ids[] = $id;
}
}
if ( ! empty( $thumb_ids ) ) {
_prime_post_caches( $thumb_ids, false, true );
}
$wp_query->thumbnails_cached = true;
}
I thought at first it was that because I was passing the post ID to get_the_post_thumbnail, but if I change that to passing the post object I still get the same warning. Can anyone share with me what I've done to cause this warning?
Thanks!
After a lot of head scratching around this I ended up going in a different direction. Instead of using get_the_post_thumbnail() I opted to get the ID of the post thumbnail ( using get_post_thumbnail_id() ) and then using wp_get_attachment_image() to output it and this removed the error.
<?php $posts = $args['posts'];
$q_args = [
'post_type' => 'post',
'post__in' => $posts
];
$posts_query = new WP_Query($q_args);
if( $posts_query->have_posts() ) { ?>
<div class="posts-grid">
<?php while( $posts_query->have_posts() ) {
$posts_query->the_post();
$thumbnail_id = get_post_thumbnail_id (); ?>
<div class="post-container">
<a class="post" href="<?php the_permalink(); ?>">
<div class="post-content">
<?= wp_get_attachment_image($thumbnail_id, 'blog-thumbnail') ?>
<?= get_the_date('d M Y'); ?>
<?php the_title('<h3>', '</h3>'); ?>
</div>
</a>
</div>
<?php }
wp_reset_postdata(); ?>
</div>
<?php } ?>
This isn't the most optimal solution as you are creating extra database calls running get_post_thumbnail_id and wp_get_attachment_image but it works.
I'm working on this theme in Wordpress and attempting to edit the plugin so that in when added in Visual Composer it will ask the question how many posts would you like to display? I added this to the Attributes 'number_of_post' => '4', and added this 'posts_per_page' => $number_of_post, to the WP_Query Arguments and it still doesn't seem to be working.
Here is the actual code for the plugin:
//Work Area
function ratio_work_area( $atts , $content = null ){
// Attributes
extract( shortcode_atts(
array(
'sec_title_before' => 'Latest',
'sec_title_after' => 'works',
'sec_btn_text' => 'See More Work',
'sec_btn_link' => '#',
'number_of_post' => '4',
), $atts )
);
ob_start();
?>
<!-- START PORTFOLIO -->
<section id="portfolio" class="works_area section-padding">
<div class="container-fluid">
<div class="row text-center">
<div class="section-title wow zoomIn">
<h2><?php echo esc_html($sec_title_before);?> <span><?php echo esc_html($sec_title_after);?></span></h2>
<div></div>
</div>
<div class="col-md-12">
<div class="our_work_menu">
<?php $terms = get_terms('cat_portfolios');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
?>
<ul>
<li class="filter wow fadeIn" data-wow-duration="1.5s" data-wow-delay=".25s" data-filter="all"><?php esc_html_e('All' , 'ratio');?></li>
<?php foreach ( $terms as $term ) :?>
<li class="filter" data-filter=".<?php echo esc_attr($term->slug); ?>"><?php echo esc_html($term->name); ?></li>
<?php endforeach;?>
</ul>
<?php }?>
</div>
</div>
<div class="work_all_item">
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'portfolios' ),
'posts_per_page' => $number_of_post,
);
// The Query
$ratio_port_query = new WP_Query( $args );
// The Loop
if ( $ratio_port_query->have_posts() ) {
while ( $ratio_port_query->have_posts() ) {
$ratio_port_query->the_post();
$ratio_port_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),'ratio_image_1200_1200');
?>
<?php
$portfolio_terms = get_the_terms(get_the_id(), 'cat_portfolios');
if ( ! empty( $portfolio_terms ) && ! is_wp_error( $portfolio_terms ) ):
$portfolios_cat_slug = array();
foreach($portfolio_terms as $portfolio_term){
$portfolios_cat_slug[] = $portfolio_term->slug ;
}
$portfolios_cat_array = join(" ", $portfolios_cat_slug);
$portfolios_class_array = join(" ", $portfolios_cat_slug);
endif;
?>
<div class="grid-item col-md-3 col-sm-6 col-xs-12 mix all <?php echo esc_attr($portfolios_class_array);?>">
<div class="single_our_work">
<div class="sing_work_photo">
<figure>
<img src="<?php echo esc_url($ratio_port_image['0']);?>" alt="">
<div class="sing_work_text_link">
<div class="sing_work_content_wrap">
<div class="sing_work_content">
<div class="sing_link_img">
<i class="fa fa-eye"></i>
<i class="fa fa-link"></i>
</div>
<h5><?php the_title();?></h5>
<?php $terms = get_the_terms(get_the_ID(), 'cat_portfolios');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
?>
<p><?php echo esc_html($term->name); ?></p>
<?php
}
} ?>
</div>
</div>
</div>
</figure>
</div>
</div>
</div>
<?php }
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
</div>
</div>
<?php if($sec_btn_link){ ?>
<div class="portfolio_btn text-center">
<?php echo esc_html($sec_btn_text);?>
</div>
<?php } ?>
</div>
</section>
<!-- END PORTFOLIO -->
<?php
return ob_get_clean();
}
add_shortcode ('work_area', 'ratio_work_area' );
Please try to use 'number_of_posts' => '4',; You have missed letter s.
If that doesn't work, try to use posts_per_page instead of number_of_posts. It's depend on version you using.
I created a custom post type and then I did the following:
In the homepage I have a normal loop which I display the content of that post type like custom fields,thumbnail etc.
Then, via functions.php I created a widget that allows me to show 2 custom post types based on category.
All works fine, but when I view the page the custom field does not show.
I used this code which works in my first loop of the homepage:
<?php echo get_post_meta($post->ID, 'game_offer', true); ?>
For some reason that does not work in the loop that I have in the functions for the widget.
Here the image where you can see that the custom field exists in the backend.
My code in the function looks like this in case needed:
class My_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'my_widget',
'description' => 'Custom Post Type widget',
);
parent::__construct( 'my_widget', 'Cutom Post Type Widget', $widget_ops );
}
public function widget( $args, $instance ) {
echo '<section id="custom_post_type_widget">';
?>
<div class="top_bar">
<h3 class="border-radius">Top Casino Offers</h3>
</div>
<?php
$mypost = array(
'post_type' => 'game_reviews',
'posts_per_page' => 2,
'type' => 'top-casino-offers',
);
$loop = new WP_Query( $mypost );
while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="custom_post_widget_container flex outer">
<div class="left">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<div class="game_imag">';
echo the_post_thumbnail();
echo '</div>';
}
?>
</div>
<div class="right">
<div>
<div class="flex">
<div>
<p>
<span class="title"><?php the_title(); ?></span> | <span class="rating">
<?php
$nb_stars = intval( get_post_meta( get_the_ID(), 'game_rating', true ) );
for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
if ( $star_counter <= $nb_stars ) {
echo '<img src="'.get_template_directory_uri().'/images/icon.png"/>';
} else {
echo '<img src="'.get_template_directory_uri().'/images/grey.png"/>';
}
}
?>
</span>
</p>
<ul class="custom-field-list">
<li><?php echo get_post_meta($post->ID, 'game_offer', true); ?></li>
</ul>
<?php echo get_post_meta($post->ID, 'game_offer', true); ?>
</div>
<div class="right">
<ul class="play-reviews flex">
<li>Play Now</li>
<li>Reviews</li>
</ul>
</div>
</div>
<p><?php echo get_the_excerpt(); ?><a class="read-more" href="<?php the_permalink(); ?>">Read More</a></p>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query();
echo '</section>';
}
}
add_action( 'widgets_init', function(){
register_widget( 'My_Widget' );
});
How can I show my custom field?
Use query_posts instead of WP_Query
<?php
class My_Widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'my_widget',
'description' => 'Custom Post Type widget',
);
parent::__construct( 'my_widget', 'Cutom Post Type Widget', $widget_ops );
}
public function widget( $args, $instance ) {
echo '<section id="custom_post_type_widget">';
?>
<div class="top_bar">
<h3 class="border-radius">Top Casino Offers</h3>
</div>
<?php
global $post;
$game_reviews_args=array(
'post_type' => 'game_reviews',
'post_status' => 'publish',
'showposts' => 2,
'tax_query' => array(
array(
'taxonomy' => 'type', //taxonomy name
'terms' => 'top-casino-offers', //category name slug
'field' => 'slug'
)
),
'orderby' => 'date',
'order' => 'DESC'
);
query_posts($game_reviews_args);
if (have_posts()) :
while (have_posts()) : the_post();
?>
<div class="custom_post_widget_container flex outer">
<div class="left">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<div class="game_imag">';
echo the_post_thumbnail();
echo '</div>';
}
?>
</div>
<div class="right">
<div>
<div class="flex">
<div>
<p>
<span class="title"><?php the_title(); ?></span> | <span class="rating">
<?php
$nb_stars = intval( get_post_meta( $post->ID, 'game_rating', true ) );
for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
if ( $star_counter <= $nb_stars ) {
echo '<img src="'.get_template_directory_uri().'/images/icon.png"/>';
} else {
echo '<img src="'.get_template_directory_uri().'/images/grey.png"/>';
}
}
?>
</span>
</p>
<ul class="custom-field-list">
<li><?php echo get_post_meta($post->ID, 'game_offer', true); ?></li>
</ul>
<?php echo get_post_meta($post->ID, 'game_offer', true); ?>
</div>
<div class="right">
<ul class="play-reviews flex">
<li>Play Now</li>
<li>Reviews</li>
</ul>
</div>
</div>
<p><?php echo get_the_excerpt(); ?><a class="read-more" href="<?php the_permalink(); ?>">Read More</a></p>
</div>
</div>
</div>
<?php
endwhile;
endif;
wp_reset_query();
echo '</section>';
}
}
add_action( 'widgets_init', function(){
register_widget( 'My_Widget' );
});
?>
So after all I figured that all I needed was the global $post variable before the WP_Query instance:
global $post;
When I try to get the caption or description from a image in wordpress it's not working. I can't find a real answer. I tried so many things but I don't know where i have to put some of the code. I read that I had to put this in functions.php:
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
$attachment_meta = wp_get_attachment(your_attachment_id);
and this in my loop:
<?php echo $attachment_meta['caption']; ?>
Like this:
<section>
<div class="container">
<div class="row">
<?php
if( class_exists('Dynamic_Featured_Image') ):
global $dynamic_featured_image;
global $post;
$featured_images = $dynamic_featured_image->get_featured_images( $post->ID );
if ( $featured_images ):
?>
<?php foreach( $featured_images as $images ): ?>
<div class="col-md-4 col-sm-4 col-xs-12">
<img src="<?php echo $images['full'] ?>" alt="" class="img-responsive">
<?php echo $attachment_meta['caption']; ?>
</div>
<?php endforeach; ?>
<?php
endif;
endif;
?>
</div>
</div>
</section>
What am I doing wrong?
You must call wp_get_attachment() function inside your loop like this:
<?php foreach( $featured_images as $images ): ?>
<div class="col-md-4 col-sm-4 col-xs-12">
<img src="<?php echo $images['full'] ?>" alt="" class="img-responsive">
$attachment_meta = wp_get_attachment($images['attachment_id']);
<?php echo $attachment_meta['caption']; ?>
</div>
<?php endforeach; ?>