How to get the post thumbnail with global post ID - wordpress

I have two different post types where I want to have a relationship. Authors can list one business(post type - "listings") and many special offers (post type - "special_offers"). In each single special offer page, I need to display which business offers that specific offer. Currently I'm using this code, and it outputs the Business name correctly. I need the business logo to be there as well (featured image)
function get_author_business() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'listings', 'post__not_in' => array( $post->ID ), 'posts_per_page' => -1 ) );
foreach ( $authors_posts as $authors_post ) {
$output .= '' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}

This might help (i'm assuming you are trying to get the image tag inside your output html)
function get_author_business() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post_type' => 'listings', 'post__not_in' => array( $post->ID ), 'posts_per_page' => -1 ) );
foreach ( $authors_posts as $authors_post ) {
$business_img = wp_get_attachment_image_src( get_post_thumbnail_id( $authors_post->ID ), 'thumbnail' );
$output .= '<img src="'.$business_img[0].'">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '';
}
return $output;
}

You can use the following code :
<?php
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$attachment_url=wp_get_attachment_image_src($post_thumbnail_id,'full');
/* full -- is image size */
?>
<img src="<?php echo $attachment_url; ?>">
hope that will work for you.

Related

How to get wordpress empty content list

i make two list theme
1.the posts have featured image and title, but empty content. i want get this list.
2.and other posts with content list.
I don't have any idea, help me please
Try to use below code:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
$content = get_the_content();
if($content == '') {
array_push( $blank_posts, $post);
}
endwhile;
endif;
/* print all blank content posts */
//echo "<pre>"; print_r($blank_posts);
/* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "ID= ". $pst->ID . ', '. "Title= ". $pst->post_title .'<hr />';
}
}

wordpress custom Query, Two columns of content, How do I get two paging function?

There are many pages, but how to get paging?
Two columns of content have two paging.
i need paging for Two columns of content.
how to do this?
and if you can tell me how to add the function of the comments, I will thank you even more.
look this:
https://stackoverflow.com/a/45451489/1266305
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$content_post =array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
global $post;
$content = get_the_content();
if(empty($content)) {
array_push( $blank_posts, $post);
}else{
array_push( $content_post, $post);
}
endwhile;
endif;
/* blank content posts */ /* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "<pre>"; print_r($blank_posts);
echo $pst->post_title;
}
};
/* have content posts */
if(!empty($content_post)){
foreach ($content_post as $pst) {
echo '<a href='. $pst->guid .'>' .$pst->post_title. '</a> ';
}
}

How to add related products or cross sells to order emails in WooCommerce

I can't seem to find a way of adding related products or cross sells to order emails in WooCommerce, and it's hurting my brain. I'm sure it must be possible but can't seem to get it working. I tried adding do_shortcode after order table, but that didn't work.
You can put this in your child theme's functions.php file:
// product suggestion in order mail
function order_mail_product_suggestion($atts) {
$atts=shortcode_atts(
array(
'id' => '',
), $atts, 'order_mail_product_suggestion');
$orderId = esc_attr($atts['id']);
$order = wc_get_order( (int)$orderId );
$items = $order->get_items();
$cat = array();
$pid = array();
foreach ( $items as $item ) {
$pid[] = $item->get_product_id();
$terms = wp_get_post_terms($item->get_product_id(),'product_cat',array('fields'=>'ids'));
foreach ( $terms as $term ) {
$cat[] = $term;
}
}
$uniuqcat = array_unique($cat);
$uniuqpid = array_unique($pid);
$html = '';
$args = array(
'post_type' => 'product',
'stock' => 1,
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '20',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => implode(',', $uniuqcat),
'operator' => 'IN'
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog',
'operator' => 'NOT IN'
)
)
);
$loop = new WC_Product_Query($args);
$products = $loop->get_products();
if ($products) {
$html .= '<div id="suggestion" style="padding: 20px 0;border-top: 1px solid #eee;">';
$html .= '<h2 style="text-align: center;font-weight: 400;color: #000;">PLUS, MORE THINGS YOU MAY LIKE</h2>';
$html .= '<table style="width: 100%;table-layout: fixed;border-collapse: collapse;">';
$html .= '<tbody>';
$html .= '<tr>';
$i=0;
foreach ( $products as $product ) {
if (in_array($product->get_id(), $pid)) {
continue;
}
$html .= '<td align="center" style="padding: 5px;border: 1px solid #eee;">';
if (has_post_thumbnail( $product->get_id() )) {
$html .= get_the_post_thumbnail($product->get_id(), 'shop_catalog');
} else {
$html .= '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />';
}
$html .= '<h3><a style="color: #000;font-weight: normal;text-decoration: none;font-size: 13px;text-align: center;display: block;line-height: 16px;" href="'.get_permalink( $product->get_id() ).'">'.esc_attr($product->get_title() ? $product->get_title() : $product->get_id()).'</a></h3>';
$html .= '<p><a style="font-weight: normal;text-decoration: none;display: block;margin: 0 auto;max-width: 72px;padding: 4px 9px;text-align: center;background-color: #000;color: #fff;font-size: 13px;" href="'.get_permalink( $product->get_id() ).'" class="shop-now">Shop Now</a></p>';
$i++;
if($i==4) break;
}
$html .= '</tr>';
$html .= '</tbody>';
$html .= '</table>';
$html .= '</div>';
}
// now return the preapred html
return $html;
}
// register shortcode
add_shortcode('email_product_suggestion', 'order_mail_product_suggestion');
And then call the function in email templates like this:-
// Product suggestions
$order = $email->object;
if ( $order ) {
$id = $order->get_id();
echo do_shortcode( '[email_product_suggestion id="'.$id.'" ]' );
}
This will show products from the category where the current order item belongs, and exclude items that are already in the order.
You will have to do this with php in the emails template. You would edit email-footer.php and add something like this:
<?php
echo do_shortcode('[related_products per_page="4"]');
?>
You would want to add this into the table, not below the table. Just add an extra and put it in there.

Get featured images from custom taxonomy terms

Oke, first of I downloaded this plugin. This enables me to give my custom taxonomy terms an image. This works great. But the way my theme is setup and the way the plugin wants to work, isn't for me.
I only want the images to show in the place where I say it should. That's why I used terms (categories). Because then I can use this code:
<?php
//list terms in a given taxonomy
$taxonomy = 'our_team';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
That only shows the names of the team members when you activated them (checked the box) in the post, and not in other places.
Now I would like to be able to use a similar code but for the featured images. The plugin uses this code to get the thumbnails.
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="my-custom-class-name">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'post_id' => 15,
'taxonomy' => 'our_team',
) );
Does anyone know how to "merge" these two codes together and generate an image only if I checked the box in the custom post type. Or knows another way to do this?
** UPDATE **
Ok, getting somewhere. The code is now only displayed where I want it. With the help of Rohit Kishore! Thanks! Here is the working code:
<div class="teamleden over-ons-ul">
<?php $terms = get_the_terms( $post->ID , 'our_team' );
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'before' => '<div class="our-team">',
'after' => '</div>',
'before_image' => '<span>',
'after_image' => '</span>',
'image_size' => 'detail',
'taxonomy' => 'our_team',
) );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'our_team' );
if( is_wp_error( $term_link ) )
continue;
}
?>
</div>
The print code had to be before the foreach part...

Custom Wp_Query still uses the main post id

On my Wordpress website, I have the following code:
if ( is_page( 8 ) ) {
include_once('somecustompage.php');
}
And in my somecustompage.php, I use a custom WP_Query:
// WP_Query arguments
$args = array (
'post_type' => 'farewell',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => '10',
'sticky' => false
);
// The Query
$current_farewell_query = new WP_Query( $args );
// The Loop
if ( $current_farewell_query->have_posts() ) {
$current_farewells = '<table><tr><th>NAME</th><th>LOCATION</th><th>FAREWELL</th></tr>';
while ( $current_farewell_query->have_posts() ) {
$farewell_name = ( get_field('name') ? get_field('name') : '' );
$chapels = wp_get_post_terms( get_the_ID(), 'chapels', array('fields' => 'name') );
$current_farewell_query->the_post();
$current_farewells .= '<tr>';
$current_farewells .= '<td>' . $farewell_name . '</td>';
$current_farewells .= '<td></td>';
$current_farewells .= '<td></td>';
$current_farewells .= '</tr>';
}
$current_farewells .= '</table>';
} else {
$current_farewells = 'No Current Farewells.';
}
// Restore original Post Data
wp_reset_postdata();
However, when I try to use the post IDs within this custom loop, both get_the_ID() and $post->ID return 8, which is the main page post id. I need to use the new post IDs that are returned in the new query.
Anybody knows how to resolve this?
I found where the problem was. Everything should've been called after this line: $current_farewell_query->the_post(); for the query to work as expected.

Resources