WordPress开发笔记

WordPress获取分类页面与posttype归档页面指定分页地址的封装函数

各种分类法分类页面及分页地址的获取函数:

function zzw_get_taxterm_fenye_pagelink($term_id, $taxonomy, $paged)
{
    //$term_id:$taxonomy分类法中,一个分类的term_id
    //$taxonomy:term_id分类所在的分类法名称
    //$paged:页码数

    global $wp_query,$wp_rewrite;

    $archive_url = get_term_link( $term_id, $taxonomy );;//这是获取$taxonomy分类法中,ID为$term_id的分类页面基础链接地址

    if ( !is_wp_error( $archive_url ) ) {
        // 添加分页参数
        if ($paged > 1) {
            if ($wp_rewrite->using_permalinks()) { //判断是否适用永久链接
                $archive_url = trailingslashit($archive_url) . 
                             $wp_rewrite->pagination_base . '/' . 
                             user_trailingslashit($paged);
                             //trailingslashit函数:附加尾部斜杠
                             //user_trailingslashit函数:如果永久链接结构有尾随斜杠,则有条件地添加尾随斜杠,如果没有,则剥离尾随斜杠。字符串通过“user_trailingslashit”过滤器。如果站点未设置为具有尾随斜杠,则将从字符串中删除尾随斜杠。
            } else {
                $archive_url = add_query_arg('paged', $paged, $archive_url);
            }
        }

        echo $archive_url;
    }
            
}

获取各类posttype页面及分页地址的获取函数:

function zzw_get_archive_fenye_pagelink($post_type,$paged)
{
    //$post_type:post_type的name
    //$paged:页码数

    global $wp_query,$wp_rewrite;

    $archive_url = get_post_type_archive_link($term_pt);//这是获取posttype归档页面基础链接地址

    if ( $archive_url ) {
        // 添加分页参数
        if ($paged > 1) {
            if ($wp_rewrite->using_permalinks()) { //判断是否适用永久链接
                $archive_url = trailingslashit($archive_url) . 
                             $wp_rewrite->pagination_base . '/' . 
                             user_trailingslashit($paged);
                             //trailingslashit函数:附加尾部斜杠
                             //user_trailingslashit函数:如果永久链接结构有尾随斜杠,则有条件地添加尾随斜杠,如果没有,则剥离尾随斜杠。字符串通过“user_trailingslashit”过滤器。如果站点未设置为具有尾随斜杠,则将从字符串中删除尾随斜杠。
            } else {
                $archive_url = add_query_arg('paged', $paged, $archive_url);
            }
        }

        echo $archive_url;
    }
            
}