WordPress.org

Ready to get started?Download WordPress

Forums

Permalink hack (3 posts)

  1. SGr33n
    Member
    Posted 6 years ago #

    I've to do a new get_archives() function like in order to filter the author's posts.

    Everything went ok selecting the posts, but not getting the right link using permalinks.

    The problem, in my opinion is here:

    function sgr_get_month_link($year, $month, $author_id, $author_nicename) {
    global $wp_rewrite;
    if ( !$year )
    $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
    if ( !$month )
    $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
    $monthlink = $wp_rewrite->get_month_permastruct();
    if ( !empty($monthlink) ) {
    $monthlink = str_replace('%author%', $author_nicename, $monthlink);
    $monthlink = str_replace('%year%', $year, $monthlink);
    $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
    return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
    } else {
    return apply_filters('month_link', get_option('home') . '/?author=' . $author_id . '&m=' . $year . zeroise($month, 2), $year, $month);
    }
    }

    As you can see I added the author_id variable to the non-permalink option, but I really cannot understand how to modify the permalink option in order to have the author too, because the get_month_permastruct is related just to month.

    Can somebody help me?

  2. SGr33n
    Member
    Posted 6 years ago #

    Perhaps solved. Now it's ok, but it puts two slashes after the author name:

    function sgr_get_month_link($year, $month, $author_id, $author_nicename) {
    global $wp_rewrite;
    if ( !$year )
    $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
    if ( !$month )
    $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
    $monthlink = $wp_rewrite->get_month_permastruct();
    if ( !empty($monthlink) ) {
    $monthlink = str_replace('%year%', $year, $monthlink);
    $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
    $author_home = get_author_link(false, $author_id, $author_nicename);
    return apply_filters('month_link', $author_home . user_trailingslashit($monthlink, 'month'), $year, $month);
    } else {
    return apply_filters('month_link', get_option('home') . '/?author=' . $author_id . '&m=' . $year . zeroise($month, 2), $year, $month);
    }
    }

  3. SGr33n
    Member
    Posted 6 years ago #

    Solved with substr. Do you think is ok ?

    function sgr_get_month_link($year, $month, $author_id, $author_nicename) {
    global $wp_rewrite;

    if ( !$year )
    $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
    if ( !$month )
    $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
    $monthlink = $wp_rewrite->get_month_permastruct();
    if ( !empty($monthlink) ) {
    $monthlink = str_replace('%year%', $year, $monthlink);
    $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
    $author_home = get_author_link(false, $author_id, $author_nicename);
    $author_home = substr($author_home, 0, -1);
    return apply_filters('month_link', $author_home . user_trailingslashit($monthlink, 'month'), $author, $year, $month);
    } else {
    return apply_filters('month_link', get_option('home') . '/?author=' . $author_id . '&m=' . $year . zeroise($month, 2), $author, $year, $month);
    }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic