I have this php code for wordpress random post plugin:
<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><?php the_title(); ?></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>
<?php $post = $tmp_post; // reset the $post to the original ?>
I wanna know what is the the meaning of the code 'offset' => 1.i already understand to others such as:
Numberpost - how many post you would like to be displayed?
Orderby – randomly select from the list of our blog post.
Post_status – selects only the blog post which is on Publish status.
Offset - ???
can someone define this for me.
The offset is used for pagination. From the docs:
offset (int) - number of post to displace or pass over. Note: Setting offset parameter will ignore the paged parameter.
Related
I made a page template and try to echo the custom post type "products" from template parts
but when I publish the page its only display one post of products only, I prefer display 4 post :(
get_header();
// Reference global $post variable.
global $post;
// Get posts.
$posts = get_posts(array(
'post_type' => 'product',
'post_count' => 4
));
// Set global post variable to first post.
$post = $posts[0];
// Setup post data.
setup_postdata( $post );
// Output template part.
get_template_part( 'template-parts/products' );
// Reset post data.
wp_reset_postdata();
Change your query like this
$query = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 4
);
$loop = new WP_Query($query);
I hope this is helps you
Change your code like this
get_header();
// Reference global $post variable.
global $post;
// Get posts.
$posts = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 4
));
// Set global post variable to first post.
$post = $posts[0];
foreach($posts as $post){
// Setup post data.
setup_postdata( $post );
}
// Output template part.
get_template_part( 'template-parts/products' );
// Reset post data.
wp_reset_postdata();
hope this will helps you
Your query is structured incorrectly as others have mentioned, the best practice I've always known is to use the WordPress loop. Hope this helps!
<?php
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 4
);
$query = new WP_Query($args);
if($query->have_posts()):while($query->have_posts()):$query->the_post(); {
// write your code
}
endwhile; endif;
wp_reset_postdata(); ?>
Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.
Hi I have this code to display only one category but I want to display category 16 and category 40. So only when the users needs to select category 16 and cat 40 then this will be display on the page:
<?php
global $post;
$args = array( 'posts_per_page' => 1, 'offset'=> 0, 'category' => 16 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
setup_postdata( $post );
?>
<div class="categoriesStyle"><?php exclude_post_categories("40"); ?></div>
<div class="first"><?php the_title(); ?></div>
<div class="paddingSpace"></div>
<div class="contentText"><span style="color: #000"><?php echo intro_text(150); ?></span></div>
<hr class="style-two">
<?php endforeach;
wp_reset_postdata(); ?>
To include more than one category you can use cat parameter instead of category
$args = array( 'posts_per_page' => 1, 'offset'=> 0, 'cat' => '16, 40' );
Or cat__in, that accept an array of category ids
$args = array( 'posts_per_page' => 1, 'offset'=> 0, 'cat__in' => array(16, 40) );
Be carefull about offset parameter that overrides $paged parameter (that you don't actually use in the present code, but can help), WP_Query class reference at Pagination Parameters part, says:
offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination (Click here for a workaround). The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
Hope its helps!
I got the solution: I did this: Where main article is the other category. Category 14 is one category and the other category is main article (I didnt put the ID, rather I put the category name and it works!
$args = array( 'posts_per_page' => 1, 'offset'=> 1, 'category' => 14, 'category_name' => 'Main article' );
I currently have this code on my template:
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><?php the_title(); ?></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>
The nature of the code is to generate a list of Random post from blog post. The problem is, the code starts ruining my comments section by displaying the wrong list of comments to unrelated blog post.
see my sample on the link above
The common sense to do, is to remove to code in my template. My question is any ideas on how to fix the code above so i can still use it?
In case you work with get_posts, and you need to override the $post, you'll have to do it like this:
<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><h2><?php the_title(); ?></h2><p><?php the_excerpt(); ?>
</p></li>
<?php endforeach; ?>
</ul>
<?php $post = $tmp_post; // reset the $post to the original ?>
I want to show few random posts at the end of each single post. I found this code for this purpose.
<div>
<h2>Random Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish', 'offset' => 1);
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
</div>
But the problem is that when I use this code on my site, it also cause the random output of next and previous post links present at the bottom of each post for better navigation.
I want to know how to terminate the effect of random posts so that pre and next post links are displayed in their original order.
Try
<?php wp_reset_query(); ?>
More info here : http://codex.wordpress.org/Function_Reference/wp_reset_query