Answers for "how to create post link in wordpress"

0

how to create post link in wordpress

add_filter( 'post_type_link', 'wpa_course_post_link', 10, 2 );

add_action( 'pre_get_posts', 'na_parse_request' );

you should first of all costomize in 'wpa_course_post_link' when they are 

then u should check if the client ask for page for recegnizing the page 
by 'na_parse_request'

// this is example
function __getAllParentsCat($post) {
    $category = get_the_category($post); 
    if(!isset($category[0])) return array();
    //firstly, load data for your child category
     $child = get_category($category[0]->term_id);
    
     //from your child category, grab parent ID
     $cats = array($category[0]->name);
     while($parent = $child->parent) {
         //load object for parent category
         $child = get_category($parent);
         var_dump($child->name);
         array_push($cats,$child->name);
     }
     $cats =  array_reverse($cats);
     return $cats;
}
function wpa_course_post_link( $post_link, $post = 0 ){
    if ( 'mq_product' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }
    $terms = wp_get_object_terms( $post->ID, 'course' );
 
    $cats = __getAllParentsCat($post);

    if ( is_object( $post ) ){
        $terms = implode("/",$cats);
        if( !$terms ){
            $terms = "uncategorized";  
        }
        $post_link = str_replace( '%category%' , $terms , $post_link );
        $len = strlen('%post_id%');
        $postPos = strpos($post_link,'%post_id%',0);
        $post_link_len = $postPos!==false? $postPos+$len:strlen($post_link);
        $post_link = substr($post_link,0,$post_link_len);
        $post_link = str_replace( '%post_id%' , $post->ID , $post_link );
    }
    return $post_link;  
}
add_filter( 'post_type_link', 'wpa_course_post_link', 10, 2 );
function na_parse_request( $query ) {
    $pp = get_post($query->query["p"]);
    if (!$pp|| 'mq_product' != $pp->post_type) {
        return;
    }
    $query->set( 'post_type', array( 'post', 'mq_product', 'page' ) );
  }
  
add_action( 'pre_get_posts', 'na_parse_request' );
Posted by: Guest on April-27-2021

Code answers related to "how to create post link in wordpress"

Browse Popular Code Answers by Language