How can I check if a post is in a specific year? I need to output some code if the single post is in a specific year.
Inside of the wp loop you can get the date of the post with the built in wp functino of the_date('Y')
So in the wp loop just check to see if the date == something like so.
<?php
if ( have_posts() ) {
while ( have_posts() ) {
if(the_date('Y') == '2015'):
the_post();
else:
continue;
endif;
} // end while
} // end if
?>
Related
In my theme, I have a custom post type called sliders. Here the user is allowed to upload images in a meta-box. So, the images are saved as meta data in wp_post_meta table.
Now what I want to do is, displaying a slider using its ID.
I did like following but no result
$my_query = new WP_Query('post_type=sliders&p=411');
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
the_post();
endwhile;
}
This doesn't display anything. No even errors. Anyway, If I used the_title() instead of the_post(), it shows the title of the slider fine. Same for the the_author() It shows the author without error.
Why is this weird ?
According to Codex the_post(); :
the_post(); iterate the post index in The Loop. Retrieves the next
post, sets up the post, sets the 'in the loop' property to true.
This function does not return any values.
Example for how we use this function:
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
he_title();
the_content();
}
}
?>
You didn't explain well you question. anyway to display your metabox just use API function
echo get_post_meta( $id, 'metabox_name', true );
I'm having a normal wordpress loop, but I want to always hide the most recent post from a specific category from this loop. Any ideas?
<?php
$categories = array();
if ( have_posts() ) {
while ( have_posts() ) {
$category = the_category();
if (in_array($category)) {
the_post(); // code after the 1st one is skipped.
} else {
$categories[] = $category; // when it's the first post, add the category so the next posts will display.
}
} // end while
} // end if
?>
This should be the basic structure of your loop.
Logic: If the category is the first, it doesn't exist in $categories, so it adds it and moved on the the next post, if that category is already in the array it displays the code.
Use a boolean variable to check whether you've already skipped one post in that category, like this:
<?php
$post_has_been_skipped = FALSE;
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
if (!$post_has_been_skipped AND in_category("your-category-slug")) {
$post_has_been_skipped = TRUE;
continue;
}
//
// Post Content here
//
} // end while
} // end if
?>
I am using a while loop
while (have_posts()) : the_post();
to get all the post and displaying a page. How to randomise the post. But the problem is, in the page I am working on there is no sign of 'query'. Also I tried to use some solutions mentioned but no result.
You can alter the main query with query_posts($args) this way:
// Alter the query
query_posts( 'orderby=RAND' );
while ( have_posts() ) : the_post();
// Do your stuff inside the loop
endwhile;
// Reset the query
wp_reset_query();
I need a little help on Wordpress searching. The thing I want to know is how to search Wordpress pages only.
You might want to check out this page on the wordpress forums.
You have to add a hidden form field in your search form and add a hook to update the query that wordpress generates.
Edit: Here's the code from the forum post mentioned above.
In the search form:
<input type="hidden" name="post_type" value="page" />
In functions.php:
function mySearchFilter($query) {
$post_type = $_GET['type'];
if (!$post_type) {
$post_type = 'any';
}
if ($query->is_search) {
$query->set('post_type', $post_type);
};
return $query;
};
add_filter('pre_get_posts','mySearchFilter');
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='post')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='post')) continue; ?>
Let me know if it worked.
We have selected to show 5 posts per page in admin panel.
And we want to show 10 posts per page in specific category (for example, "projects" width id=2).
How would we do it?
Change the normal loop to a query post. Like
if ( is_category(2) ){
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile; else:
echo'Nothing here...';
endif;
//Reset Query
wp_reset_query();
}
This should be used in the main loop only. . If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead.
Cheers
Pali Madra
I had the same problem.
This worked for me:
if ( is_category(2) ){
global $query_string;
query_posts( $query_string . "&posts_per_page=500" );