Strange behaviours getting all post from wordpress category - php

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)

Related

Am looking for a way to show random products in products category page using woocomerce

How do I change product in same category randomly? I've been looking but can't seem to find any plugin/script to do this, anyone got an idea on this... thanks
You can use the following code to display the products rows of "CATXXX" :
<?php echo do_shortcode( '[product_category category="CATXXX" per_page="8" columns="4" orderby="rand"]' ) ?>
Also you can treat products like any other post type and query using get_posts(). Limit the number of posts retrieved to 1 and change the orderby parameter to random:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category' => 'CATXXX'
'post_type' => 'product');
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();
You can also use new WP_Query() which would be similar.
You can try this code shortcode
[product_category category="category_slug" per_page="10" orderby="rand"]
Or
<?php echo do_shortcode( '[product_category category="category_slug" per_page="10" orderby="rand"]' ) ?>

Extract title and add to post in Wordpress

I have a Wordpress site with 500+ posts. I would like to extract the title of each post, wrap it with some span class code, add the title and code to the end of each post (the same post from which it was extracted), and hide its visibility. How would I go about doing this in Wordpress/PHP?
Thanks!
EDIT: #unixmiah, would this be achieved like so?
<?php
global $post;
$args = array( 'numberposts' => 500, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li span class="name hidden"><?php the_title();?></li>
<?php endforeach; ?>
Which file do I insert this into? functions.php? Also, how do I ensure this code will run site-wide?
You can use the code below to accomplish this with wordpress internal functions to get that information in a loop.
For example
<?php
global $post;
$args = array( 'numberposts' => 500, 'offset'=> 0, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
your javascript span question seems vague, do you mean to hide it and show it on mouseover?

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 determine category name of returned Posts in WordPress

What i'm trying to do is find out what category the Posts are when they are returned so i can put them in specific divs.
e.g. i have #div1 #div2 and #div3 and i have 3 posts returned each with a different category.
What i want to do is have 1 post each in a div. To do this i need to determine the category.
Here is how i fetch Posts by category but how do i fetch Posts of 3 categories and determine the category of the Posts returned, so i can change the div id?
<?php
//gallery
$args = array( 'category_name' => 'Clubs');
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div1">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
Obviously i could duplicate this code twice and just change the category and div names but is that the right way to do this?
To avoid copy-paste, I'd suggest putting your code in a function, which takes the parts you want to change as parameters. Something like this (I added the posts_per_page parameter, since you said you only want one post per div):
<?php
function show_gallery($category, $id) {
$args = array( 'category_name' => $category, 'posts_per_page' => 1);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<div id="div<?php echo $id; ?>">
<?php the_content(); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
}
show_gallery('Clubs', 1);
show_gallery('Hearts', 2);
show_gallery('Diamonds', 3);
?>
I haven't tested, but hopefully it works and is enough to get you started.
I'd put the function declaration at the top of your template.

Issue with accessing category for post formatting in WordPress

I am creating separate templates and I am trying to have post query based on the category but for some reason it seems not to be working. If you could give me a hand that would be great. I am just getting every single post no matter what category.
Code:
$args = array( 'numberposts' => 100000000000, 'offset'=> 1, 'category' => 'Uncategorized' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<!--<h2><?php //the_title(); ?></h2>-->
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endforeach; ?>
Read get_post you will see many note over there one of which says
Note: The category parameter needs to be the ID of the category, and not the category name.
So give category ID for your category "Uncategorized".

Categories