WordPress.org

Support

Support » Plugins and Hacks » WordPress Calendar For Custom Post Types » [Resolved] [Plugin: WordPress Calendar For Custom Post Types] Showing posts from calendar

[Resolved] [Plugin: WordPress Calendar For Custom Post Types] Showing posts from calendar

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Stas Su?cov

    @sushkov

    Yes, it’s a limitation caused by WordPress lack of dynamic archive pages.
    You can use something like this to link to latest article post instead of broken archive page.

    /**
    * Find an event and replace the calendar day link
    */
    function cptc_calendar_day_link( $url, $year, $month, $day ) {
    # Filter post types here
    #if ( !is_post_type_archive( 'event' ) )
    # return $url;
    
    $day_event = get_posts( array(
        'numberposts' => 1,
        'post_type' => 'event',
        'day' => $day,
        'monthnum' => $month,
        'year' => $year
    ) );
    
    $day_event_url = get_post_permalink( reset( $day_event ) );
    if ( $day_event_url )
        return $day_event_url;
    return $url;
    }
    add_filter( 'day_link', 'cptc_calendar_day_link', 10, 4 );
    
    /**
    * Find an event and replace the calendar month link
    */
    function cptc_calendar_month_link( $url, $year, $month ) {
    return add_query_arg( 'm', $year . zeroise( $month, 2 ), get_post_type_archive_link( 'event' ) );
    }
    add_filter( 'month_link', 'cptc_calendar_month_link', 10, 3 );

    Good luck.

    Yes, it works! Thanks!

    Ops… it works with the days, but it don´t with the month links!!!

    Could you take a look on that??
    Thanks!

    Sorry, it was my fault. I didn´t change the custom post type name in your script before. Everything is working fine now!

    Thanks!!!

    Hi Stas,

    I still have a problem with that… in case I have more than one event at the same day, the link should appoint to a day archive, right?

    Is that a way to do that?
    Thanks!

    I solve it this way:

    1) Install the Custom Post Type Archives plugin, that creates date post type archives.

    2) Change your filter, to count posts and change day links when it has more than one event/post:

    /**
    * Find an event and replace the calendar day link
    */
    function cptc_calendar_day_link( $url, $year, $month, $day ) {
    # Filter post types here
    #if ( !is_post_type_archive( 'events' ) )
    # return $url;
    
    $day_event = get_posts( array(
        'post_type' => 'events',
        'day' => $day,
        'monthnum' => $month,
        'year' => $year
    ) );
    
    $myposts = count($day_event);
    
    if ($myposts == 1) {
    	$day_event_url = get_post_permalink( reset( $day_event ) );
    } else {
    	$day_event_url = site_url() . "/events/" . $year . "/" . $month . "/" . $day;
    }
    
    if ( $day_event_url )
        return $day_event_url;
    return $url;
    }
    add_filter( 'day_link', 'cptc_calendar_day_link', 10, 4 );
    
    /**
    * Find an event and replace the calendar month link
    */
    function cptc_calendar_month_link( $url, $year, $month ) {
    return add_query_arg( 'm', $year . zeroise( $month, 2 ), get_post_type_archive_link( 'events' ) );
    }
    add_filter( 'month_link', 'cptc_calendar_month_link', 10, 3 );

    That is it. Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] [Plugin: WordPress Calendar For Custom Post Types] Showing posts from calendar’ is closed to new replies.
Skip to toolbar