• I am now trying to create a custom breadcrumbs on my WordPress site, but I am now struggling of calling a permalink of custom post type archive, that because calling a custom post archive permalink requires a post type string like here get_post_type_archive_link( string $post_type ), but this is impossible to do on breadcrumbs because it should be dynamic for each custom post type. Someone suggest me to try using get_queried_object_id() but I don’t know how to combine it with get_post_type_archive_link( string $post_type ) to call the link. here is my breadcrumb codes:

    function my_breadcrumb() {
        $postType = get_post_type_object(get_post_type());
        echo '<a href="'.home_url().'" rel="nofollow">Home</a>';
        if ((get_post_type() || is_single()) && !is_search()) {
            echo "  ?  ";
            echo '<span style="text-transform: capitalize;">';
            echo esc_html($postType->labels->singular_name); 
            echo '</span>';
                if (is_single()) {
                    echo "   ?   ";
                    the_title();
                }
        }
    };
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The queried object ID isn’t helpful for post archive requests because there is no single object involved. Try get_query_var('post_type'). You’ll get a similar value for both post archive and single post requests. You may need to also involve is_archive() if it’s not appropriate for single post requests.

    If the post type query var is empty, it’s not a post request of any sort, it’s some other kind of query like category, etc. Note that the query var value could be a string or an array of post types. If it’s an array, you could arbitrarily use the first element value. It’s possible on your site this will never be an issue, but your code ought to still check for the var type to avoid causing any warnings or errors, just in case.

Viewing 1 replies (of 1 total)
  • The topic ‘Calling post type archive permalink on breadcrumbs’ is closed to new replies.