Does it show any posts?
Try it with a date_query
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$today = getdate();
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'date_query' => array(
array(
'year' => $today["year"],
'month' => $today["mon"],
),
),
);
$posts = get_posts($args);
?>
http://codex.wordpress.org/Function_Reference/WP_Query#Date_Parameters
Doesn’t seem to work. I am basically building an archive system manually.
You go to recent posts and and shows the most up to date posts at the moment that is November. But when it turns to December and i click the November archive i want it to display only posts from November. and then when i click the December archive it only shows posts from December.
I say manually because then all i have to do is change either a number or a word in the programming for each new paged archive.
SO at the moment i have:
blog/index.php <- Recent Posts (Contains a sidebar where the archives are)
blog/archives/november2013.php <- Only November Posts
—
when December hits i will copy the code from november2013.php and make a new page called december2013.php and just change the number or word that gives it the month thus making it work for December 2013.
If you name your archive pages like this november-2013.php (with a dash between month and year) you can get the the year and the month from the filename and use it to query posts like this:
<?php
list( $year, $month ) = explode( '-', basename( __FILE__, '.php' ) );
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'date_query' => array(
array(
'year' => (int) $year,
'month' => (int) $month,
),
),
);
$posts = get_posts( $args );
?>
If you would use a theme WordPress would have date archives out of the box and more.
So it would be this? Because this is not working. Sorry for the hassle.
http://cameronandrews.com/blog/archives/november-2013.php
<?php
require('blog/wp-blog-header.php');
require('blog/wp-includes/general-template.php');
?>
<?php
list( $year, $month ) = explode( '-', basename( __FILE__, '.php' ) );
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'date_query' => array(
array(
'year' => (int) $year,
'month' => (int) $month,
),
),
);
$posts = get_posts( $args );
foreach ($posts as $post) : setup_postdata( $post );
echo "<h2>"; the_title(); echo "</h2>";
echo "<span style='font-size:11px;'>By <a href='/company/employees/cameronandrews.php'>"; the_author(); echo "</a> | Published "; the_date(); echo "</span><br /><br>";
the_content();
endforeach;
?>
No problem. I’m not that familiar with integrating WordPress with an exisiting site.
http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
What happens when you do this (to test if any posts are shown)?
<?php
$args = array( 'posts_per_page' => 10 );
$posts = get_posts( $args );
?>
Never mind the basic php worked. but the code to pull the month and such didnt. Ideas as to why?
Can you try and print the values for $year and $month:
<?php
list( $year, $month ) = explode( '-', basename( __FILE__, '.php' ) );
echo $year;
echo $month;
?>
Did you put a dash in the filename november-2013.php.
prints this “november2013”
http://cameronandrews.com/blog/archives/november-2013.php
and yes i did put the dash as you can tell by the link above.
Ah, I see it should be:
list( $month, $year ) = explode( '-', basename( __FILE__, '.php' ) );
Is there a way i can make a “test” post for say a month from now or a month ago to make sure it doesnt pull that post. Because it seems to be working now but just in case i want to test. and i very much thank you for your help.
UH OH. i figured out how to publish it so it was in say september, but it still pulls the post.
http://cameronandrews.com/blog/archives/november-2013.php
You can create a test post and change the date for it just above the publish button.
Can you post all the code you have now.
Yeah here you go:
<?php
require('../blog/wp-blog-header.php');
require('../blog/wp-includes/general-template.php');
?>
<?php
list( $month, $year ) = explode( '-', basename( __FILE__, '.php' ) );
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'date_query' => array(
array(
'year' => (int) $year,
'month' => (int) $month,
),
),
);
$posts = get_posts( $args );
foreach ($posts as $post) : setup_postdata( $post );
echo "<h2>"; the_title(); echo "</h2>";
echo "<span style='font-size:11px;'>By <a href='/company/employees/cameronandrews.php'>"; the_author(); echo "</a> | Published "; the_date(); echo "</span><br /><br>";
the_content();
endforeach;
?>