This is my code here -
<div class="gallery-posts">
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'numberposts' => 4
);
$product_posts = get_posts( $args );
?>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php $videos = rwmb_meta( '_video-link' );
foreach ( $videos as $video ); ?>
<div
class="card-container video-link"
data-link="<?php echo $video['src']; ?>"
title="<?php the_title(); ?>"
description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>"
thumbnail-link="<?php the_post_thumbnail_url() ?>">
<figure class="gallery-image">
<?php the_post_thumbnail(); ?>
<div class="gallery-image-text">
<p><?php the_title() ?></p>
<i class="fas fa-play"></i>
</div>
</figure>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
I want to set it up so that Next/Prev shows whenever there are more than 4 posts.
With the regular Word Press loop I know you can just do the_posts_pagination but that doesn't work here.
What needs to be added for pagination to work with this loop?
Change your code like this:
<div class="gallery-posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'posts_per_page' => 4,
'paged' => $paged
);
$product_posts = get_posts( $args );
foreach ( $product_posts as $post ) : setup_postdata( $post );
$videos = rwmb_meta( '_video-link' );
foreach ( $videos as $video ):
?>
<div class="card-container video-link" data-link="<?php echo $video['src']; ?>" title="<?php the_title(); ?>" description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>" thumbnail-link="<?php the_post_thumbnail_url() ?>">
<figure class="gallery-image">
<?php the_post_thumbnail(); ?>
<div class="gallery-image-text">
<p><?php the_title() ?></p>
<i class="fas fa-play"></i>
</div>
</figure>
</div>
<?php
next_posts_link( 'Older Entries', $product_posts->max_num_pages );
previous_posts_link( 'Next Entries »' );
wp_reset_postdata();
endforeach;
?>
</div>
Related
I have loop that displays posts. I want to add some html if there will be no posts. Like if there is no posts ---> show my html if/else.
<?php $args = array(
'posts_per_page' => 1000,
'post_type' => 'contract',
'author' => $current_user->ID,
'post_status' => 'publish' );
?>
<?php $get_category_posts = get_posts( $args ); ?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>
<li class="jail-app-v-0-4 contract-item">
<div class="contract-item-inner">
<a class="hoverable" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
Try this code out please:
<?php $args = array(
'posts_per_page' => 1000,
'post_type' => 'contract',
'author' => $current_user->ID,
'post_status' => 'publish' );
?>
<?php $get_category_posts = get_posts( $args );
if(!empty($get_category_posts)):
?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>
<li class="jail-app-v-0-4 contract-item">
<div class="contract-item-inner">
<a class="hoverable" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
<?php else: ?>
NO POSTS FOUND!
<?php endif; ?>
Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>
In Woocommerce, I Need some help with this custom product loop, in my code my result is : how it looks like
The loop doesn't stop and it is looping the same products for three or four times.
The code I am using is here :
<div class="container">
<div id="default_products_page_container" class="wrap wpsc_container">
<?php
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 );
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
$cat = get_query_var( 'product_cat' );
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => $cat, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
?>
<div class="wpsc_default_product_list">
<?php
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
<div class="default_product_display product_view_<?php echo get_permalink( $loop->post->ID ); ?>group">
<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 if (has_post_thumbnail( $loop->post->ID ))
{
?>
<div class="product-image-thumb">
<img src="<?php echo the_post_thumbnail_url( $loop->post->ID );?>"/>
</div>
<?php
} else
{?>
<div class="product-image-thumb">
<a>
<img src="<?php echo woocommerce_placeholder_img_src();?>"/>
</a>
</div>
<?php } ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_query(); ?>
</div>
</div>
I Hope this code gonna help you guys :)
<ul class="products">
<?php
$args = array(
'product_cat' => 'Shampoo',
'posts_per_page' => 4,
'orderby' => 'rand'
);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
global $product; ?>
<div class="row">
<!-- <h2>Shampoo</h2> -->
<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>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
<!--/.products-->
You need to do a tax query instead this way:
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 5,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug', // Or 'term_id' or 'name'
'terms' => get_query_var( 'product_cat' ), // A slug term
// 'include_children' => false // or true (optional)
)),
'orderby' => 'rand'
) );
Tested and works on Woocommerce product category archive pages…
Your code should work, although it will take all products in the loop (including those marked as "draft" when you are logged in). You can define only published by adding 'post_status' => 'publish' to your argument variable.
And you should take a look at those anchor tags (specifically the close part)
I'm building a custom page template and I can't seem to get the pagination to work. It keeps showing the same posts as on the home/first page. I have tried a lot of different code but getting the same problem with each results.
This is the current query I am using:
<?php
global $paged;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=2&post_type=post'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="news-item">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
?>
<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
Found the solution, this code worked:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => '2',
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="news-item">
<?php
if ( has_post_thumbnail() ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
echo '<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>';
}
else {
}
;?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
<div id="single-post-container"></div>
<a class="button-1 load-more" href="<?php echo get_permalink(); ?>">Lees meer</a>
<a class="ajax-close button-1" href="#">X</a>
</div>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Try this code just copy pase
<?php
$args = array(
'post_type'=> 'post',
//'category_name' => 'blog',
'orderby' => 'post_date',
//'posts_per_page'=>'1'
'paged' => get_query_var('paged')
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php // Wordpress Pagination
$big = 999999999; // need an unlikely integer
$links = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '<',
'next_text' => '>',
'type' => 'array'
) );
if(!empty($links)){ ?>
<ul class="pagination">
<?php
foreach($links as $link){
?>
<li><?php echo $link; ?></li>
<?php
}
wp_reset_query(); ?>
</ul>
<?php } ?>
I have this code:
<div class="row-fluid">
<div class="span6">
<h3>UPCOMING DEBATES</h3>
<?php
$query = new WP_Query( array(
'post_type' => 'debate',
'post_status' => 'future',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1
) );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
}
}
?>
</div>
And inside that if( $query->have_posts() ) { I want to add this code:
<div class="post-title box">
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php if ( has_post_thumbnail() ) { ?>
<div class="entry-image" style="<?php echo $max_height; ?>"><?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large', false, '' ); ?>
<a href="<?php echo $src[0]; ?>" class="lightbox" title="<?php the_title(); ?>" rel="bookmark">
<?php the_post_thumbnail( $featured_image_size ); ?>
</a>
</div>
<?php } ?>
This code is retrieving the post_title and the thumbnail_image.
The problem is that I don't know how to add this inside my first code ?
I've tried to put it inside my if if( $query->have_posts() ) { but it doesn't work. It seems to be more complicated than just copy and paste.
Any suggestions on how can I make this?
I'm a PHP beginner.
Try this:
<div class="row-fluid">
<div class="span6">
<h3>UPCOMING DEBATES</h3>
<?php
$query = new WP_Query( array(
'post_type' => 'debate',
'post_status' => 'future',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1
) );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
?>
<div class="post-title box">
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php if ( has_post_thumbnail() ) { ?>
<div class="entry-image" style="<?php echo $max_height; ?>"><?php
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large', false, '' ); ?>
<a href="<?php echo $src[0]; ?>" class="lightbox" title="<?php the_title(); ?>" rel="bookmark">
<?php the_post_thumbnail( $featured_image_size ); ?>
</a>
</div>
<?php }
}
}
?>
</div>