| | 1810 | |
| | 1811 | /** |
| | 1812 | * Outputs breadcrumb navigation with user arguments for post type and other parameters |
| | 1813 | */ |
| | 1814 | |
| | 1815 | function wp_breadcrumbs( $args = '' ) { |
| | 1816 | $defaults = array( |
| | 1817 | 'post_type' => 'post', |
| | 1818 | 'home_label' => __( 'Home' ), |
| | 1819 | 'item_sep' => ' › ', |
| | 1820 | 'list_sep' => ' | ', |
| | 1821 | 'show_title' => true, |
| | 1822 | 'start_wrap' => '', |
| | 1823 | 'end_wrap' => '', |
| | 1824 | 'echo' => true, |
| | 1825 | ); |
| | 1826 | |
| | 1827 | $params = wp_parse_args ( $args, $defaults ); |
| | 1828 | |
| | 1829 | $args = apply_filters( 'wp_breadcrumb_args', $params ); |
| | 1830 | |
| | 1831 | // get args and values |
| | 1832 | global $post; |
| | 1833 | $home_url = home_url( '/' ); |
| | 1834 | |
| | 1835 | if ( $args['show_title'] ) { |
| | 1836 | $post_title = get_the_title(); |
| | 1837 | } else { |
| | 1838 | $post_title = null; |
| | 1839 | } |
| | 1840 | |
| | 1841 | $cat_list = get_the_category_list(); |
| | 1842 | if ( ! empty ( $cat_list) ) { |
| | 1843 | $post_category = get_the_category_list( $args['list_sep'] ) . $args['item_sep']; |
| | 1844 | } else { |
| | 1845 | $post_category = null; |
| | 1846 | } |
| | 1847 | |
| | 1848 | if ( $args['show_title'] == true ) { |
| | 1849 | $post_title = '<span itemprop="title">' . get_the_title() . '</span>'; |
| | 1850 | } else { |
| | 1851 | $post_title = null; |
| | 1852 | } |
| | 1853 | |
| | 1854 | if ( $args['echo'] ) { |
| | 1855 | $print = true; |
| | 1856 | } else { |
| | 1857 | $print = false; |
| | 1858 | } |
| | 1859 | |
| | 1860 | // check for page ancestors |
| | 1861 | |
| | 1862 | $par = get_post_ancestors( get_the_id() ); |
| | 1863 | |
| | 1864 | $pages = get_pages ( array ( 'include' => $par ) ); |
| | 1865 | |
| | 1866 | foreach ( $pages as $page ) { |
| | 1867 | $parent_list[$page->post_title][] = get_permalink ( $page->ID ); |
| | 1868 | } |
| | 1869 | |
| | 1870 | $parent_items = array(); |
| | 1871 | foreach ( $parent_list as $key => $value ) { |
| | 1872 | $parent_items[] .= '<a href="' . $value[0] . '">' . $key . '</a>'; |
| | 1873 | } |
| | 1874 | |
| | 1875 | if ( !empty ( $par ) ) { |
| | 1876 | $processed_parents = implode( $args['item_sep'], $parent_items ) . $args['item_sep']; |
| | 1877 | } else { |
| | 1878 | $processed_parents = null; |
| | 1879 | } |
| | 1880 | |
| | 1881 | // output for posts |
| | 1882 | if ( 'post' == $args['post_type'] ) { |
| | 1883 | $output = $args['start_wrap'] . '<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href=" ' . $home_url . '" itemprop="url">' . $args['home_label'] . '</a> ' . $args['item_sep'] . $post_title . '</div>' . $args['end_wrap']; |
| | 1884 | if ( $print ) { |
| | 1885 | echo $output; |
| | 1886 | } else { |
| | 1887 | return $output; |
| | 1888 | } |
| | 1889 | } |
| | 1890 | |
| | 1891 | // output for pages |
| | 1892 | elseif ( 'page' == $args['post_type'] ) { |
| | 1893 | if ( $post->post_parent ) { |
| | 1894 | $parent_title = get_the_title ($post->post_parent); |
| | 1895 | $parent_link = get_the_permalink ($post->post_parent); |
| | 1896 | $parent_output = '<a href="' . $parent_link . '">' . $parent_title . '</a>' . $args['item_sep']; |
| | 1897 | } else { |
| | 1898 | $parent_output = null; |
| | 1899 | } |
| | 1900 | |
| | 1901 | $output = $args['start_wrap'] . '<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"> |
| | 1902 | <a href=" ' . $home_url . '" itemprop="url">' . $args['home_label'] . '</a>' . $args['item_sep'] . $processed_parents . $post_title . '</div>' . $args['end_wrap']; |
| | 1903 | if ( $print ) { |
| | 1904 | echo $output; |
| | 1905 | } else { |
| | 1906 | return $output; |
| | 1907 | } |
| | 1908 | } else { |
| | 1909 | |
| | 1910 | // output for passed custom post types |
| | 1911 | if ( post_type_exists ( $args['post_type'] ) ) { |
| | 1912 | |
| | 1913 | if ( get_post_type_archive_link( $args['post_type'] ) ) { |
| | 1914 | $post_type_name = ucwords ( $args['post_type'] ); |
| | 1915 | $archive_url = get_post_type_archive_link( $args['post_type'] ); |
| | 1916 | $post_type_archive_link = '<a href="' . $archive_url . '">' . $post_type_name . '</a>' . $args['item_sep']; |
| | 1917 | } else { |
| | 1918 | $post_type_archive_link = null; |
| | 1919 | } |
| | 1920 | |
| | 1921 | $output = $args['start_wrap'] . '<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"> |
| | 1922 | <a href=" ' . $home_url . '" itemprop="url">' . $args['home_label'] . '</a>' . $args['item_sep'] . $post_type_archive_link . $post_title . '</div>' . $args['end_wrap']; |
| | 1923 | if ( $print ) { |
| | 1924 | echo $output; |
| | 1925 | } else { |
| | 1926 | return $output; |
| | 1927 | } |
| | 1928 | |
| | 1929 | } else { |
| | 1930 | // nothing if post type is invalid |
| | 1931 | return; |
| | 1932 | } |
| | 1933 | } |
| | 1934 | |
| | 1935 | } |