DextrousDave
DextrousDave

Reputation: 6793

Customizing a Wordpress-generated breadcrumb link

Good Day

I am using Wordpress, and I am using a breadcrumbs plugin (Breadcrumb NavXT). Now it works great, exept that I would like to customize the way it links to a specific link:

Now here is the scenario:

I have a page, and on the page are links to posts. So when you click on one of the links on the page, it takes you to the post. Now this posts belongs to a category named Drill Rigs, and Drill Rigs is a child category of Products.

Now on the post page, which has category 'products>drill rigs' (parent/child), the breadcrumb shows the following way:

Home>products>drill rigs>postname

Problem is, that of you click on the products link above, it takes you to the category page of products:

siteurl/category/products

and not the actual wordpress page that is called products (that contains the links linking to the posts mentioned above:

siteurl/absolute link to the page, which is a unique link eg. siteurl/803-2

Now as far as I know, you cannot change the path from a category (which are unique to posts) to a page, neither in wordpress nor the plugin.

The best way I can think of is to add custom jquery or php to the posts php page (or index/header pages) that looks for the container div containing the anchor tags that hosts the title 'products'...and then replacing that anchor tag with my custom unique page url...

How would I do this...?

Upvotes: 0

Views: 10488

Answers (4)

Otis Feru
Otis Feru

Reputation: 151

This code works with asynchron content for your posts: (to copy in functions.php)

function ariane() {
    $cat_id         = get_the_category()[0]->term_id;
    $breadcrumb     = '<li>' . get_the_title() . '</li>';

    $if_parent = TRUE;

    while($if_parent == TRUE) :
        $cat_object     = get_category($cat_id);
        $cat            = $cat_object->term_id;
        $categoryURL    = get_category_link($cat);
        $name           = $cat_object->name;
        $cat_id         = $cat_object->parent;              

        $add_link       = '<li><a href="' . $categoryURL . '">' . $name . '</a></li>';

        $breadcrumb     = substr_replace($breadcrumb, $add_link, 0, 0);

        if($cat_id  == 0) :
            $if_parent = FALSE;
        endif;
    endwhile;

    echo '<ul>' . $breadcrumb . '</ul>';    
}

In your archive.php or in the loop WP_QUERY

<div class="ariane"><?php ariane(); ?></div>

in css :

.ariane ul{
    display: flex;
    justify-content: flex-start;
    align-items: center;
}  
.ariane li:not(:last-child):after {
    content: '>';
    margin: 0 5px;
}

Upvotes: 1

DextrousDave
DextrousDave

Reputation: 6793

<script type="text/javascript">
$('#breadcrumbs .category[href="http://mysite.com/category/products"]').attr('href','http://mysite.com/803-2');
</script>

where:

http://mysite.com/803-2

is the url of the p[age I want to link to...

Upvotes: 0

Vikas Gautam
Vikas Gautam

Reputation: 978

function the_breadcrumb() {

$sep = '/';

if (!is_front_page()) {

    echo '<div class="breadcrumbs">';
    echo '<a href="';
    echo get_option('home');
    echo '">';
    bloginfo('name');
    echo '</a>' . $sep;

    if (is_category() || is_single() ){
        the_category('title_li=');
    } elseif (is_archive() || is_single()){
        if ( is_day() ) {
            printf( __( '%s', 'text_domain' ), get_the_date() );
        } elseif ( is_month() ) {
            printf( __( '%s', 'text_domain' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
        } elseif ( is_year() ) {
            printf( __( '%s', 'text_domain' ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
        } else {
            _e( 'Blog Archives', 'text_domain' );
        }
    }

    if (is_single()) {
        echo $sep;
        the_title();
    }

    if (is_page()) {
        echo the_title();
    }

    if (is_home()){
        global $post;
        $page_for_posts_id = get_option('page_for_posts');
        if ( $page_for_posts_id ) { 
            $post = get_page($page_for_posts_id);
            setup_postdata($post);
            the_title();
            rewind_posts();
        }
    }

    echo '</div>';
}

}

try this hope this may help you

Upvotes: 1

Vikas Gautam
Vikas Gautam

Reputation: 978

you can use the function to set your Breadcrumb by coding :

function the_breadcrumb() {
if (!is_home()) {
    echo '<a href="';
    echo get_option('home');
    echo '">';
    echo "Home";//bloginfo('name');
    echo "</a> / ";
    if (is_category() || is_single()) {
        the_category('title_li=');
        if (is_single()) {
            echo " / ";
            the_title();
        }
    } elseif (is_page()) {
        echo the_title();
    }
}

}

Put the above code in function.php file

you have to just call the function where you want to show the bredcrumb

<?php the_breadcrumb(); ?>

Hope this will help you ...

Upvotes: 0

Related Questions