Ask your WordPress questions! Pay money and get answers fast! (more info)

Link custom archive page by month WordPress

  • SOLVED

Custom Post Archive Links to Go to Custom Archive Page by Month and year

so far and have the months of the custom post types listed underneath the relevant year.



<li><a href="<?php echo get_post_type_archive_link ('sermons'); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
</li>



months are linked to the custom archive page, but the page lists all the posts - not the posts for the monthly link that is selected.

I need the layout attached, pretty please. Stuck here!

Answers (3)

2013-11-25

Arnav Joy answers:

take help from this code

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<ul>
<?php
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'sermons' ORDER BY post_date ASC");

foreach($years as $year) : ?>

<h4><li><a href="<?php echo get_year_link($year ); ?> "><?php echo $year; ?></a>
<ul>
<?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_type = 'sermons' AND post_status = 'publish' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");

foreach($months as $month) : ?>

<h4>
<li>
<a href="<?php echo get_month_link($year, $month); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
<ul>
<?php $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'sermons' AND post_status = 'publish' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");

foreach ($theids as $theid): ?>

<h4 style= font-style:italic;><li><a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>"><?php echo $theid->post_title; ?></a></li></h4>

<?php endforeach; ?>

</ul>
</li></h4>

<?php endforeach;?>

</ul>
</li></h4>

<?php endforeach; ?>

</ul>
</div>
</div>


Niconline comments:

Thank you, but the months still link to the regular posts page.

What I really need is for the months to link to the archive-sermons.php page by month and year like this

www.freelancerunlimited.co.za/totestt/sermons/2013/10

currently, the link just goes to

www.freelancerunlimited.co.za/totestt/sermons


Arnav Joy comments:

can you provide us full code how you have shown year and month name in following page

http://www.freelancerunlimited.co.za/totestt/sermon-archives/


Niconline comments:

Hi Arnav,

Please find page attached.

With thanks.


Niconline comments:


<?php
/*
Template Name: Sermon Archives
*
* @package WordPress
* @subpackage trinity
*/

get_header(); ?>

<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>
<div id="secondary-left" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-2' ); ?>
</div><!-- .extra-sidebar .widget-area -->
<?php endif; // end extra sidebar widget area ?>

<div id="primary" class="site-content">

<div class="archive-header">
<h1 class="archive-title">SERMONS</h1>

</div><!-- .archive-header -->

<div id="centcontm">

<div class="contmargin">

<ul>
<?php
/**/
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='sermons' ORDER BY post_date DESC");
foreach($years as $year) :
?>
<li> <?php echo $year; ?>

<ul>
<? $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='sermons' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");
foreach($months as $month) :
?>
<li><a href="<?php echo get_post_type_archive_link ('sermons'); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
</li>
<?php endforeach;?>
</ul>
</li>
<?php endforeach; ?>
</ul>

<?php trinity_pagination(); ?>

</div><!-- .contmargin -->

</div><!-- #centcontm -->

<?php if ( is_active_sidebar( 'sidebar-3' ) ) : ?>
<div id="secondary-right" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-3' ); ?>
</div><!-- .extra-sidebar .widget-area -->
<?php endif; // end extra sidebar widget area ?>


<?php get_footer(); ?>


Arnav Joy comments:

try this

<?php

/*

Template Name: Sermon Archives

*

* @package WordPress

* @subpackage trinity

*/



get_header(); ?>



<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>

<div id="secondary-left" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-2' ); ?>

</div><!-- .extra-sidebar .widget-area -->

<?php endif; // end extra sidebar widget area ?>



<div id="primary" class="site-content">



<div class="archive-header">

<h1 class="archive-title">SERMONS</h1>



</div><!-- .archive-header -->



<div id="centcontm">



<div class="contmargin">



<ul>

<?php

/**/

$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='sermons' ORDER BY post_date DESC");

foreach($years as $year) :

?>

<li> <?php echo $year; ?>



<ul>

<? $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='sermons' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");

foreach($months as $month) :

?>

<li><a href="<?php echo bloginfo('url'); ?>/sermons/<?php echo $year; ?>/<?php echo $month;?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>

</li>

<?php endforeach;?>

</ul>

</li>

<?php endforeach; ?>

</ul>



<?php trinity_pagination(); ?>



</div><!-- .contmargin -->



</div><!-- #centcontm -->



<?php if ( is_active_sidebar( 'sidebar-3' ) ) : ?>

<div id="secondary-right" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-3' ); ?>

</div><!-- .extra-sidebar .widget-area -->

<?php endif; // end extra sidebar widget area ?>





<?php get_footer(); ?>


Niconline comments:

Spot on :)))

Thank you so much!


Niconline comments:

How do I pay you? This is the first time I have used this site...


Arnav Joy comments:

check this tutorial to assign me prize

http://blog.tailormadeanswers.com/2011/04/22/voting-assign-prize-money/

2013-11-25

Navjot Singh answers:

If your archive file uses wp_get_archives() function then you will need to add support for custom post types explicitly to it. By default the function doesn't support custom post types. [[LINK href="http://bajada.net/2010/07/15/adding-custom-post-types-to-wp_get_archives"]]Here is[[/LINK]] how you can do it.


Niconline comments:

Hi,

I believe I have done this already (I think!)

But the months still link to the regular posts page.

What I really need is for the months to link to the archive-sermons.php page by month and year like this

www.freelancerunlimited.co.za/totestt/sermons/2013/10

currently, the link just goes to

www.freelancerunlimited.co.za/totestt/sermons


Navjot Singh comments:

[[LINK href="http://stackoverflow.com/questions/14486792/custom-post-type-yearly-monthly-archive"]]This solution[[/LINK]] will work for you. Enter both parts of the code in your theme's function.php.

Now in your above archive file you can replace the month listing code with the array you get from the solution. That should work.

2013-11-25

Just Me answers:

Add month to the href and check/use this in archive page.

href="<?php echo get_post_type_archive_link ('sermons'); ?>/?month=<?php echo date( 'F', mktime(0, 0, 0, $month) );?>">




Niconline comments:

Hi,

This just made the month listed disappear.

Please view source (have reverted file to original)

http://www.freelancerunlimited.co.za/totestt/sermon-archives/

Thank You!


Niconline comments:

The months link to the regular posts page.

What I really need is for the months to link to the archive-sermons.php page by month and year like this

www.freelancerunlimited.co.za/totestt/sermons/2013/10

currently, the link just goes to

www.freelancerunlimited.co.za/totestt/sermons