display two categories in post per page wordpress - php

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' );

Related

WP_Query - `category__not_in` not working when showing sticky posts

I'm trying to show the first 4 sticky posts that do not have the videos category and this is my current loop:
<?php $videos_cat_id = get_cat_ID('videos');
$args = array(
'post__in' => get_option('sticky_posts'),
'category__not_in' => $videos_cat_id,
'ignore_sticky_posts' => 1,
'posts_per_page' => 4,
);
$featured_loop = new WP_Query($args);
if ($featured_loop->have_posts()) :
while ($featured_loop->have_posts()) : $featured_loop->the_post(); ?>
<div class="col-sm-6 m-bottom p-left-none p-right-none">
<?php get_template_part('card-featured') ?>
</div>
<?php endwhile; wp_reset_postdata();
endif; ?>
But I'm still seeing posts with the videos category rendered. I'm not entirely sure why this loop isn't respecting category__not_in, any ideas?
Try this:
you may need to supply an array value in category__not_in. And since get_cat_ID() function returns string/int, you may need to this -> 'category__not_in' => array($videos_cat_id)

Why wp_query display posts form another category?

I use this function:
<?php
$args = array(
'posts_per_page' => 6,
'category' => 317,
'orderby' => 'most_recent'
);
$the_query = new WP_Query( $args );
?>
Problem is it displays 4 posts from category "317" and 2 from another category. Why? I would like to have posts only form 317 category. (Now in this category there is 4 posts).

Wordpress: How to make random posts excluding current and from chosen date

I need to have 5 random posts on post page (http://7cuteoutfits.com/2015/07/08/suits-rachel-z-office-fashion/) excluding current post. Random posts should be on chosen dates (for example posts from last 2 months until yesterday )
I added a few lines of code to single.php of my wordpress and now have 5 random posts. So I need to modify the code so that it will meet my requirements (above). I think it's 2 more lines, I'll be very thankful if you help.
<ul>
<?php
$currentID = get_the_ID();
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args);
foreach ( $rand_posts as $post ) :
setup_postdata( $post ); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ul>
You can use WP_Query for that.
global $post;
$args = array(
'post__not_in' => array($post->ID)
'orderby' => 'rand'
'date_query' => array(
array(
'after' => 'January 1st, 2015',
'before' => array(
'year' => 2015,
'month' => 07,
'day' => 9,
),
'inclusive' => true,
),
),
'posts_per_page' => 5,
);
$query = new WP_Query( $args );
This query orders randomly posts between today (inclusive) and Jan. 1st 2015
I haven't tested the snippet here, so please let me know if it does not work for you.
More info on WP_query and its usage (also for date parameters) here
Once you query with WP_Query, you have to
wp_reset_postdata();
just as you are already doing.
EDIT:
To show the post content, you can call
the_content()
to print it directly, or
get_the_content()
to get it as a return value. Then you can handle the printing later with the HTML markup you desire.

How to display recent ten posts of specific category when visiting that specific category's link/url

When i visit mysite.com/category/*** It only displays last post of that category.
But i want it to display recent ten posts of that category.
In short...
I want my site http://bishwash.com.np/category/entertainment/ to display posts like http://www.onlinekhabar.com/category/bichitra-world/
you can get the recent posts with wp_recent_posts($arg,ARRAY_A );
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
You can use get_posts to retrieve posts.
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&category=1');
foreach($myposts as $post) :
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Change category=1 to your category ID number.If you to get post from XYZ category and XYZ category had 13 ID number then it would be something like this category=13.
In categroy.php page before
<?php if ( have_posts() ) : ?>
//paste the following code
$catID = the_category_ID();
$args = array( 'numberposts' => '10','category' => $catID);
$recent_posts = wp_get_recent_posts( $args );
setup_postdata($recent_posts);
At the end of loop
wp_reset_postdata();
The number of posts is set globally for all index type pages (including the home page and archives and category) in settings, reading
It seems you have limited it to 1 ? Then it's better design to set it to 10 because that will apply to all pages, and design a specific template for the home page, where you limit the number of posts with the methods given to to.
You can achieve that in two ways :
- build a page template, with a specific query, create a page with this template, and define it as homepage in settings -> reading
- create a home.php file in your theme
In both cases, it's better to have a child theme, not to loose modifications when your theme is updated.
Create the template (sample.php) and write code like
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
or use CATEGORYNAME
Note: When we click the link it redirects to to the sample.php and the category name is dynamic that means when we click the category its automatically stored to the CATEGORYNAME

What is the meaning of 'OFFSET' => 1 (WordPress)

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.

Categories