0

Im having a problem with the custom date archive on how to display it to date.php

Here's my code for the custom archive

 <?php
    $o_query_result = new WP_Query( array( 'posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => $post_type ) );
    $a_archive_post_count = array();
    $a_archive_post_url = array();
    if( isset($o_query_result->posts) && !empty($o_query_result->posts) )
    {
        foreach( $o_query_result->posts AS $o_post_details )
        {
            $datetime = new DateTime($o_post_details->post_date);
            $s_array_index = $datetime->format('Y') ."年". $datetime->format('m') ."月";
            if( array_key_exists($s_array_index, $a_archive_post_count) ) 
            {
                $a_archive_post_count[$s_array_index] = $a_archive_post_count[$s_array_index] + 1;
            }
            else
            {
                $a_archive_post_count[$s_array_index] = 1;  
            }
            $a_archive_post_url[$s_array_index] = '/' . $datetime->format('Y') .'/'. $datetime->format('m');
        }
        wp_reset_postdata();
    }
?>

<ul class="introduction__blog__list">
    <?php foreach ($a_archive_post_count as $s_archive_post_count_key => $s_archive_post_count_value): ?>
    <li class="introduction__blog__list__item--sb mt10p">
        <a class="" href="<?php echo home_url(); ?>/<?php echo $post_type; ?><?php echo $a_archive_post_url[$s_archive_post_count_key] ?>">
            <p class="introduction__blog__list__summary__date">
                <?php echo $s_archive_post_count_key . ' (' .$s_archive_post_count_value. ')' ?>
            </p>
        </a>
    </li>
    <?php endforeach; ?>
</ul>

it will give me a result of: 2016年11月 (6) -> 6 means there are 6 posts in a custom blog

if the user click the date above which has the link (for example) http://portfolio.com/custom_blog/2016/11 , it gives me an error like page not found.

Is there a way to display the result in the date.php or should i need to create a page for each custom post like custom_blog/2016/11?

I have tried http://portfolio.com/2016 -> it shows the code that i put in date.php but i dont want to do that.

Please advise me on how can i have the result or output in date.php or other page/file.

0

You will need to write a custom rewrite rule for this.

Just copy the following code into your functions.php and flush rewrite rules.

 
function xlinkerz_custom_archive_rewrite_rule( $rewrite_rules ) {

  $custom_slug = 'custom_archive';

  $year_rule = array( $custom_slug . '/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?year=$matches[1]&monthnum=$matches[2]' );

  $month_rule = array( $custom_slug . '/([0-9]{4})/?$' => 'index.php?year=$matches[1]' );

 // Merging rules
 $rewrite_rules = $year_archive + $month_archive + $rewrite_rules;

 return $rewrite_rules;

}

add_filter('rewrite_rules_array', 'xlinkerz_custom_archive_rewrite_rule');

*Flushing rewrite rules can be done in several ways, the easiest way is to go to the admin area and just save the permalinks. Remember you should not be calling flush_rewrite_rules(); within a function which defines new rewrite rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.