I want to show comments of a post on single post page, but in that page I show the other post with same category too. when I use comments_template(); the comments of other post shown too. here is my code
<?php if(have_posts()): while(have_posts()): the_post(); ?>
// show the single post and get the category of post
<?php endwhile;endif; ?>
if(isset($cats_arr) && !empty($cats_arr)){
$args = array(
'category__in' => $cats_arr,
'posts_per_page'=>3,
'post__not_in' => array( $post->ID ) );
$the_query= new wp_query($args);
if($the_query->have_posts()): while($the_query->have_posts()):
$the_query->the_post(); ?>
//// shoe same category post title and image
<?php endwhile;endif ?>
<?php comments_template(); ?>
Can you help me find my problem?
Thanks a lot
add wp_reset_postdata(); to end of second query after if statment
Related
I'm trying to create a set of WP pages with indices to all posts which have tag X as well as category Y.
I found this which seems to do exactly what I want and I understand how to edit it for each index page, but I don't know where/how to use the actual code to create the index pages.
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1); //get all posts
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;
TIA for your help.
This is what finally worked - note that I needed the category ID but the tag slug.
<?php if ( have_posts() ) : ?>
<?php query_posts( 'cat=6&tag=a1&&orderby=title&order=asc' );?>
<?php while ( have_posts() ) : the_post();?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php else : ?>
You use that code to replace the basic query in the loop in your template.
https://codex.wordpress.org/Function_Reference/query_posts
I am trying to figure out how to sort the search results by post type. I asked the question on wordpress stack exchange and someone tried answering but I don't think they understood what I was trying to achieve.
-- https://wordpress.stackexchange.com/questions/72914/search-results-sorted-by-post-types
For example, when a person searches a term they are taking to the page
with the results, all >posts found from all post types are shown but
up top there are the different post types name >links that will sort
the results and show only the respective post type's post. Right now I
have several loops on the search results page for each post type but when I test it, the >loops are all showing the same results even
though each loop has a query for a different post type.
My code for the search page - http://pastebin.com/L9zEw1cn
This is a little above the first loop <?php global $wp_query; $total_results = $wp_query->found_posts; ?> This is the first loop <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs// <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
Then the second loop which is the same for all the others except the post type name -- <?php $args = array( 'post_type' => 'videos', 's' => $s ); ?> <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
How can I fix it so that each loops display the search results for only that post type?
Try using get_posts for the second loop.
$args = array(
'post_type'=> 'videos',
'numberposts' => -1,
's' => $s
);
$videos = get_posts( $args );
foreach( $videos as $post ) : setup_postdata($post);
// My divs
endforeach;
http://codex.wordpress.org/Template_Tags/get_posts
Edit: added 'numberposts' to the arguments.
I displayed all the post from all the post type in the page. Now i want to display all the latest post from all the post type. I want only one post from all the post type.
To display all the post from all post type i used the following code.
<?php query_posts(array('post_type'=>array('a','b','c'), 'posts_per_page' => 2)); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="issue-content">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?>
<?php get_template_part( 'content-issues', 'page' ); ?>
<?php comments_template( '', true ); ?>
</div><!--issue-content-->
<?php endwhile; // end of the loop. ?>
i tried with the following code
<?php query_posts(array('post_type'=>array('a','b','c'), 'posts_per_page' => 1)); ?>
i cant get from all the post type. How can i get the latest post from all the post type.
You should use multiple query_posts()
query_posts('post_type=a&posts_per_page=1');
query_posts('post_type=b&posts_per_page=1');
query_posts('post_type=c&posts_per_page=1');
You can also use, get_posts(); function for getting all posts from every post types.
hope you guys can help me.
I have two very simple custom loops from one category: one sticky, one non-sticky. I want the sticky posts to only display on the first page, and not on the rest of the pages when I paginate. There must also be exactly 8 post (including the Sticky posts on page 1) on every page. All this is working fine with the code I got below.
The problem is this: Page one: 1 sticky & posts #15-9, pages two: post #7-1. WP skips one post (#8) on the second page because of the sticky post. Anyone got a solution for this? I would appreciate the help very much.
<!-- Sticky -->
<?php if ( $paged != True ): ?>
<?php $wp_query = new WP_Query(array('post__in' => get_option('sticky_posts'), 'category_name' => Work)); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop', 'index' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
<?php endif ?>
<?php
$postcount = $wp_query->post_count;
$postnumber = 8;
if ( $paged != True ){ $postnumber = $postnumber - $postcount; }
?>
<!-- Non-Sticky -->
<?php $wp_query = new WP_Query(array('post__not_in' => get_option('sticky_posts'), 'category_name' => Work, 'posts_per_page' => $postnumber, 'paged' => $paged)); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop', 'index' ); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
take a look at my answer to a similar question here:
Can post_nav_link navigation work with WordPress custom page templates?
the key is offset
I haven't worked much with wordpress, what I am trying to do is have a page that displays random posts.
It would be similar to my main page where the latest posts are shown but it would display random posts every time the page is refreshed.
If the main page is at http://example.com i want my random page to be at http://example.com/random
how to do it?
The orderby argument to get_posts accepts the value rand.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
More info in the documentation: http://codex.wordpress.org/Template_Tags/get_posts#Random_posts
Enable permalinks for you wordpress installation by visiting http://example.com/wp-admin/options-permalink.php, If you would like the title of the post to be the last url segment you would select Custom Structure and set this value to %post_name%
Create a new page template in your current theme folder as described here: Creating Your Own Page Templates
Create a new page witht the title Random and select your newly created page template under Page Attributes in the pages edit screen.
Then in your page template you would do something like this as Filip suggested:
<ul>
<?php
$args = array('posts_per_page' => 5, 'orderby' => 'rand');
$rand_posts = get_posts($args);
foreach ($rand_posts as $post) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Try This ...
1.First a create a custom page template. Name it as random post or a name of your choice!
2.Open the page and remove the default wp loop and Paste the code below
3.To change the no of post change the number ‘1’ to your choice!
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>
source: http://www.yengkokpam.com/displays-random-posts-in-a-page/
You can use this code to display random posts on your page.
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile;
endif; ?>