<?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.
Related
this is my code
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
foreach($portfolio_categories as $portfolio_category)
echo '<li data-filter=".' .$portfolio_category->slug. '">' .$portfolio_category->name. '</li> ';
?>
this shows the filters created by the category's name inside $portfolio_categories
then, there is the creation of client's list
<ul id="list" class="portfolio_list clearfix responsive">
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' ) );
?>
<?php if (have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php $terms = wp_get_object_terms($post->ID, 'portfolio_category'); ?>
<?php $pf_bimg = wp_get_attachment_url( get_post_thumbnail_id() );?>
<?php $pf_simg = aq_resize( $pf_bimg, 420, 450, true ); ?>
<li class="span3 list_item <?php foreach ($terms as $term) { echo $term->slug.' '; } ?>">
<div class="recent-item">
<figure>
<div class="touching medium">
<img src="<?php echo $pf_simg; ?>" alt="<?php the_title(); ?>" />
<!-- <i class="icon-search"></i>
<i class="icon-link"></i> -->
</div>
<figcaption class="item-description">
<h5><?php the_title(); ?></h5>
<span><?php $i = 0; foreach ($terms as $term) { if($i)echo " / "; echo $term->name; $i=1; } ?></span>
</figcaption>
</figure>
</div>
</li>
<?php endwhile; ?> <?php endif; ?>
<?php wp_reset_query();?>
</ul>
What i 'm trying to do is to display a different element for every different category .
I thought to insert an if condition before the tag , like this
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif
?>
Thank all for your time,
Dan
You need to store last category, otherwise you'll have <figure> tag before each row:
<?php
$portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
?>
<?php if ($portfolio_category != $prev_portfolio_category): ?>
<?php if ($portfolio_category = 'category1'): ?>
<figure>1</figure>
<?php else: ?>
<figure>2</figure>
<?php endif; ?>
<?php endif; ?>
<?php
$prev_portfolio_category = $portfolio_category;
?>
I'm trying to style the first post differently than the rest. It's for a "featured" section on index.php
<?php
$count = 0;
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$count++;
if ( $count == 1 ) : ?>
<div class="big">
<div class="details">
<a class="cat" href="#">nike</a>
<p><?php the_title(); ?></p>
<ul class="stats">
<li class="icon-calendar"><?php the_time('m.d.Y') ?></li>
<li class="icon-eye"><?php if(function_exists('the_views')) { the_views(); } ?></li>
</ul>
</div>
<?php echo get_first_inserted_image(); ?>
</div>
<?php else : ?>
<div class="small">
<div class="holder">
<?php echo get_first_inserted_image(); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
the above code does not seem to work as nothing is showing up. I would also like to expand on this to show only 4 random posts from the last 7 days. Any guidance is appreciate it! Thanks!
this got it done
<?php
// WP_Query arguments
$args = array (
'orderby' => 'rand',
'showposts' => 5,
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
$count = 0;
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$count++;
if ( $count == 1 ) : ?>
<div class="big">
<div class="details">
<a class="cat" href="#">nike</a>
<p><?php the_title(); ?></p>
<ul class="stats">
<li class="icon-calendar"><?php the_time('m.d.Y') ?></li>
<li class="icon-eye"><?php if(function_exists('the_views')) { the_views(); } ?></li>
</ul>
</div>
<?php echo get_first_inserted_image(); ?>
</div>
<?php else : ?>
<div class="small">
<div class="holder">
<?php echo get_first_inserted_image(); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
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>
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;
?>
I am creating a wordpress template to query my custom post type posts by using tax_query. It works fine and I get the correct posts in page #1, but I see the same posts as seen at page #1 when I click page #2. What did i missing here? Thanks in advance.
<?php
get_header();
?>
<h1><?php
the_title();
?></h1> <hr> <br>
<div id="content" class="three_fourth <?php
echo of_get_option('blog_sidebar_pos');
?>">
<?php
?>
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'tax_query' => array(
array(
"taxonomy" => "wpdic-category",
"field" => "slug",
"terms" => "featured-charities",
"numberposts" => 5,
"paged" => $paged
)
)
);
$wp_query = new WP_Query($args);
?>
<?php
if ($wp_query->have_posts()):
while ($wp_query->have_posts()):
$wp_query->the_post();
?>
<article id="post-<?php
the_ID();
?>" <?php
post_class();
?>>
<header>
<h2><a href="<?php
the_permalink();
?>" title="<?php
the_title();
?>" rel="bookmark"><?php
the_title();
?></a></h2>
<?php
$post_meta = of_get_option('post_meta');
?>
<?php
if ($post_meta == 'true' || $post_meta == '')
{
?>
<div class="post-meta">
<div class="fleft"></div>
<div class="fright"></div>
</div><!--.post-meta-->
<?php
}
?>
</header>
<?php
$post_image_size = of_get_option('post_image_size');
?>
<?php
if ($post_image_size == '' || $post_image_size == 'normal')
{
?>
<?php
if (has_post_thumbnail())
{
echo '<a href="';
the_permalink();
echo '">';
echo '<div class="featured-thumbnail"><div class="img-wrap">';
the_post_thumbnail();
echo '</div></div>';
echo '</a>';
}
?>
<?php
}
else
{
?>
<?php
if (has_post_thumbnail())
{
echo '<a href="';
the_permalink();
echo '">';
echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">';
the_post_thumbnail('post-thumbnail-xl');
echo '</div></div></div>';
echo '</a>';
}
?>
<?php
}
?>
<div class="post-content">
<?php
$post_excerpt = of_get_option('post_excerpt');
?>
<?php
if ($post_excerpt == 'true' || $post_excerpt == '')
{
?>
<div class="excerpt"><?php
$excerpt = get_the_excerpt();
echo my_string_limit_words($excerpt, 52);
?></div>
<?php
}
?>
<div id="widget_my_contentwidget"><ul><li><a class="link" href="<?php
the_permalink();
?>"> read more</a></li></ul></div>
<hr>
</div>
</article>
<?php
endwhile;
else:
?>
<div class="no-results">
<p><strong>There has been an error.</strong></p>
<p>We apologize for any inconvenience, please <a href="<?php
bloginfo('url');
?>/" title="<?php
bloginfo('description');
?>">return to the home page</a> or use the search form below.</p>
<?php
get_search_form();
?> <!-- outputs the default Wordpress search form-->
</div><!--noResults-->
<?php
endif;
?>
<?php
if (function_exists('wp_pagenavi')):
?>
<?php
wp_pagenavi();
?>
<?php
else:
?>
<?php
if ($wp_query->max_num_pages > 1):
?>
<nav class="oldernewer">
<div class="older">
<?php
next_posts_link('« Older Entries');
?>
</div><!--.older-->
<div class="newer">
<?php
previous_posts_link('Newer Entries »');
?>
</div><!--.newer-->
</nav><!--.oldernewer-->
<?php
endif;
?>
<?php
endif;
?>
<!-- Page navigation -->
<?php
$wp_query = null;
$wp_query = $temp;
?>
<div id="footer">
<div class="clearfix">
<?php
if (!dynamic_sidebar('Footer Content')):
?>
<?php
endif;
?></div></div>
</div><!--#content-->
<?php
get_footer();
?>
wp_pagenavi() will work with the global $wp_query by default; if you want to paginate some custom query you need to pass it as a parameter, like:
wp_pagenavi( array( 'query' => $mycustomquery ) );
I have encountered the same problem before. For some reasons i cannot make WP query to work, but instead i used query_posts. Here is the code
<?php
/**
* #author MESMERiZE
* #copyright 2012
*/
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'tax_query' => array(
array(
"taxonomy" => "wpdic-category",
"field" => "slug",
"terms" => "featured-charities",
"numberposts" => 5,
"paged" => $paged
)
)
);
query_posts($args);
while(have_posts()): the_post();
endwhile;
?>
Then i added the wp pagenavi function after the endwhile keyword.
if (function_exists('wp_pagenavi')) {
wp_pagenavi();}