I know how to use few loops without duplicate posts.
But My question is:
Suppose I have two loops, each two loop shows 1 post only, both of them have the same newest post. if I use the code below, the duplicate post will be not shown at the second loop, but it is also stop to continue with the next post. how to solve it. million thx!
code:
<?php $my_query = new WP_Query('cat=1,2&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
first loop
<?php endwhile; ?>
<?php query_posts('cat=10&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); if (in_array($post->ID, $do_not_duplicate)) continue;?>
second loop
<?php endwhile; endif; ?>
I think you need to clean the example up a little bit:
You have $do_not_duplicate = $post->ID;, assuming your variable is the String post_id, then you check if (in_array($post->ID, $do_not_duplicate)) continue;
At this point $do_not_duplicate is NOT an array().
If you want to store an array(), try this: $do_not_duplicate[] = $post->ID;, then you can perform your current check.
Related
So I query the posts, I get back everything that matches, then do my loop - but I want to do some sort of print_r type thing on each loop so I can see 100% of what data is being fetched. I've got a total brain block here.. Help!
Here is my query, and then my loop:
<?php query_posts('post_type=property'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
Just can't figure out how to echo out 100% of what was fetch through each iteration of the loop?
Thanks for the help everyone!
Does it have to use the loop? If not then I would try:
<? $posts_array = get_posts('post_type=property');
print_r($posts_array); ?>
inside your loop
print_r($post);
I like to add the following to the top of any of my single php pages where I want to see the output...
<?php global $post; var_dump($post);die;?>
and in the loop :-
the_post();
global $post;
print_r($post);
http://wordpress.org/extend/plugins/simple-timed-plugin/
Im using this function to dissapear(not delete) expired posts:
<?php if (function_exists('simple_timed_content')) : ?>
<?php if (simple_timed_content("offdate=20120309 offtime=2350")) : ?>
Some content goes here
<?php endif; ?> <?php endif; ?>
I want to use as dates from custom fields like this:
<?php if (simple_timed_content("offdate='$end_date' offtime='$end_time'")) : ?>
where : `
$end_date=date( 'Ymd', strtotime( get_post_meta( $Post->ID, "_EventEndDate", true), time()));`
and
$end_time=date( 'HM', strtotime( get_post_meta( $Post->ID, "_EventEndDate", true), time()));`
Assuming allways that _EventEndDate value is : Ymd HM (20120309 2350)
How would be the complete code for this to work?
My other option is to use this code i found :
www.rockia.com/2010/01/modify-you-wordpress-theme-to-enable-an-expiration-for-your-posting
but it doesnt seem to work with my
www.wordpress.org/extend/themes/bombax
where loop is in single.php file
If someone could help I would appreciate it. Im new to php.
Thank you in advance
I have different way to accomplish this try below
Edit your theme and replace your current WordPress loop by this "hacked" loop:
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$disappeartime = get_post_custom_values('disappear');
if (is_array($disappeartime)) {
$disappearstring = implode($disappeartime);
}
$secondsbetween = strtotime($disappearstring)-time();
if ( $secondsbetween > 0 ) {
// For exemple...
the_title();
the_excerpt();
}
endwhile;
endif;
?>
To create a post with date/time disappear, just create a custom field. Give it disappear as a key and your date/time (format: mm/dd/yyyy 00:00:00) as a value.
The post will not show after that time stamp.
Doing something like this:
<?php $my_query = new WP_Query('category_name=process&posts_per_page=20');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
Destroys the original reference to the single post, which I would want to list afterward. What is the best way to resolve this? I have tried both of the multiple loop examples on the wordpress doc page, and either I misunderstood them, or they don't work. A friend suggested that the only way to do it was to store the original post ID and then to call that post by ID afterward. If that's the solution, then I could use:
<?php $thePostID = $post->ID; ?>
To get the post id, but how do I query a single post by id?
Thanks in advance.
To query single post by ID you can use get_post( $post_id );, for usage check http://codex.wordpress.org/Function_Reference/get_post
A few things to understand before my question will make sense:
I use a hidden category called 'Unique' to specify if the post will use the single.php or a special one used for the unique ones.
I want the index to act as a single: showing only one post, displaying next/prev post links, and comments also.
I need the index.php to say if the post is in category 15 (unique) than <the_unique_content>, else; <the_default_content>
My loop does all this, but the problem is that if the current post is unique, it also displays 1 additional post below the unique post.
Here is the loop >
<?php $wp_query->is_single = true; ?>
<?php $post_count = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php if ($post_count == 0) : ?>
<?php if (in_category('15')) { ?>
<?php the_content(); ?>
<?php } else { ?>
<?php the_content(); ?>
<?php $post_count++; ?>
Thanks for any help!
I don't think you are setting the query correctly to return a single post. Your code is limiting the number of posts through your $post_count variable, but in the case where the post is "unique", it only increments to 1 on the second post.
Here is one way to limit the number of posts to a single post. It involves modifying the loop query to set the number of posts per page to one.
<?php
global $wp_query;
$new_query = array_merge( array( 'posts_per_page' => 1 ), $wp_query->query );
query_posts( $new_query );
if (have_posts()) : while (have_posts()) : the_post(); ?>
etc...
I am using the following code to try and display posts from only a certain category horizontally in three rows. I have the horizontal display issue figured out (using css) but with the following code it displays all posts and not posts from specific category.
<?php query_posts('showposts=5'); ?>
<?php query_posts('cat=7'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
Any help would be greatly appreciated.
You're misunderstanding some concepts in query_posts and get_posts.
query_posts is to be used inside the loop. get_pages isn't. If you want to use query_posts, you don't need to create the get_pages call. Use query_posts or get_pages to accomplish what you're trying to do.
You need to combine your category parameters in query_posts.
<?php
query_posts('showposts=5&cat=7');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
If you want to do the same logic but without The Loop, just call
$posts = get_posts('numberposts=5&offset=0&category=7').
Read the links I provided. They have all information you need to understand how to do what you need.