• Resolved spexy90

    (@spexy90)


    Hi,
    what is the best way to print the primary category in breadcrumbs with php? I found this get_primary_term..

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    You found the right thing 😉

    To expound on that, you can get the primary category like so:

    if ( function_exists( 'the_seo_framework' ) ) { 
    	$term = the_seo_framework()->get_primary_term();
    }
    
    // Default to WordPress's best effort when no valid term is returned.
    if ( empty( $term ) ) { 
    	$cats = get_the_category(); // this returns an array... nice function name :)
    	if ( $cats ) {
    		$cats = wp_list_sort(
    			$cats,
    			[ 'term_id' => 'ASC' ]
    		);
    		$term = $cats[0];
    	}
    }
    
    if ( ! empty( $term ) ) {
    	// Make breadcrumb item from $term here.
    }
    

    I hope this helps! Cheers 🙂

    Thread Starter spexy90

    (@spexy90)

    Thank you! But I’m learning php … so if I had to insert everything with the href tag it would be something like that?

    
    if ( function_exists( 'the_seo_framework' ) ) { 
    					$term = the_seo_framework()->get_primary_term();
    				}
    				
    				// Default to WordPress's best effort when no valid term is returned.
    				if ( empty( $term ) ) { 
    					$cats = get_the_category(); // this returns an array... nice function name :)
    					if ( $cats ) {
    						$cats = wp_list_sort(
    							$cats,
    							[ 'term_id' => 'ASC' ]
    						);
    						$term = $cats[0];
    					}
    				}
    				
    				if ( ! empty( $term ) || $portfolio_page_id) {
    					$breadcrumbs[] = '<a href="'. esc_url(?) .'">'. esc_url(?) .'</a>';
    				}
    

    How do i get url and name?
    Thank you very much, very kind

    Thread Starter spexy90

    (@spexy90)

    Yes, I solved it like this:

    
    if ( function_exists( 'the_seo_framework' ) ) {
    					$term = the_seo_framework()->get_primary_term(get_the_ID(), 'portfolio-types' );
    				}
    
    				if ( empty( $term ) ) {
    					$cats = get_the_category(); 
    					if ( $cats ) {
    							$cats = wp_list_sort(
    									$cats,
    									[ 'term_id' => 'ASC' ]
    							);
    							$term = $cats[0];
    					}
    				}
    
    				if ( ! empty( $term ) || $portfolio_page_id) {
    					$breadcrumbs[] = '<a href="'. esc_url(get_term_link($term)) .'">'. esc_html($term->name) .'</a>';
    				}
    
    

    Thanks for your help!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Print Primary Category via Php’ is closed to new replies.