How should I put count on this loop?
<?php
global $data;
$args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
Someone can help me?
Thanks so much
Complete code:
<?php
global $data;
$args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); ?>
<article class="article one-third column">
<div class="thumbnail">
<?php the_post_thumbnail('latest-news-thumb'); ?>
</div>
<h2><?php the_title(); ?><span>.</span></h2>
<div class="meta">
<span><?php _e('Postado em -', 'kula'); ?> <?php the_category(' & '); ?><br />on <strong><?php the_time('F jS, Y'); ?></strong></span>
<span><i class="icon-comment"></i> <?php $commentscount = get_comments_number(); echo $commentscount; ?> <?php _e('Comentários', 'kula'); ?></span>
</div>
<?php the_excerpt(); ?>
<a class="read-more-btn" href="<?php the_permalink() ?>"><?php _e('Leia mais', 'kula'); ?> <span>→</span></a>
</article><!-- end article -->
<?php endwhile; ?>
I need count 3 by 3 posts and put a class on article...
So i can put this code on article class:
<?php if (($count%3)==0) {echo ' last';}?>
Thanks
<?php
global $data;
$args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
$loop = new WP_Query($args);
$totalPost = count($loop->posts); //will give total number of posts
?>
Edit:
This will insert last class in article after 3 posts
<?php
global $data;
$args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
$loop = new WP_Query($args);
$postNo=0;
while ($loop->have_posts()) : $loop->the_post(); ?>
<article class="article one-third column <?php echo (($postNo++)%3==0)?' last ':'' ;?>">
<div class="thumbnail">
<?php the_post_thumbnail('latest-news-thumb'); ?>
</div>
<h2><?php the_title(); ?><span>.</span></h2>
<div class="meta">
<span><?php _e('Postado em -', 'kula'); ?> <?php the_category(' & '); ?><br />on <strong><?php the_time('F jS, Y'); ?></strong></span>
<span><i class="icon-comment"></i> <?php $commentscount = get_comments_number(); echo $commentscount; ?> <?php _e('Comentários', 'kula'); ?></span>
</div>
<?php the_excerpt(); ?>
<a class="read-more-btn" href="<?php the_permalink() ?>"><?php _e('Leia mais', 'kula'); ?> <span>→</span></a>
</article><!-- end article -->
<?php endwhile; ?>
After $loop = new WP_Query($args); use Sanjeev's suggestion i.e, $totalPost = count($loop->posts);
<?php
global $data;
$args = array('post_type' => 'post', 'posts_per_page' => $data['select_news']);
$loop = new WP_Query($args);
$count = 1; // add count variable
while ($loop->have_posts()) : $loop->the_post(); ?>
<article class="article one-third column<?php if (($count%3)==0) {echo ' last';}?>">
<!-- put your code here -->
</article>
<?php
$count++; //count ++
endwhile;
?>
Related
I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();
I have a simple loop on a wordpress homepage that pulls in 3 random articles. It's intermittently rendering the cards inside the other cards instead of 3 separate cards. I feel like it's a timing issue being caused from pulling them in randomly. Is there a way to force them to render separately?
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '3'
);
$loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="card" data-url="<?php echo the_permalink(); ?>">
<div class="card-body">
<?php the_post_thumbnail('homepage-thumbs', array( 'class' => 'aligncenter' )); ?>
<h3><?php the_title(); ?></h3>
<span><i class="fal fa-user"></i> by <?php the_author(); ?></span>
<p></p>
<p><?php echo substr(get_the_content(), 0, 128); ?>...</p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
Your code works in my test. However, I would use the wp_trim_words to create your excerpt rather than what you did, which could cut a word in the middle.
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '3'
);
$loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="card" data-url="<?php echo the_permalink(); ?>">
<div class="card-body">
<?php the_post_thumbnail('homepage-thumbs', array( 'class' => 'aligncenter' )); ?>
<h3><?php the_title(); ?></h3>
<span><i class="fal fa-user"></i> by <?php the_author(); ?></span>
<p></p>
<p><?php
$theContent = get_the_content();
// strip out any shortcodes from the excerpt
$theContent = strip_shortcodes( $theContent );
// wp_trim_words($content, number_of_words, read_more_text)
echo wp_trim_words( $theContent, 30 , '...' );?></p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
Try to add wp_reset_postdata after your loop, like
<?php endwhile; wp_reset_postdata(); ?>
I want to add a Wordpress loop for a specific category in a post template that exculdes the current post.
I was suggested to use:
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
But I'm having trouble getting it to work.
My loops currently looks like this.
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Try this code.
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Use
'post__not_in' => array($post->ID)
The two code blocks are using two different techniques for a Wordpress custom loop... the first modifies the global query, and the second creates a new custom query. I've outlined both below with your loop template.
Example with suggested code, global query:
Loop through the global $wp_query object in the loop code:
<div class="video">
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
<!-- use the global loop here -->
<?php while ( have_posts() ) : the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p
</div>
Example with original code, custom query:
Loop through the custom query, adding 'post__not_in':
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Sorry if my original answer was unclear, I initially thought you were combining the two code blocks.
Basically I need this code to display only products from a category id.
Its from a woocomerrce template.
Any help would be appreciated!
<div class="book_wrapper" <?php echo (!empty($book_wrapper)) ? 'style="background-image:url('.esc_url($book_wrapper).');"' : ''; ?>>
<a id="next_page_button"></a>
<a id="prev_page_button"></a>
<div id="loading" class="loading"><?php _e('Loading pages!', THEME_NAME); ?>...</div>
<div id="mybook" style="display:none;">
<div class="b-load">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
$counter = 0;
echo "<div><ul>";
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php if ($counter == 8): ?>
<?php echo '</ul></div><div><ul>'; $counter = 0; ?>
<?php endif ?>
<li>
<a href="<?php the_permalink(); ?>">
<div class="meal-name"><?php the_title(); ?></div>
<div class="meal-price"><?php echo $product->get_price_html(); ?></div>
</a>
</li>
<?php $counter++; endwhile; endif; ?>
<?php echo '</ul></div>'; ?>
</div>
</div>
</div>
Check this url for more info https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
You will need to add cat in your argument for WP_Query with specific category.
<div class="book_wrapper" <?php echo (!empty($book_wrapper)) ? 'style="background-image:url('.esc_url($book_wrapper).');"' : ''; ?>>
<a id="next_page_button"></a>
<a id="prev_page_button"></a>
<div id="loading" class="loading"><?php _e('Loading pages!', THEME_NAME); ?>...</div>
<div id="mybook" style="display:none;">
<div class="b-load">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, cat=> 'Replace with cat id here' );
$loop = new WP_Query( $args );
$counter = 0;
echo "<div><ul>";
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php if ($counter == 8): ?>
<?php echo '</ul></div><div><ul>'; $counter = 0; ?>
<?php endif ?>
<li>
<a href="<?php the_permalink(); ?>">
<div class="meal-name"><?php the_title(); ?></div>
<div class="meal-price"><?php echo $product->get_price_html(); ?></div>
</a>
</li>
<?php $counter++; endwhile; endif; ?>
<?php echo '</ul></div>'; ?>
</div>
</div>
</div>
<?php
$args=array(
'post_type'=>'post',
'cat' => '',
'posts_per_page' => 4,
'paged'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="box1">
<h1 class="blog1"><?php the_title(); ?></h1>
<h2 class="blog2">Posted By :- <?php the_author_meta('display_name'); ?> <span class="blog3">
<?php echo get_the_date(); ?></span></h2>
<?php $a=wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<span class="blog4"><img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo $a; ?>&w=200&h=150&q=100" alt=""></span>
<p class="blogcontent"><?php $a=get_the_content();
$b=strlen($a);
if($b>770)
{
$con = substr($a, 0, strrpos(substr($a,0,770),' ')). '....';
echo $con;
?>
<p style=" float:right;"><a style ="margin-top:15px;" href="<?php echo get_permalink($post->ID);?> ">Read More</a></p>
<?php
}
else
{
echo $a;
}
?>
</p>
</div>
<?php //endforeach;
endwhile; ?>
<?php endif; ?>
<span style="padding:30px;"><?php if(function_exists('wp_paginate')) {wp_paginate();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
</span>
I working on wordpress blog and make a pagination in blog there were issue in pagination.when click on page 2 the url change but page not changed.i am using the following code for that
Thanks & Regards
Try this code:
<?php
$args=array(
'post_type'=>'post',
'cat' => '',
'posts_per_page' => 4,
'paged'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="box1">
<h1 class="blog1"><?php the_title(); ?></h1>
<h2 class="blog2">Posted By :- <?php the_author_meta('display_name'); ?> <span class="blog3">
<?php echo get_the_date(); ?></span></h2>
<?php $a=wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<span class="blog4"><img src="<?php echo get_template_directory_uri(); ?>/timthumb.php?src=<?php echo $a; ?>&w=200&h=150&q=100" alt=""></span>
<p class="blogcontent"><?php $a=get_the_content();
$b=strlen($a);
if($b>770)
{
$con = substr($a, 0, strrpos(substr($a,0,770),' ')). '....';
echo $con;
?>
<p style=" float:right;"><a style ="margin-top:15px;" href="<?php echo get_permalink($post->ID);?> ">Read More</a></p>
<?php
}
else
{
echo $a;
}
?>
</p>
</div>
<?php //endforeach;
endwhile; ?>
<?php endif; ?>
<span style="padding:30px;">
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
$wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
</span>
Alternatively, you can see this tutorial if you want to have numbered pagunation.