Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

History trail breadcrumb - Echo Page the user came from WordPress

  • SOLVED

I'd like to include in existing breadcrumbs, a link to point to the page user came from. Is this possible and any solutions for WordPress?

What I have is:
<a href="#">Blog</a> | Current page

What I try to achieve is:
<a href="#">Link to the page/category/post user came from within the website [name of the page linked]</a> <a href="#">Blog</a> | Current page

I need to add this to the beginning of the existing Breadcrumb (will not interact though.)

Answers (3)

2010-11-24

Andrzej answers:

I'd suggest cookies approach, more-less like this:

<?php

if ( $_COOKIE['bread_type'] && $_COOKIE['bread_id'] ) :

$id = $_COOKIE['bread_id'];

switch($_COOKIE['bread_type'] ) {
case 'single':
$link_code = '<a href="'.get_permalink($id).'">'.get_the_title($id).'</a>';
break;
case 'page':
$link_code = '<a href="'.get_permalink($id).'">'.get_the_title($id).'</a>';
break;
case 'single':
$link_code = '<a href="'.category_link($id).'">'.category_title($id).'</a>';
break;
}
endif;

echo $link_code;


global $post, $cat;

if (is_single()) :
$type = 'single';
$id = $post->ID;
else if (is_category()) :
$type = 'cat';
$id = $cat;
else if (is_page()) :
$type = 'page';
$id = $post->ID;
else :
$type = false;
$id = false;

endif;

setcookie('bread_type', $type);
setcookie('bread_id', $id);

?>


Lucian Florian comments:

getting this error:
Parse error: syntax error, unexpected T_IF, expecting ':'


Andrzej comments:

do you also want the name of the page you came from or only "back" anchor text?


Lucian Florian comments:

Yes, name of the page too.


Andrzej comments:

is it possible (at your website) that user will come from a category posts listing rather than single page?


Andrzej comments:

try this
$referer_id = url_to_postid(wp_get_referer());
if ( !$referer_id && get_option('page_on_front') ) {
if ( wp_get_referer() == get_bloginfo('wpurl').'/') {
$referer_id = get_option('page_on_front');
}
}
if ( $referer_id ) {
?>
<a href="<?php echo wp_get_referer(); ?>"><?php echo
get_the_title($referer_id); ?></a> | ...
<?php
}


Lucian Florian comments:

This one works too. I decided to increase the prize and divide money. Thank you

2010-11-24

Pau answers:

if you mean to say the page user came from then Michael Fields is right though i haven't use the <strong>wp_get_referer()</strong> it might be equal to:

<?php echo $_SERVER['HTTP_REFERER']; ?>


Pau comments:

try:

<a href="<?php wp_get_referer() ?>"><?php wp_get_referer() ?></a> <a href="#">Blog</a> | Current page

or:

<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>"><?php echo $_SERVER['HTTP_REFERER']; ?></a> <a href="#">Blog</a> | Current page


Lucian Florian comments:

It returns the link to the page but as a name return the full url instead of the name of the page. First method doesn't work.


Lucian Florian comments:

If we figure out how to echo the page name dynamically, I think we reached the solution.


Pau comments:

Basename will do the trick

<a href="<?php echo $_SERVER
['HTTP_REFERER']; ?>"><?php echo
basename($_SERVER['HTTP_REFERER']); ?></a> <a
href="#">Blog</a> | Current page


Lucian Florian comments:

Almost good.
Any chance we can return the_title instead of the link url? I am concerned when there are more words, as they will have a dash as delimiter.


Pau comments:

then try this:

<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>"><?php echo get_the_title(url_to_postid($_SERVER['HTTP_REFERER'])); ?></a> <a
href="#">Blog</a> | Current page


Lucian Florian comments:

You're a genius :) Thank you. Let me run few more tests before I select you as winner.


Pau comments:

glad it work! looking forward to selecting me as the winner. thanks!

2010-11-24

Michael Fields answers:

[[LINK href="http://codex.wordpress.org/Function_Reference/wp_get_referer"]]wp_get_referer()[[/LINK]] would be a good place to start.


Lucian Florian comments:

any time estimate for this method?


Michael Fields comments:

Please be more specific. List exactly what you are trying to achieve. I'm not sure what "link where user cam from" really refers to... How does this link relate to the breadcrumb list that you already have? What breadcrumb script are you using?


Lucian Florian comments:

I added more details in the question. Let me know if I am specific enough.


Michael Fields comments:

Does "Link to the page user came from" refer to "any link on your blog"?