ฉันจะได้รับ Permalink ของแท็กโพสต์หรือหมวดหมู่ที่เฉพาะเจาะจง แต่ถ้าฉันต้องการได้รับ Permalink ประเภทโพสต์ที่กำหนดเอง? ฉันไม่พบสิ่งใดใน Codex หรือที่อื่นเกี่ยวกับวิธีการทำเช่นนี้
ฉันจะได้รับ Permalink ของแท็กโพสต์หรือหมวดหมู่ที่เฉพาะเจาะจง แต่ถ้าฉันต้องการได้รับ Permalink ประเภทโพสต์ที่กำหนดเอง? ฉันไม่พบสิ่งใดใน Codex หรือที่อื่นเกี่ยวกับวิธีการทำเช่นนี้
คำตอบ:
วิธีการเกี่ยวกับhref="<?php echo get_post_type_archive_link( $post_type ); ?>"
ที่$post_type
เป็นประเภทโพสต์ของคุณหรือไม่
อ่านเพิ่มเติม: Codex
the_permalink()
ภายในวงที่คุณสามารถใช้ get_permalink( $id )
ด้านนอกของวงที่คุณสามารถใช้
ฉันรู้ว่าโพสต์นี้อาจเก่า แต่ในกรณีที่คนอื่นกำลังค้นหาฟังก์ชั่นที่ทำสิ่งนี้นี่คือสิ่งที่ฉันเขียน $ post_type ต้องถูกส่งเป็นตัวแปร :)
if( !function_exists( 'wp_get_post_type_link' ) ){
function wp_get_post_type_link( &$post_type ){
global $wp_rewrite;
if ( ! $post_type_obj = get_post_type_object( $post_type ) )
return false;
if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
$struct = $post_type_obj->rewrite['slug'] ;
if ( $post_type_obj->rewrite['with_front'] )
$struct = $wp_rewrite->front . $struct;
else
$struct = $wp_rewrite->root . $struct;
$link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
} else {
$link = home_url( '?post_type=' . $post_type );
}
return apply_filters( 'the_permalink', $link );
}
}
หวังว่ามันจะช่วย! :)