This get_posts function displays all the articles of the categories except one.
numberposts => -1 is specified to retrieve all the articles in a category.
why am I not showing them all?
Thx!
<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => -1, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post )
{ setup_postdata($post); ?>
<div class="article">
<a class="article_link" href="<?php the_permalink(); ?>" title="En savoir plus"><?php the_title(); ?></a>
</div>
<?php
} wp_reset_postdata(); ?>
Because in your get_posts() function, you are passing the parameter:
'post__not_in' => array($post->ID)
So the get_posts() would retrieve all posts, except the current one (which you are passing via $post->ID)
Here's the screenshot of the documentation:
Related
I'm trying to create a loop that goes at the end of certain posts that will pull posts from related categories and exclude the current post. I feel like I'm close but it's still not quite working. It's just pulling all posts currently for some reason. Any help would be much appreciated.
$args = array(
'posts_per_page' => 8,
//'paged' => get_query_var('paged'),
'post__not_in' => array($post->ID),
'category__in' => $cat_ID
);
// The Query
query_posts( $args );
// The Loop
while ( have_posts() ) : the_post(); ?>
<div style="max-width: 1350px; margin: auto;">
<div id="post-container02">
<div class="post-image"> <?php the_post_thumbnail(); ?></div>
<div class="post-feed-title"> <?php the_title(); ?></div>
<div class="read-more-button">Read More</div>
</div><!-- post-container01 -->
You may want to try this method within your main post loop. It will find the related categories and avoid the current category.
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post);
/*whatever you want to output*/
}
wp_reset_postdata();
https://wordpress.stackexchange.com/questions/30039/how-to-display-related-posts-from-same-category
I am trying to display posts from two categories on one page (6 & 7). It should include posts from either category as well as the ones matching both. Unfortunately I have so far only found solutions for the latter which is why I am asking you for help now. This is my current code:
<div class="sponsoren_container_wrapper">
<?php $args = array ( 'posts_per_page' => -1, 'category' => 6 );
$myposts = get_posts ( $args );
foreach ($myposts as $post) : setup_postdata( $post ); ?>
<div class="gallery_image_container">
<a href="<?php
$content = get_the_content();
print $content;
?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
How would I be adding the category ID "7" to that code to make everything work as explained above?
Thanks in advance!
use a comma seperated string:
$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
http://codex.wordpress.org/Template_Tags/get_posts
$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
$myposts = get_posts ( $args );
$args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
$myposts = get_posts ( $args );
For me, 'category' would not pass but 'cat' did. I hope this helps someone.
I found this excellent function to display all the posts listed under a specific custom taxonomy. It works great. Found here http://gilbert.pellegrom.me/wordpress-list-posts-by-taxonomy I tried a number of ideas, however unsuccessful, to try and paginate the returned data. I either get no data or it continues to display the entire list. Some of my taxonomies have over 10K posts associated. So pagination would seem logical.
What I want to do is; have the information that gets returned create pages of 'n' number of posts and make links for the other pages (1,2,...4,5 etc). Any help is greatly appreciated.
I tossed this in my functions file;
function list_posts_by_taxonomy( $post_type, $taxonomy, $get_terms_args = array(),
$wp_query_args = array() ){
$tax_terms = get_terms( $taxonomy, $get_terms_args );
if( $tax_terms ){
foreach( $tax_terms as $tax_term ){
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
);
$query_args = wp_parse_args( $wp_query_args, $query_args );
$my_query = new WP_Query( $query_args );
if( $my_query->have_posts() ) { ?>
<h2 id="<?php echo $tax_term->slug; ?>" class="title">
<?php echo $tax_term->name; ?></h2>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
}
wp_reset_query();
}
}
}
?>
And this code goes in the template, stuff whatever 'taxonomy' name and it displays the data. Another questions I was not sure about, if the pagination should go in the function or the template.
<div class="my_class">
<?php
list_posts_by_taxonomy( 'my_posttype', 'taxo_mytaxo' );
?>
</div>
Thank you everyone!
In order to have pagination with your query first of all you have to use the paged parameter.
To get some extra info check the wordpress codex for Pagination
Usually you get the pagination variable like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
Then you pass it to the query by including it in the query arguments for your code:
$query_args = array(
'post_type' => $post_type,
"$taxonomy" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'ignore_sticky_posts' => true
'paged' => $paged //I've added it here
);
Then you'll have to build the pagination links something like(this will be done inside the loop):
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
I have a wordpress website and in the custom template for the category 10 I have this code:
<?php
function new_excerpt_length($length) {return 30;} add_filter('excerpt_length', 'new_excerpt_length');
global $post;
$args = array( 'numberposts' => 5, 'category' => 10 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
echo get_the_post_thumbnail($post_id, $size)?></div><div id="eu_post_category">
<h2><a class="roll-link" href="<?php the_permalink(); ?>"><span data-title="<?php the_title(); ?>"><?php the_title(); ?></span></a></h2>
<h6><?php the_excerpt(); ?></h6>
<?php endforeach; ?>
This returns the last five posts in my category. But I do not want to lose the older posts. I want to have two buttons newer posts||older posts at the bottom in order to let the user see all the posts.
What I've tried: setting the max number in settings-> reading and to use this code in functions.php (with no results):
function limit_posts_per_archive_page() {
if ( is_category('10') )
set_query_var('posts_per_archive_page', 5); // or use variable key: posts_per_page
}
add_filter('pre_get_posts', 'limit_posts_per_archive_page');
Thanks!
< when u click on new posts/older posts button get the page number(which is stored inside $paged varible) pass this inside the arguments
<?php
$args = array(
'paged' => $paged,
'category' => 10,
'posts_per_page' => 5,
'post_status' => 'publish');
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) { the_post();
echo $post->post_title;
}
}
?>
for references:
http://codex.wordpress.org/Function_Reference/query_posts http://codex.wordpress.org/Template_Tags/get_posts
I wrote a query to show related posts in Wordpress,
A brief explanation:
First it checks what categories current post belongs to,
then puts "slug" name of that categories inside an array ("$cats")
Then using foreach, I query the posts with same taxonomies.
This code works great except for when current post belongs to more than one category and there is another post which belongs to these categories too, in this situation the other post gets displayed twice or even more depending on how many categories they share,
So the question is, How do I check if a post is repeating and how to prevent it?
Code:
<?php
$terms = get_the_terms( $currentid, 'taxonomy' );
$cats = array_map(function($a){ return $a->slug; }, $terms);
foreach ($cats as $cat) {
$args=array(
'taxonomy' => $cat ,
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4,
);
global $wp_query;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
if($currentid !== get_the_ID()){
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php
if ( has_post_thumbnail() ){
$appfetured = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ));
$appfeturedimage = $appfetured[0];
}else{
$appfeturedimage = '../img/defaultappicon.png';
}
?>
<img class="appfeatured" src="<?php echo $appfeturedimage; ?>" alt="<?php the_title_attribute(); ?>" />
</a>
</li>
<?php
}
endwhile;
wp_reset_query(); // Restore global post data stomped by the_post().
}//end foreach
?>
If all you are trying to do is get all of the posts in all of the categories for the current post try this:
<?php
$args = array(
'cat' => wp_get_post_categories($currentid),
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4
);
new WP_Query($args);
It's been a while since I've used Wordpress so I don't know if there's a difference between the categories and the taxonomies so you can try this:
<?php
terms = get_the_terms( $currentid, 'taxonomy' );
$cats = array_map(function($a){ return $a->slug; }, $terms);
$args = array(
'taxonomy' => $cats,
'post_type' => 'apps',
'post_status' => 'publish',
'posts_per_page' => 4,
);
global $wp_query;
$wp_query = new WP_Query($args);