Query all posts in a certain category - php

I've been trying to show all post in a page of a certain category. Here's what I did so far:
<?php
$args = array(
'category_name' => 'diy-tutorial',
'post_type' => 'post',
'posts_per_page' => 3
//'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
$x = 0;
while (have_posts()) : the_post(); ?>
It's actually working but it wouldn't let me navigate for the next pages of the categories Like for example, i have 30 posts in that certain category, so my code will show me three posts per page right? The next>> link won't show up as well as if I change the number of posts to show per page, it won't work as well...
If you have an alternative way of showing it please let me know.

sorry I am a little rusty on my wordpress, but the way I like to get posts is by passing a querystring to get_posts
$catID=get_cat_id('diy-tutorial');
$queryString="cat=".$catID."&numberposts=1000";
global $post;
//execute the query from the string
$myposts = get_posts($queryString);
//loop thru and do stuff
foreach($myposts as $post) :
setup_postdata($post);
the_post();
endforeach;
see the codex here: http://codex.wordpress.org/Template_Tags/get_posts

I believe you have to add the buttons and possibly build up the function: Read the Codex friend: https://codex.wordpress.org/Pagination#Function_Reference
Also, I believe the commented out part of your code must be active to declare paged.
<nav class="wp-prev-next">
<ul class="clearfix">
<li class="prev-link"><?php next_posts_link(__('« Older Entries', "bonestheme")) ?></li>
<li class="next-link"><?php previous_posts_link(__('Newer Entries »', "bonestheme")) ?></li>
</ul>
</nav>

Related

Wordpress custom post type menu generation

The problem: I have a site (under development) where I have generated a custom post type called "Products" . Inside I have different categories and posts. I would like to generate a menu like behaviour to my sidebar, but using google I haven't really stumbled onto something good. Who knows, maybe my idea is all wrong and is doable using some other solution.
Currently I have used WP Query to echo out all posts under one category. So this means that I have to manually copy code to echo new category with its posts when it is created. One solution is to create a menu manually, but i'd like to keep it auto-generated because users are not keen to do extra work and ofter forgot how it is done.
So, the question: Is my solution all wrong? How can i achieve menu like behaviour with class active for clicked category and its sub item? Is it better to just use pages?
Example of my code:
<?php
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'cat' => 1
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
echo '<ul>';
while ( $query->have_posts() ) : $query->the_post(); ?>
<li>Category name, title</li>
<li class="cpt-menu-item" id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php endwhile;
echo '</ul>';
}
wp_reset_postdata();
?>

How to show all posts of submenu which is a category

I need to show all the posts of the submenu/category in my own theme. The submenu is a category. My menu looks like this:
- about (page)
- references (page)
--reference1 (subpage)
---test1 (submenu/category)
--reference2 (subpage)
---test2 (submenu/category)
I can get all the posts by using the ID of the category:
<?php
query_posts('cat=5');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
But I need the category-ID dynamically (that the reason, why I define the category as submenu in my custom menu). But maybe there is an other way?!
When I am on the page reference1, I would like to show all the posts of the category test1. On the page reference2 I would like to show all the posts of the category test2. I can not figure out, how I can realize that. Any help?
Greetings and thanks in advance,
Yab86
Have you tried something like that:
<?php
$id = 5;
$args = array( 'category' => $id, 'post_type' => 'post', 'order' => 'ASC', 'posts_per_page'=>-1, 'numberposts'=>-1 );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endforeach; ?>
You will find further information on:
https://codex.wordpress.org/Template_Tags/get_posts

how to get the posts from my index page to someother pages(like blog) in WordPress

i am beginner to WordPress, i have created the index.php it automatically shows my posts.. but i wanna posts to show also in another php file like blog.php.. how can i retrieve the same posts which is shown in index.php.........
The scenario is, i am developing theme for http://themeforest.net show that i wanna make some features in my blog page like without sidebar, left sidebar, right sidebar... but my home(index.php) contains posts.. whenever i starts new page with same coding(like blog.php), it doesn't show the index.php 's posts..
This sample piece of code retrieves 3 posts from categpry id 4
<?php
$args = array( 'numberposts' => 3, 'order'=> 'DESC', 'orderby' => 'post_date', 'category' => 4 );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post);
$a= get_the_date();
?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php endforeach; ?>
Refer the WordPress documentation http://codex.wordpress.org/The_Loop
it contain different example how to display post include or exclude post from specific categories etc..

Strange behaviours getting all post from wordpress category

today i need to get all the posts from a specific category on wordpress.
The code it's pretty basic, and it's like that:
print_r(get_posts(array('numberposts' => -1, 'category' => 3)));
The id of the category it's obviously 3.
But i receive always the last 5 posts of that category, not all the post present there (something like 60posts).
Anyone know why could happen this strange stuff?
Try something like this:
<ul>
<?php
$args = array( 'numberposts' => -1,'category' => 3 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; wp_reset_postdata();?>
</ul>
If this works for you, all you need to do then is to change the output display(the ul and li)

Wordpress: Limit posts per page in category

In my index.php I use this code to limit the posts per page and it works without any problems:
$showposts = 5;
$do_not_show_stickies = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('category__in' => $cat, 'showposts' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
$loop2query = new WP_Query($args);
query_posts($args); if(have_posts()) : while (have_posts()) : the_post(); ?>
<div class="blogpost"> ... </div>
<?php endwhile; endif;
posts_nav_link(); // Navigating the pages with $showposts each. ?>
The same code did not work in category.php so I changed it to the following but it still does not work:
$showposts = 4;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if (have_posts()) { while (have_posts()) { the_post(); ?>
<div class="blogpost"> ... </div>
<?php } }
else { ?>
<p>There are no blog posts in this category.</p>
<?php } ?>
<?php posts_nav_link(); // Navigating the pages with $showposts each. ?>
I tried to change the line with if(have_posts()) : while (have_posts()) : the_post(); ?> [...] in category.php to make it similar to that line in index.php but nothing I tried worked.
Wordpress has a setting for this, found in the admin area under SETTINGS -> READING -> Blog pages show at most
You can use this instead of custom-modifying your queries. It may make it a little easier to maintain your project down the road.
Use the posts_per_page argument (Codex here)
$args = array('category__in' => $cat, 'posts_per_page' => $showposts, 'ignore_sticky_posts' => 1, 'paged' => $paged);
In you first example, you actually have two queries : new WP_query, then query_posts, so you need to get rid of one of them, as this is redundant. In the second exemple, it is the contrary, you do not have any query (although WordPress might execute one by default, depending on where this page is called). So anyway, there is no point in using $showposts in your 2nd example, as you are not executing a query after...
if (have_posts()) is generally used to treat a default (not visible in your page code) loop from WordPress or to treat a query that you declare just before (generally with query_posts()).
As #Samuel is saying, the argument to use is posts_per_page, but I think you are not there yet and you should first start to learn how to execute a query, so you can start by reading the WordPress codex on query_posts, it will be the best place to go first : http://codex.wordpress.org/Function_Reference/query_posts.

Categories