How Do I Make Dynamic Breadcrumbs in WordPress?
Dynamic breadcrumbs in WordPress are a great way to keep your breadcrumbs organized and easy to follow. You can create them using a few simple steps.
First, create a new file called breadcrumbs.php in your WordPress theme. Inside this file, add the following code:
/** * Dynamic breadcrumbs */ function dynamic_breadcrumbs() { global $post; $breadcrumbs = array(); $breadcrumbs[] = array( ‘name’ => esc_html__( ‘Pages: ‘, ‘wp_title’ ), ‘link’ => esc_url( get_permalink() ), ); // Add post meta data if needed if( is_post_type_archive() && ( get_post_type() -> post_type == ‘page’ )) { $breadcrumbs[] = array( ‘link’ => esc_url( get_permalink() ), ‘title’ => __( ‘Pages: %s’, ‘wp_title’ ), ‘archive_name’ => esc_attr( get_the_term_list( ‘posts_per_page’ ) ), ); } return $breadcrumbs; } // Add dynamic breadcrumbs function dynamic_breadcrumbs_add() { global $post; $breadcrumbs = dynamic_breadcrumbs(); $breadcrumbs[] = array( ‘name’ => esc_html__( ‘Pages: ‘, ‘wp_title’ ), ‘link’ => esc_url( get_permalink() ), ); // Add post meta data if needed if( is_post_type_archive() && ( get_post_type() -> post_type == ‘page’ )) { $breadcrumbs[] = array( ‘link’ => esc_url( get_permalink() ), ‘title’ => __( ‘Pages: %s’, ‘wp_title’ ), ‘archive_name’ => esc_attr( get_the_term_list( ‘posts_per_page’ ) ), ); } return $breadcrumbs; } add_action( ‘wp_head’, ‘dynamic_breadcrumbs_add’ ); } /** * Add dynamic breadcrumbs */ function dynamic_breadcrumbs_add() { global $post; $breadcrumbs = dynamic_breadcrumbs(); $breadcrumbs[] = array( ‘name’ => esc_html__( ‘Pages: ‘, ‘wp_title’ ), ‘link’ => esc_url( get_permalink() ), ); // Add post meta data if needed if( is_post_type_archive() && ( get_post_type() -> post_type == ‘page’ )) { $breadcrumbs[] = array( ‘link’ => esc_url( get_permalink() ), ‘title’ => __( ‘Pages: %s’, ‘wp_title’ ), ‘archive_name’ => esc_attr( get_the_term_list( ‘posts_per_page’ ) ), ); } return $breadcrumbs; }
Next, add the following line to your site’s header:
Finally, add the following line to your theme’s functions.php file:
add_action( ‘init’, ‘dynamic_breadcrumbs_add’ );
Your dynamic breadcrumbs are now ready to use. To add them to your posts, simply add the following line to the post’s content: