Wordpress thumbnail from category as background div - php

I've got a problem with Wordpress.
I want the thumbnail of my last post as background of my div.
Code:
<div<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"id="photopost"', $image_src[0] );
}
?><?php $cat_id = 3; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?><?php endwhile; endif; ?>></div>
This works fine with the thumbnail of my last post (all post).
But i want the thumbnail from the last post of category ID 3.
How can i fix this?
Thanks,
Bjorn

You are calling the Query after you have created the thumbnail. The Query should wrap the div.
Try:
<?php $cat_id = 3; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?>
<div<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"id="photopost"', $image_src[0] );
}
?>></div>
<?php endwhile; endif; ?>

Related

WooCommerce display product thumbnail and add to cart button

I have created a live search field that fetches the title of the product using AJAX.
It works correctly, however, I would like to fetch the product thumbnail and the ‘Add to the cart’ button.
**Edit: I have added the output for the thumbnail and add to cart button. It works, but it only displays in singular rows. How can I update my output in rows of 4 as below?
Front End Code
<input type="text" name="keyword" id="keyword" onkeyup="fetch()">
<div id="datafetch">Your numbers will show here</div>
<script>
function fetch(){
$.post('<?php echo admin_url('admin-ajax.php'); ?>',{'action':'my_action'},
function(response){
$('#datafetch').append(response);
console.log(result);
});
}
</script>
Code in Functions.php
<?php
}// LOTTERY start the ajax function
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
global $product;
$product = get_product( get_the_ID() ); //set the global product object
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {?>
<h4><?php the_title();?></h4>
<h4><?php the_post_thumbnail();?></h4>
<p><?php echo $product->get_price_html(); ?></p>
<?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
<?php
}
endwhile;
wp_reset_postdata();
endif;
die();
}
You can use this get_the_post_thumbnail_url(get_the_ID()) to get thumbnail URL and add image.
You can use this https://yourdomain.com/?add-to-cart=<product_id> to add product add to cart URL.
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/
Try this approach
//Ajax
add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
$the_query = new WP_Query( array( 'posts_per_page' => -1, 's' => esc_attr( $_POST['keyword'] ), 'post_type' => 'product' ) );
if( $the_query->have_posts() ) :
while( $the_query->have_posts() ): $the_query->the_post();
$myquery = esc_attr( $_POST['keyword'] );
$a = $myquery;
$search = get_the_title();
if( stripos("/{$search}/", $a) !== false) {
//get image and add-to-cart buttom here
$query->the_post();
global $product;
wc_get_template_part('content', 'product');
//End get image and add-to-cart buttom here
}
endwhile;
wp_reset_postdata();
endif;
die();
}

Get the current post category name inside while loop

I created a custom post type "stm_media_gallery"
And three category inside this custom post type.
I want to display category name associated with each post.
<?php $gallery_query = new WP_Query( array('post_type' =>
'stm_media_gallery', 'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post(); ?>
--Display post name and its category name
<?php endif; ?>
<?php endwhile; ?>
You just need to put following code inside loop :
<div>
<?php
foreach((get_the_category()) as $category){
echo $category->name."<br>";
echo category_description($category);
}
?>
</div>
Update in existing code
<?php $gallery_query = new WP_Query(
array('post_type' => 'stm_media_gallery',
'posts_per_page' => -1) );
if( $gallery_query->have_posts() ) :
while( $gallery_query->have_posts() ) : $gallery_query->the_post();
$gallery_category = get_the_category( get_the_ID() );
the_title( '<h3>', '</h3>' );
echo "<br>";
<?php foreach ( $gallery_category as $key => $value) { echo $value->category_nicename; } ?>
<?php endif; ?>
<?php endwhile; ?>
You can use the pre-made WordPress function the_category( $separator, $parents, $post_id ) to print the post categories as links.
Further information on the WordPress Codex: Function Reference: the_category
Edit: Print only the names:
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo esc_html( $categories->name );
}
Put this inside While Loop
global $post;
$postcat = get_the_category( $post->ID );

How to show post from just two categories on Wordpress Home page?

Only two categories need to be showed in the homepage. Can anyone help.
You can use WP_Query to get your posts list, and display it with the loop
Example :
$the_query = new WP_Query( array( 'category_name' => 'staff,news' ) );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
In your functions.php file paste the below code:
I am assuming that you want to show categories from two categories which are having ids 5 and 9.
function kiran_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '5,9');
}
}
add_action( 'pre_get_posts', 'kiran_home_category' );
Explanation:
kiran_home_category is just a custom name for the function. That can be any name. The way it works is you attach a function to the action hook pre_get_posts. So before getting the posts the function kiran_home_category will be called. And then inside the function I am changing the query here to only load categories with ID 5 and 9
In wordpress WP_query, category__in parameter used to select category with posts.
<?php
$query = new WP_Query( array( 'category__in' => array( 2, 6 ),'post_status'=>'publish','orderby'=>'menu_order','order'=>'Asc' ) );
if($query->have_posts()):
echo '<ul>';
while ( $query->have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
endif;
?>
For more information about wordpress query click here , you can read more information.
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => -1,'category_name' => array('Latest News','News') );
$loop = new WP_Query( $args );
if($loop->have_posts()):
?><ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li> <span class="date"><?php echo get_the_date( 'd F Y');?></span>
<h3><?php echo get_the_title();?></h3>
<?php echo $description = get_the_content(); ?>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
<?php wp_reset_postdata(); ?>
Do the following, usually in page.php or single.php or if you want a custom page for a category, you can do, category-samplecat.php..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => array('samplecat', 'anothercat'),
'paged' => $paged
);
$arr_posts = new WP_Query($args);
Then do the usual if, while statement..
if($arr_posts->have_posts() ) :
// Start the loop.
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();?>
<?php endwhile;
endif;

wordpress category pagination not working anymore

My navigation stoped working on my custompost category... i have this :
Portfolio
<?php $loop = new WP_Query( array( 'post_type' => 'portfolios', 'posts_per_page' => 8, 'offset'=> 0 ,'paged' => $paged, ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="list_c">
<a href="<?php the_permalink(); ?>"><div><div id="over2"><p>+info</p></div></div>
<div id="read"> <span>ler mais </span></div>
<div id="ptxL" class="filldiv">
<div id="thumbL" class="grow "><?php the_post_thumbnail('featuredImageCropped3');?></div>
<div id="infoL">
<div id="titled" class="tt">
<h2><?php the_title();?></h2></div>
<div id="contentL"><h3><?php the_excerpt(); ?> </h3></div></div><div id="color"></div>
<!-- You have not associated any custom fields with this post-type. Be sure to add any desired custom fields to this post-type by clicking on the "Manage Custom Fields" link under the Custom Content Type menu and checking the fields that you want. -->
</div></a></li>
<?php endwhile; wp_reset_query(); ?>
<?php wpbeginner_numeric_posts_nav(); ?>
<?php get_footer(); ?>
and on my functions.php:
function wpbeginner_numeric_posts_nav() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li class="pre">%s</li>' . "\n", get_previous_posts_link() );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
i changed servers maybe thats the problem...but i can make it work...my debug finds no errors...
Please try this code.I think you have mistake in paged parameter.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'portfolios', 'posts_per_page' => 8, 'offset'=> 0 ,'paged'=>$paged ) ); ?>
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="list_c">
<a href="<?php the_permalink(); ?>"><div><div id="over2"><p>+info</p></div></div>
<div id="read"> <span>ler mais </span></div>
<div id="ptxL" class="filldiv">
<div id="thumbL" class="grow "><?php the_post_thumbnail('featuredImageCropped3');?></div>
<div id="infoL">
<div id="titled" class="tt">
<h2><?php the_title();?></h2></div>
<div id="contentL"><h3><?php the_excerpt(); ?> </h3></div></div><div id="color"></div>
<!-- You have not associated any custom fields with this post-type. Be sure to add any desired custom fields to this post-type by clicking on the "Manage Custom Fields" link under the Custom Content Type menu and checking the fields that you want. -->
</div></a></li>
<?php endwhile;endif; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
<?php wp_reset_query(); ?>
I hope this will work for you.
Thanks

How to display contents using while loop and then exclude a comma at the end ?

I have this code in displaying content, it use "while" loop
<?php
$mypost = array( 'post_type' => 'testimonials', );
$loop = new WP_Query( $mypost );
while ( $loop->have_posts() ) : $loop->the_post();$do_not_duplicate = $post->ID;
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
echo "{image : '$image[0];'},";
endwhile;
?>
But what I want to achieve is, to remove the comma of the last loop so for example I have a 3 stuff to loop then the scenario should be like this.
{image : 'http://website.com/wp-content/uploads/2013/09/image1.jpg'},
{image : 'http://website.com/wp-content/uploads/2013/09/image2.jpg'},
{image : 'http://website.com/wp-content/uploads/2013/09/image3.jpg'}
as you can see at the third loop the comma was removed, that's what I am trying to achieve. But so far I have no idea how to make that.
I am open in any suggestion, recommendation and ideas.
$images = array();
while ( $loop->have_posts() ) : $loop->the_post();$do_not_duplicate = $post->ID;
$images[] = "{image : '<?php echo get_option('slider3_Field');?>'}";
endwhile;
echo implode(',', $images);
You want to use it as JSON, right? Why not separate your PHP from your JS?
$images = array();
while ( $loop->have_posts() ) : $loop->the_post(); $do_not_duplicate = $post->ID;
$images[] = (object) array('image' => wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' )[0]);
endwhile;
echo json_encode($images);
instead of echo have your tried savins as a variable i.e.
while ( $loop->have_posts() ) : $loop->the_post();$do_not_duplicate = $post->ID;
$str .= "{image : '<?php echo get_option('slider3_Field');?>'},";
endwhile;
rtrim($str,',');
echo $str;

Categories