I have a code in a page template that looks like this (obviously first I set up IDs of categories):
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $post;
$myposts = get_posts("cat=$catIDs&posts_per_page=12&paged=" . $paged);
?>
<div class="column-odd">
<?php
$i=1;
foreach($myposts as $post) :
if($i%2 != 0) :
setup_postdata($post);
//output odd post
endif;
$i++;
endforeach;
?>
</div>
<div class="column-even">
<?php
$i=1;
foreach($myposts as $post) :
if($i%2 == 0) :
setup_postdata($post);
//output even post
endif;
$i++;
endforeach;
?>
</div>
<nav>
<ul>
<?php
if($link = get_previous_posts_link("« Poprzednia strona"))
echo '<li class="prev">'.$link.'</li>';
if($link = get_next_posts_link("Następna strona »"))
echo '<li class="next">'.$link.'</li>';
?>
</ul>
</nav>
The problem is that get_next_posts_link is not returning anything. How can I make it work?
It won't work with get_posts. You need to use query_posts. I think it should work with the same arguments. edit: you'll also need to save it as $myposts = query_posts( ... ), since you're using two foreach loops like this.
You simply need to add the following line:
<?php query_posts('post_type=post&paged='. get_query_var('paged')); ?>
just before your get_posts() loop starts.
Full article here: http://blog.onireon.it/wordpress-get_posts-and-pagination-posts-are-correctly-displayed-but-no-pagination-links-are-showing/
Related
How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.
include('wp-config.php');
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result = array(
"id"=>$args['ID'],
"type"=>$args['post_type'],
"url"=>$args['guid']);
endforeach;
wp_reset_postdata();
print($result);
Try below :-
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result[] = array(
"id"=>$post->ID, // changed $args to $post
"type"=>$post->post_type,
"url"=>$post->guid);
endforeach;
wp_reset_postdata();
print_r($result);
If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
?>
If you want to display the same in pages other than category page, just add the following code to the corresponding files.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
are you just trying to get posts from a category?
This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;
This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.
On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>
I have multiple loops that I want to display on my WordPress blog. The categories are as follows:
Top-story (1 per page)
Small-story (1 per page)
Normal (1 per page)
Quickfill (2 per page)
I've got 4 loops to display these. I'm trying to display this amount of posts per page and once they've been displayed, don't show them again on another page, as I don't want to show duplicates.
My loops are as follows. I seem to be getting duplicates and when I'm trying to get them to remove duplicates I tend to remove everything without knowing how.
Top story - 1st loop
<?php
global $do_not_duplicate;
$do_not_duplicate = array();
$paged = max(1, get_query_var('paged'));
$my_query = new WP_Query('category_name=top-story&posts_per_page=1&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate[] = $post->ID; ?>
// content
<?php endwhile; ?>
Small-story - 2nd loop
<?php
$my_query = new WP_Query('category_name=small-story&posts_per_page=1&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
$do_not_duplicate[] = $post->ID;
?>
// content
<?php endwhile; ?>
Normal posts (note: the category is actually called normal) - 3rd loop
<?php $my_query = new WP_Query('category_name=normal&posts_per_page=1&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
$do_not_duplicate[] = $post->ID; ?>
// content
<?php endwhile; ?>
Quickfill - 4th loop
<?php
$int = 0;
$my_query = new WP_Query('category_name=quickfill&posts_per_page=2&paged='.$paged);
while ($my_query->have_posts()) : $my_query->the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
$do_not_duplicate[] = $post->ID;
if ($int==0) { ?>
<hr class="seperator" />
<div class="gr_bg_post">
<img src="<?php bloginfo('template_directory'); ?>/images/quickfill.png" />
<?php } ?>
<div class="fl">
<h4><?php the_title(); ?></h4>
<?php the_content('<button type="button" class="read_more_green">Read More</button>'); ?>
</div>
<?php if ($int==1) { ?>
</div>
<?php } ?>
<?php $int++; ?>
<?php endwhile; ?>
Last loop - standard WordPress protocol for display posts... (I think this may have something to do with my duplicates)
<?php
$my_query = new WP_Query(array('post_not_in' => $do_not_duplicate));
if (have_posts()) : while ($my_query>have_posts()) : $my_query>the_post();
?>
// content
<?php endwhile; endif; ?>
I've been trying to figure this out for a few days now and I just can't seem to stop duplicates showing, or get the correct posts to show. Any help is greatly appreciated!
You are missing the query variable $my_query in the last query
$my_query = new WP_Query(array('post_not_in' => $do_not_duplicate));
if ($my_query->have_posts()) : while ($my_query>have_posts()) : $my_query>the_post();
// content
endwhile; endif;
Y try paginate results of wordpress and use this script , the problem it´s when i go paginate , the number of pages show right but if go the links of paginate always show the same content no change in each number of pages
<?php
global $post;
if (have_posts()) : ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=36&posts_per_page=4' );
while (have_posts()) : the_post(); ?>
<div class="cols_posts">
<?php
$imagen = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$ruta_imagen = $imagen[0];
echo '<img src="'.$ruta_imagen.'">';
?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="extracto"><?php the_excerpt(); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
In tye page number one must show the content for that page , in the page 2 the same , etc but always show the same page , the first page content
Regards
The problem is here:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'cat=36&posts_per_page=4' );
You are getting $paged variable correctly but you are not using it in a query.
So add it to the query like this:
query_posts( 'cat=36&posts_per_page=4&paged='.$paged );
You can use a function for this. For that, visit this page
http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/
I am trying to group the posts by Category in a Monthly archive but don't know how to achieve that.
Can someone here help me please.
Regards
This is the php code I am using in archive.php
<?php
$terms = get_terms("publicationcat");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
echo "<div class=\"publication-area\">";
echo '<h3 class="term-heading">' . $term->name . '</h3>';
echo "<div class=\"publication-txt\">";
echo "<ul>";
while ( have_posts() ) : the_post();
echo "<li><a target=\"_blank\" href=\"" . wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)) . "\">".$post->post_title."</a></li>";
endwhile;
echo "</ul>";
echo "</div>";
echo "</div>";
}
}
?>
The only problem is that its displaying same post titles for all the terms..
When you get to looping through the post in each category, you are not finding only the posts related to that category, and are instead looping through EVERY post. The below (untested, so comment if you get errors) should sort your problem.
Please note however that you may still get some repetition, if a post is assigned to more than one category.
Check each post to ensure it is in the desired category. Good point - only one query. Bad point - loops through all posts for every cat.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php if($my_query->have_posts()) : ?>
<ul>
<?php if(in_category($term->slug)) : ?>
<?php while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php
endforeach;
endif;
?>
Re-query the posts and output all that are grabbed. Good point - only pulls only the posts required for the category being looped. Bad point - lots of queries, so many queries could slow your site down.
<?php
$terms = get_terms('publicationcat');
if(!empty($terms)) : foreach($terms as $term) :
?>
<div class="publication-area">
<?php sprintf('<h3 class="term-heading">%1$s</h3>', $term->name); ?>
<div class="publication-txt">
<?php
$args = array(
'cat' => $term->term_id
);
$my_query = new WP_Query($args);
?>
<ul>
<?php if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $link = wp_get_attachment_url(get_post_meta($post->ID, 'document_file_id', true)); ?>
<li><a target="_blank" href="<?php echo ($link !== '') ? $link : ''; ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
</div>
</div>
<?php
endforeach;
endif;
?>
I am using the code below to attempt to display a list of tags associated with posts in the category 'html'
<ul>
<?php
query_posts('category_name=bikes');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
?>
</ul>
I am not seeing any results when i run it though, I have checked and there are lots of tags associated with the posts in the category.
Can anyone help?
You'll have to remove the $posttags = since you don't want to assign a variable but output it
<ul>
<?php
query_posts('category_name=bikes');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
?>
</ul>
A better way to get the results you're looking for would be not to use query_posts at all. Rather, use a new query to add to your loop. If my category were named photography, I would use this:
<ul>
<?php $photographyTags = new WP_Query(array('category_name' => 'photography')); ?>
<?php if($photographyTags->have_posts()) : while($photographyTags->have_posts()) : $photographyTags->the_post(); ?>
<?php
if( get_the_tag_list() ){
echo get_the_tag_list('<li>','</li><li>','</li>');
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
</ul>