How to get specific featured images of the post in wordpress. ? - php

In the code below I am trying to extract the exactly featured image of the post_id = 25 but instead, it is pulling new images of the different post.
<?php
$post_ID= 25;
$post_url= get_permalink($post_ID);
$queried_post = get_post($post_ID);
?>
<img class="img-circle" src="<?php
if (has_post_thumbnail($post_ID)) {
the_post_thumbnail('medium');
}
?>"
<h2><a href="<?php get_permalink($post_url) ?>"</a><?php echo $queried_post->post_title; ?></h2>
<p><?php
query_posts('p=25');
if (have_posts()) : while (have_posts()) : the_post();
?>
<div class="entry">
<?php echo substr(get_the_excerpt(), 0, 300); ?><span>[...]</span>
</div>
<?php
endwhile;
endif;
?>
<p><a class="btn btn-default" href="#" role="button" >View details ยป</a></p>

You'll need to use get_the_post_thumbnail in order to pass in the id.
if (has_post_thumbnail($post_ID)) {
echo get_the_post_thumbnail($post_ID,'medium');
}
the_post_thumbnail actually does this:
function the_post_thumbnail( $size = 'post-thumbnail', $attr = '' ) {
echo get_the_post_thumbnail( null, $size, $attr );
}
get_the_post_thumbnail in turn uses the current post id if null is passed in.
EDIT
You just need to change this:
<?php
$post_ID= 25;
$post_url= get_permalink($post_ID);
$queried_post = get_post($post_ID);
?>
<img class="img-circle" src="<?php
if (has_post_thumbnail($post_ID)) {
the_post_thumbnail('medium');
}
?>"
To this:
<?php
$post_ID= 25;
$post_url= get_permalink($post_ID);
$queried_post = get_post($post_ID);
?>
<img class="img-circle" src="<?php
if (has_post_thumbnail($post_ID)) {
echo get_the_post_thumbnail($post_ID,'medium');
}
?>"

Related

Display woocommerce product variations as single products on shop page and ajax load more

I have displayed wc product's variations as a single product.
Here is link: https://hueslabsstage.wpengine.com/store/
also, you need credentials:
login: hueslabsstage
password: 11test11
I have an issue with ajax load more function. I am using WooCommerce Load More Products by BeRocket. For example, when I choose 2 products to show on load more it really shows 2 products, but with all variations. For example, I have 4 products, the first one and the second one with 2 variations, so the plugin shows me 4 items on the shop page(2 products with 2 variations).
So, I need to display each variation as a single product in the way the plugin sees products. In order to when I choose 2 products to show, the plugin should display 2 variations of the product, instead of 2 products with them all variations.
Hope I explained my issue correctly.
My content-product.php:
<?php
if ($product->is_type('variable')): ?>
<?php
$unique_variations = [];
foreach($product->get_available_variations() as $key => $item) {
if (!variation_exists($item['attributes']['attribute_pa_color'], 'attribute_pa_color', $unique_variations)) {
$unique_variations[] = $item;
}
}
?>
<?php foreach($unique_variations as $variation):
$itemVariation = $variation['variation_id'];
?>
<?php
$meta = get_post_meta($variation['variation_id'], 'attribute_pa_color', true);
if (empty($meta)) {
continue;
}
$term = get_term_by('slug', $meta, 'pa_color');
$color = get_field('product_color', $term->taxonomy . '_' . $term->term_id);
$gallery = get_field('gallery', $variation['variation_id']);
$front_image = wp_get_attachment_image_src($variation['image_id'], 'product-thumbnail')[0];
$back_image = null;
if (!empty($gallery)) {
$back_image = $gallery[0]['sizes']['product-thumbnail'];
}
?>
<article class="<?php echo $outOfStock; ?>">
<?php if ($product_status_badge): ?>
<div class="product_badge">
<img src="<?php echo $product_status_badge; ?>" alt="Product status">
</div>
<?php endif; ?>
<div class="product_images">
<?php
if ( ! $product->managing_stock() && ! $product->is_in_stock() ){
?>
<div class="out_of_stock_overlay">Sold Out</div>
<?php
}
?>
<div class="product_image_items">
<figure>
<a href="<?php echo get_the_permalink() ?>?attribute_pa_color=<?php echo $meta ?>" title="<?php echo get_the_title(); ?>">
<img src="<?php echo $item['image']['url'];?>" alt="<?php echo get_the_title(); ?>" class="front_img">
<img src="<?php echo $back_image;?>" alt="<?php echo get_the_title(); ?>" data-test="<?php echo $back_image;?>" class="back_img">
</a>
<div class="product_arrows">
<div class="arrow_left">
<em class="fal fa-chevron-left"></em>
</div>
<div class="arrow_right">
<em class="fal fa-chevron-right"></em>
</div>
</div>
</figure>
</div>
</div>
<div class="product_top">
<div class="product_left">
<h3 class="product_title"><?php echo get_the_title(); ?></h3>
<div class="price">
<?php if ($product->is_type('variable')): ?>
<?php echo $product->get_variation_price() . " " . get_woocommerce_currency_symbol(); ?>
<?php else: ?>
<?php echo $product->get_price_html(); ?>
<?php endif; ?>
</div>
</div>
<?php if ($product->is_type('variable')): ?>
<?php
$unique_variations = [];
foreach($product->get_available_variations() as $key => $item) {
if (!variation_exists($item['attributes']['attribute_pa_color'], 'attribute_pa_color', $unique_variations)) {
$unique_variations[] = $item;
}
}
?>
<div class="product_right">
<ul class="product_variations_list">
<?php foreach($unique_variations as $variation): ?>
<?php
$meta = get_post_meta($variation['variation_id'], 'attribute_pa_color', true);
if (empty($meta)) {
continue;
}
$term = get_term_by('slug', $meta, 'pa_color');
$color = get_field('product_color', $term->taxonomy . '_' . $term->term_id);
$gallery = get_field('gallery', $variation['variation_id']);
$front_image = wp_get_attachment_image_src($variation['image_id'], 'product-thumbnail')[0];
$back_image = null;
if (!empty($gallery)) {
$back_image = $gallery[0]['sizes']['product-thumbnail'];
}
?>
<li class="<?php echo $variation['variation_id'] == $itemVariation ? 'active' : ''; ?>" data-variation-id="<?php echo $variation['variation_id']; ?>" data-variation-image="<?php echo $front_image; ?>" data-variation-back-image="<?php echo $back_image; ?>">
<a href="<?php echo get_the_permalink(); ?>?attribute_pa_color=<?php echo $term->slug;?>" title="<?php echo $term->name; ?>">
<em class="fas fa-certificate primary" style="color: <?php echo $color ;?>;"></em>
<em class="fas fa-certificate secondary"></em>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
<div class="product_bottom">
<ul>
<?php foreach ($attributes as $attribute): ?>
<?php $image = get_field('image', 'pa_specifications_' . $attribute->term_id); ?>
<li>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $attribute->name; ?>">
</li>
<?php endforeach; ?>
</ul>
</div>
</article>
<?php endforeach; ?>
<?php endif;
?>
archive-product.php is not changed :
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}

Current Menu Item not working with $childpages

I need to add the "active" or "current-menu-item" class to a custom childpages menu. I've tried the code below but it doesn't seem to work.
I've browsed google but can't find anything that will work!
<?php
$childpages = query_posts('orderby=menu_order&order=asc&post_type=page&post_parent=35&posts_per_page=300');
if ($childpages)
{
// Display the children content
foreach ($childpages as $post)
{
setup_postdata($post)
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="mezzanine-sub-title <?php echo!empty($_GET['page_id']) && $_GET['page_id'] == $post->ID ? "active" : NULL ?>">
<span><?php the_title(); ?></span>
<a>
<?php
global $post;
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(5600, 1000), false, '');
?>
<div class="mezzanine-sub-image" style="background: url(<?php echo $src[0]; ?> );border:<?php the_field('border'); ?>;">
</div>
</a>
</a>
<?php
}
}
?>
Thanks in advance
change query_posts to get_posts also you have invalid syntax with your <a> tag.
finally instead of using $get which is not returning anything use wp_query
so your code should be:
$childpages = get_posts('orderby=menu_order&order=asc&post_type=page&post_parent=35&posts_per_page=300');
if ($childpages)
{
// Display the children content
foreach ($childpages as $post)
{
setup_postdata($post)
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="mezzanine-sub-title<?php if ( $post->ID == $wp_query->post->ID ) { echo ' active'; }?>">
<span><?php the_title(); ?></span>
<?php
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(5600, 1000), false, '');
?>
<div class="mezzanine-sub-image" style="background: url(<?php echo $src[0]; ?> );border:<?php the_field('border'); ?>;">
</div>
</a>
<?php
}
}

How to show text if no posts from query?

I have a custom WordPress post type, and want to make so if there are no posts in the query to display, it shows a message of saying no posts were found, I've tried a few suggestions but nothing seems to be working so far.
Full Code of the relevant section:
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
while($the_query->have_posts()):$the_query->the_post();
$k++;
if($k==1){
$color= 'gray';
}else if($k==2){
$color= 'blue';
}else if($k==3){
$color= 'green';
}else if($k==4){
$color= 'light-blue';
}else if($k==5){
$color= 'yellow';
}
?>
<div itemprop="exampleOfWork" class="col-xs-12 col-sm-4 portfolio-item">
<div class="portfolio-holder">
<?php
if(has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'singleport',true);
?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo $image_url[0];?>" alt="Portfolio image" /></a></figure>
<?php }else{ ?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/noimage.jpg" alt="No image" /></a></figure>
<?php } ?>
<h6 itemprop="name"><?php the_title(); ?></h6>
<span itemprop="genre"><?php the_field('name');?></span>
<div class="transparentbg <?php echo $color; ?>">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<a itemprop="url" href="<?php the_permalink();?>">
<img src="<?php echo $image['url']; ?>" alt="logo" />
</a>
<?php endif; ?>
<a href="<?php the_permalink();?>" class="info">
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/port-hover-bg.png" alt="Port hover bg" /><br />
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/search-icon.png" height="20" width="20" alt="Search icon">
</a>
</div>
</div>
</div>
<?php endwhile;wp_reset_query();?>
</div>
</div>
</section>
How can we make it so, if there's no posts, it echo's a message?
Add a check -
if (!empty($the_query->have_posts())) {
while(...) {...}
} else {
echo "No Posts!";
}
Issue
You want to use WP Query class for fetching a particular type of posts ( based on custom fields ), and if it returns no posts at all, it should display a message.
Solution
$the_query = new WP_Query( $args );
Where "$args" is your list of parameters for WP_Query constructor.
Now $the_query is an object of class type WP-Query. Hence it contains a function that will return whether there is any post matching your particular query. If there is at least one post, it will return True, else it will return False.
That function to check if there are any posts matching your queries is have_posts() .
Usage is ObjectName->have_posts();
in your specific case it is
$the_query->have_posts();
This will return true or false based on the query ( ie, arguments you supply using $arg variable ).
hence:
if($the_query->have_posts()) {
//There are posts matching the query
}
else
{
//There are no posts matching the query
}
You can use the below code ( You will have to modify this, but still a good start ).
<?php
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Reference : https://codex.wordpress.org/Class_Reference/WP_Query
You need to add if condition before while loop like that way:
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
if( $the_query->have_posts() ) {
while($the_query->have_posts()):
// your code
<?php endwhile;
} else {
echo "No data found.";
}
wp_reset_query();
?>
<?php
$k=0;
$args = array(
'post_type'=>'portfolios',
'posts_per_page'=>5,
'order'=>'DESC'
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
while($the_query->have_posts()):$the_query->the_post();
$k++;
if($k==1){
$color= 'gray';
}else if($k==2){
$color= 'blue';
}else if($k==3){
$color= 'green';
}else if($k==4){
$color= 'light-blue';
}else if($k==5){
$color= 'yellow';
}
?>
<div itemprop="exampleOfWork" class="col-xs-12 col-sm-4 portfolio-item">
<div class="portfolio-holder">
<?php
if(has_post_thumbnail()){
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'singleport',true);
?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo $image_url[0];?>" alt="Portfolio image" /></a></figure>
<?php }else{ ?>
<figure><a itemprop="url" href="<?php the_permalink();?>"><img src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/noimage.jpg" alt="No image" /></a></figure>
<?php } ?>
<h6 itemprop="name"><?php the_title(); ?></h6>
<span itemprop="genre"><?php the_field('name');?></span>
<div class="transparentbg <?php echo $color; ?>">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<a itemprop="url" href="<?php the_permalink();?>">
<img src="<?php echo $image['url']; ?>" alt="logo" />
</a>
<?php endif; ?>
<a href="<?php the_permalink();?>" class="info">
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/port-hover-bg.png" alt="Port hover bg" /><br />
<img itemprop="image" src="<?php echo esc_url( get_template_directory_uri() );?>/assets/images/search-icon.png" height="20" width="20" alt="Search icon">
</a>
</div>
</div>
</div>
<?php
endwhile;
else:
echo "Your message goes here";
endif;
wp_reset_query();
?>
</div>
</div>
</section>
Adding if condition will do it.

Inserting php inside wordpress query

i have this code
<?php $project_taxonomy_select = get_post_meta( $post->ID, '_ep_project_taxonomy_select', true );
$theCatSlug = get_term_by( 'id', $project_taxonomy_select[0], 'project_filter' );
$theCatSlug = $theCatSlug->slug;
echo $theCatSlug; ?>
how to put the php above in "project_filter=" inside my query in the below php code
<div id="project">
<?php $my_query = new WP_Query('post_type=project&posts_per_page=-1&project_filter=the php here');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a class="all project-item <?php echo $tax ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php if ( has_post_thumbnail()) { ?><?php $thumb_id = get_post_thumbnail_id(); $thumb_url = wp_get_attachment_image_src($thumb_id,'project-thumb-main', true); echo $thumb_url[0]; ?><?php } else { ?><?php echo esc_url(home_url('/')); ?>assets/img/no-project-thumb-main.jpg<?php } ?>">
<div class="magnifier">
<span class="text"><?php the_title(); ?></span>
</div>
</a>
<?php endwhile; wp_reset_query(); ?>
</div>
im using Custom-Metaboxes-and-Fields-for-WordPress
Please Help.. :)
You'd just use simple string concatentation:
WP_Query('post_type=project&posts_per_page=-1&project_filter='.$theCatSlug);

Query not loading get_post_meta data

The function below is used to load my custom post type posts. The problem is that it isn't loading the get_post_meta data. I have this in my functions.php file. Do I need something added to the get_post_meta since it's in the functions file?
function get_latest_post(){
global $wpdb;
$num=$_POST['numposts'];
$id = $_POST['id'];
$id = split('page',$id);
$id = str_replace('/','',$id[1]);
if($id!=0)
{
$offst = $id*$num-$num;
}else $offst=0;
$wp_query = new WP_Query(); $wp_query->query('post_type=video_posts&orderby=date&posts_per_page='.$num.'&offset='.$offst);
if($wp_query->have_posts()) : $i = 1; while ($wp_query->have_posts()) : $wp_query->the_post();?>
<?php $embeds = get_post_meta($post->ID, 'rm_video_embed_code'); ?>
<?php $thumbnail_id = get_the_post_thumbnail($post->ID);
preg_match ('/src="(.*)" class/',$thumbnail_id,$link);
if(!empty($thumbnail_id)) {
$image_path = thumbGen($link[1],190,0,"crop=1&halign=center&valign=center&return=1");
$image_all = get_bloginfo('url').$image_path;
$my_image = array_values(getimagesize($image_all));
list($width, $height, $type, $attr) = $my_image;
}
?>
<div class="pop_<?php echo $i; ?>" style="display:none;"><div class="bClose"></div>
<?php foreach($embeds as $embed) { echo $embed; }?>
</div>
<?php if(has_post_thumbnail()) : ?>
<li><a href="javascript:void(0);" class="video_popup_<?php echo $i; ?>" title="<?php the_title(); ?>">
<div class="video_title"><?php the_title(); ?></div>
<div class="video_description"><?php the_content(); ?></div>
<div><img src="<?php echo $image_path; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" /></div>
<div class="clear"></div>
</a></li>
<?php endif; ?>
<?php $i++; endwhile; ?>
<?php endif; //wp_reset_query();
if((1==1)===FALSE){
echo "Oops! System error!";
}
else {
}
die();
}
add_action('wp_ajax_get_latest_post', 'get_latest_post');
add_action('wp_ajax_nopriv_get_latest_post', 'get_latest_post');
add this to the function global $post;

Categories