0

I have some doubt about the wp_get_archives() function

In my website I have create the following page template that use the wp_get_archives() function to show the posts list:

<?php 

    /**
     * Template Name: Posts Archive
     *
     * A custom page template for displaying all posts.
     *
     * The "Template Name:" bit above allows this to be selectable
     * from a dropdown menu on the edit page screen.
     *
     * @package WordPress
     * @subpackage Twenty_Ten
     * @since Twenty Ten 1.0
     */

    get_header(); 

?>

<!-- Contenuti (griglia) -->
<div class="container">
    <!-- Lead presentazione -->
    <section id="presentazione">
        <div class="row">
            <div class="col-sm-12">
                <!--<h1 class="text-center"><small>Associazione per la Tutela dei Diritti Umani del Popolo Eritreo</small></h1>-->
                <h1 class="text-center title">Associazione per la Tutela dei Diritti Umani del Popolo Eritreo</h1>
                <h1 class="text-center leadTitle">Association in Defense of the Human Rights of the Eritrean People</h1>
                <!--
                <p class="lead text-center">
                    Association in Defense of the Human Rights of the Eritrean People
                </p>
                -->
            </div><!-- /.col-sm-12 -->
        </div><!-- /.row -->
    </section><!-- /section presentazione -->
    <!-- Progetti in evidenza -->

    <header class="header-sezione">
        <h2>Archivio post</h2>
    </header>

    <ul>
        <?php wp_get_archives('type=postbypost'); ?>
    </ul>

    </section>

</div>

<?php get_footer(); ?>

It work and this is the result (I only need to set the right CSS related to the font size)

Ok, now my question is: using the wp_get_archives() function can I show the post date before each posts?.

I want to obtain something like:

10/04/2014 - POST TITLE

rather than the mere post title.

Can I do it passing some parameters to the wp_get_archives() function? Or the only solution is do not use this function and create a custom loop into my template page?

0

wp_get_archives() can not display post date, but you can force wp_get_archives to display date by using hook.

Put the following function at the end of your custom template file or functions.php file -

function wpse_the_title($title, $id){
    if( $date = get_the_date('d/m/Y', $id) ){
        $title = sprintf('%s - %s', $date, $title);
    }
    return $title;
}

Replace with appropriate code -

<?php add_filter('the_title', 'wpse_the_title', 10, 2); ?>
<ul>
    <?php wp_get_archives('type=postbypost'); ?>
</ul>
<?php remove_filter('the_title', 'wpse_the_title', 10, 2); ?>
0

The wp_get_archives function in Wordpress does not support displaying dates. However my solution worked without using this function it's still probably handy for your or someone else his cause:

In the functions.php file of your theme place the following:

 function recentPostsDate() {
    $rPosts = new WP_Query();
    $rPosts->query('showposts=6');
    while ($rPosts->have_posts()) : $rPosts->the_post(); ?>
        <ul>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                    <?php the_date('d/m/Y', '<span class="date">', '</span>'); the_title(); ?>
                </a>
            </li>
        </ul>
    <?php endwhile;
    wp_reset_query();
}

After this you can use this in your main template simply by calling the function:

<?php recentPostsDate(); ?>

Based upon the settings I have chosen the output will look something like:

01/04/2015 First news title
23/03/2015 2nd news title
14/03/2015 Third news title
...

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.