I have four loops. In the first loop I want to display all current articles from all categories (breaking news). In the 2nd loop only articles with the category 4,5,6,7 should be displayed. But if in the 1st loop an article from these categories were published, this newest article should not appear in the 2nd loop. So far I have only read about the general exclusion of articles but nothing about conditions.
You must use category__not_in:
$include_cats = array( 4, 5, 6, 7 );
$exclude_cats = array( 3 ); // 'breaking news' TERM ID
$posts_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4 ,
'ignore_sticky_posts' => true,
'category__in' => $include_cats,
'category__not_in' => $exclude_cats,
'orderby' => 'date',
);
$posts = new WP_Query( $posts_args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
?>
<a href="<?php echo get_the_permalink( $posts->post->ID ); ?>" title="<?php echo get_the_title( $posts->post->ID ); ?>">
<?php
if ( has_post_thumbnail( $posts->post->ID ) ) {
echo get_the_post_thumbnail( $posts->post->ID, $img, array( 'class' => 'post-thumb' ) ); // YOUR IMAGE SIZE
} else {
echo '<img class="' . $img . '" src="placeholder.png" width="150" height="150" alt="' . get_the_title( $posts->post->ID ) . '">'; //ADD PLACEHOLDER IMAGE HERE
}
?>
<h3 class="post-title"><?php echo get_the_title( $posts->post->ID ); ?></h3>
<span class="author"><?php echo get_the_author( $posts->post->ID ); ?></span>
</a>
<?php
}
}
wp_reset_postdata();
Related
I face a problem with displaying products on a specific portfolio post. I use product shortcodes in order to display specific products, but they do not appear.
I use wordpress 5.2.5, avada theme 5.9.1 and woocommerce 3.9.2.
Anyone got an idea what is going on?
If not, can anyone tell me an alternative way to display them?
use shortcode [get_specific_product_shortcode id='here enter product id']
function get_specific_product_shortcode_function($atts){
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
ob_start();
$args = array(
'posts_per_page' => 1,
'post__in' =>array($id),
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish',
);
$query = new WP_Query( $args );
$count = $query->found_posts;
if($query->have_posts()){
while( $query->have_posts() ) { $query->the_post();
$post_thumbnail_id = get_post_thumbnail_id();
$product = wc_get_product(get_the_ID());
if (!empty($post_thumbnail_id)) {
$image_size = 'medium'; // (thumbnail, medium, large, full or custom size)
$img_url_arr = wp_get_attachment_image_src( $post_thumbnail_id, $image_size );
$img_url = $img_url_arr[0];
}else{
//placeholder image
$img_url = home_url().'/wp-content/uploads/woocommerce-placeholder.png';
}
?>
<div>
<a href="<?php echo get_permalink();?>">
<img src="<?php echo $img_url?>" style="width: 150px;">
</a>
<div>Price: <?php echo $product->get_price_html(); ?></div>
<div><?php echo get_the_title();?></div>
</div>
<?php
}
wp_reset_postdata();
}else{
echo 'no post'; echo "<br>";
}
return ob_get_clean();
}
add_shortcode('get_specific_product_shortcode', 'get_specific_product_shortcode_function');
I am using a template which has a widget to show woocommetce product on the home page, Currently price of product is just a number E.g. ₹18500/- without any decimal or comma, I want to show price with Thousand separator and Decimal separator E.g. ₹ 18,500/-
Note: Thousand separator and Decimal separator is enabled from woocommerce settings, and here I want to show price on a widget.
I am attaching code of the widget.
any help will be appreciated.
* Filter the arguments for the Recent Posts widget.
*
* #since 1.0.0
*
* #see WP_Query
*
*/
$query_args = array(
'posts_per_page' => $post_number,
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order,
'meta_query' => array(),
'tax_query' => array(
'relation' => 'AND',
),
);
switch ( $wc_advanced_option ) {
case 'featured' :
if( !empty( $product_visibility_term_ids['featured'] )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_term_ids['featured'],
);
}
break;
case 'onsale' :
$product_ids_on_sale = wc_get_product_ids_on_sale();
if( !empty( $product_ids_on_sale ) ){
$query_args['post__in'] = $product_ids_on_sale;
}
break;
case 'cat' :
if( !empty( $travel_way_wc_product_cat )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $travel_way_wc_product_cat,
);
}
break;
case 'tag' :
if( !empty( $travel_way_wc_product_tag )){
$query_args['tax_query'][] = array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $travel_way_wc_product_tag,
);
}
break;
}
switch ( $orderby ) {
case 'price' :
$query_args['meta_key'] = '_price';
$query_args['orderby'] = 'meta_value_num';
break;
case 'sales' :
$query_args['meta_key'] = 'total_sales';
$query_args['orderby'] = 'meta_value_num';
break;
case 'ID' :
case 'author' :
case 'title' :
case 'date' :
case 'modified' :
case 'rand' :
case 'comment_count' :
case 'menu_order' :
$query_args['orderby'] = $orderby;
break;
default :
$query_args['orderby'] = 'date';
}
$travel_way_featured_query = new WP_Query( $query_args );
if ($travel_way_featured_query->have_posts()) :
echo $args['before_widget'];
$animation = "init-animate zoomIn";
?>
<section id="<?php echo esc_attr( $unique_id ); ?>" class="at-widgets acme-abouts <?php echo $bg_gray_class;?>">
<div class="container">
<?php
if ( ! empty( $title ) ) {
echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
}
$div_attr = 'class="featured-entries-col woocommerce"';
?>
<div class="row at-cat-product-wrap clearfix ">
<div <?php echo $div_attr;?>>
<?php
$travel_way_featured_index = 1;
while ( $travel_way_featured_query->have_posts() ) :$travel_way_featured_query->the_post();
$travel_way_list_classes = 'single-list';
if ( 1 == $column_number ) {
$travel_way_list_classes .= " col-sm-12";
} elseif ( 2 == $column_number ) {
$travel_way_list_classes .= " col-sm-6";
} elseif ( 3 == $column_number ) {
$travel_way_list_classes .= " col-sm-4 col-md-4";
} else {
$travel_way_list_classes .= " col-sm-4 col-md-3";
}
?>
<div class="<?php echo esc_attr( $travel_way_list_classes ); ?>">
<a href="<?php the_permalink();?>">
<?php the_post_thumbnail($travel_way_img_size)?>
<div class="caption">
<h3 class="at-woo-title"><?php the_title();?></h3>
<?php
woocommerce_template_loop_rating();
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if($sale) :
global $post, $product;
echo apply_filters( 'woocommerce_sale_flash', '<span class="onsale">' . esc_html__( 'Sale!', 'travel-way' ) . '</span>', $post, $product );
?>
<p class="product-price">
<del>
<?php
echo esc_html($currency);
echo esc_html( $price );
?>
</del>
<?php
echo esc_html($currency);
echo esc_html( $sale . '/-' );
?>
</p>
<?php elseif($price) : ?>
<p class="product-price">
<?php
echo esc_html($currency);
echo esc_html( $price . '/-');
?>
</p>
<?php endif;
?>
</div>
</a>
</div><!--dynamic css-->
<?php
$travel_way_featured_index++;
endwhile;
?>
</div><!--featured entries-col-->
</div><!--cat product wrap-->
<?php
echo $args['after_widget'];
echo "<div class='clearfix'></div>";
// Reset the global $the_post as this query will have stomped on it
?>
</div>
</section>
<?php
endif;
wp_reset_postdata();
}
} // Class Travel_Way_Wc_Products ends here
} ```
You can use number_format. Should be something like:
$price = floatval($price);
$decimal_separator = wc_get_price_decimal_separator();
$thousand_separator = wc_get_price_thousand_separator();
$decimals = wc_get_price_decimals();
$formatted_price = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
echo esc_html( $formatted_price . '/-');
In wordpress i am creating a custom loop/query throughwhich i pass certain parameters. As i click through pages however the last page duplicates some posts/products inorder to satisfy the posts_per_page variable however i would like to specify that i dont want any repetitions. Is there a standard way to do this? It would seem like a pretty obvious point.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'products', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php previous_posts_link('« Previous', $loop ->max_num_pages);
next_posts_link(' Next »', $loop ->max_num_pages); ?>
<?php wp_reset_query();?>
It would seem like a pretty obvious point.
Not if you're using 'orderby' => 'rand' on your query, which is also very expensive by the way on large tables.
If you want to make sure that the items which already have been displayed will be excluded in the upcoming queries you'll need to save the post_ids which already has been displayed and pass them to the post__not_in parameter, see the codex page an search for post__not_in .
You could do something like this which should help you get the idea:
...
// don't forget to initialize the session somewhere
$already_displayed_post_ids = [];
if ( ! isset( $_SESSION['already_displayed_post_ids'] ) {
$_SESSION['already_displayed_post_ids'] = $already_displayed_post_ids;
} else {
$already_displayed_post_ids = array_merge( $already_displayed_post_ids, $_SESSION['already_displayed_post_ids'] );
}
$args = [
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'products',
'orderby' => 'rand',
'post__not_in' => $already_displayed_post_ids
];
$loop = new WP_Query( $args );
...
i have the title of my last 4 posts but i need the image post too. I'm terrible programmer `
include('../blog/wp-load.php'); // Blog path
// Get the last 4 posts
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 4,
'category' => 0,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish'
));
// Display them as list
echo '<ul>';
foreach($recent_posts as $post) {
echo '<li>', $post['post_title'], '</li>';
}
echo '</ul>'`
i'm trying to do something like this:
I think you're looking for get_the_post_thumbnail Here's the Codex.
For example:
get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));
You might try something like this for your final product (untested):
// Display them as list
$output = '<ul>';
foreach($recent_posts as $post) {
$link = get_permalink($post['ID']);
$image = get_the_post_thumbnail( $postID,'medium', array( 'class' => 'aligncenter' ));
$title = $post['post_title'];
$output .= '<li>
<a href="'.$link.'">'.
$image
.'<h2>'.$title.'</h2>
</a>
</li>';
}
$output .= '</ul>';
echo $output;
It's must be work!
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 4,
'category' => 0,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish',
));
foreach($recent_posts as $single_post){
$get_post_images = get_attached_media( 'image', $single_post['ID'] );
$get_post_images = array_shift( $get_post_images );
$first_image_url = $get_post_images->guid;
?>
<?php echo $single_post['post_title'] ?><br />
<img src="<?php echo $first_image_url; ?>" /><br />
<?php
}
?>
If you have't the post thumbnail, but have images inside post, then you need this solution which use inside the loop:
$get_post_images = get_attached_media( 'image', $post->ID );
$get_post_images = array_shift( $get_post_images );
//Get url for image
$first_image_url = $get_post_images->guid;
// Show the image
echo '<img src="'. $first_image_url .'" />';
I got it!
<ul>
<?php
include('esenergy/wp-load.php'); // Blog path
function recentPosts() {
$rPosts = new WP_Query();
$rPosts->query('showposts=3');
while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
<li>
<?php the_post_thumbnail('recent-thumbnails'); ?>
<h2><?php the_title();?></h2>
</li>
<?php endwhile;
wp_reset_query();
}
?>
</ul>
<?php echo recentPosts(); ?>
I got custom post types and categories made for them. All I want to do is to list all the custom categories with list of all posts that belongs to this category (with title and excerpt). None of the solutions I found works. I can display custom categories but can't list posts that belong to these categories. Here's my code
<?php
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$taxonomy = 'timeline_category';
$title = '';
$description = true;
$args = array(
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'taxonomy' => $taxonomy,
'use_desc_for_title' => $description,
'title_li' => $title
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
//display posts
$cat_new = $category->term_id;
$post_args = array( 'numberposts' => 5, 'category' => $cat_new, 'caller_get_posts' => 0 );
$myposts = get_posts( $post_args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; } ?>
The code above shows even that there is 1 post in this category (Post Count) so the problem is somwhere in foreach agrument for posts.
Any ideas?
Thanks
I'm not too familiar with endforeach;, but after a quick Google search, it looks like you have a syntax error.
<?php foreach($foo as $bar):
// Stuff
endforeach; // No trailing "}" ?>
Or
<?php foreach($foo as $bar){
// Stuff
} ?>