By Stevep-1 on
Hi,
I've searched the forum but it seems no one has addressed the problem of long pages ... and the convenience of having a "Back to top of page" link to an anchor at the top of the page.
I know I can use a pager to break up the pages, but I would like to have the option of displaying long pages without break.
Does anyone know the required php code to conditionally display the html link should the page extend beyond a certain size?
Looking forward to any suggestions.
Steve
Comments
Why can't you just press
Why can't you just press Home?
need more info
I don't know of any way to immediately identify a page as a "long" page. Perhaps you could give some more information as to what you perceive a long page to be? More than ten nodes displayed? More than 1000 words in a blog entry?
If you wish to put a "Back to top of page" link on every page, you could edit your page template (page.tpl.php, assuming you're using phptemplate), and put in an anchor at the top and a link to the anchor at the bottom.
re need more info
Thanks for your reply.
Yes, I was thinking more than, say 8 nodes would then display the html link.
I had already edited the page.tpl.php file, but the link "Back to top of page" was displaying on single-node pages as well, which obviously was redundant (and perhaps potentially confusing to some).
As for the "home" suggestion, putting a home link anywhere near the bottom would send the visitor back to the top of the Home page, not the current page.
I had wondered if something like the following was close (assuming there's a (a name="top") suitably located):
(<)?php if ($pager_page_array[$element] > 8): (?>)
(<)a href="#top" title="Back to top">Back to top of page<(/a>)
(<?)php endif; (?>)
But it doesn't work, and not being a php programmer, was wondering what the right hook was...
Any suggestions appreciated.
Best ..
Stevep
I think the variable you
I think the variable you want is
$pager_total_items.Start by overriding the page theming function. First find the current page theming function - most likely this will be phptemplate_page() in the file phptemplate.engine in the folder themes/engines/phptemplate. Copy/paste this function in the new location: in your theme folder (let's say it's called "stevep"), edit the file template.php and copy the function in here, and rename it stevep_page(). At the beginning of stevep_page(), add the following lines:
global $pager_total_items;Further down in stevep_page(), add another element to the
$variablesarray:The variable
$total_items, if it is set, is now available to your page template.Now, edit the page template. You can now check to see if
$total_itemsis set and if it exceeds a certain number, and insert the "top of page" link:My apologies if the code isn't exact, but I think it should be pretty close. Let me know if you're having trouble with this.
Regards,
Ben
Another way..simpler?
For what it's worth, here is how I did it without modifying the theme. I just had few long pages and all of them were based on view that pulled articles from a particular category. So If you have just few pages then go to site building->views , choose the view that is creating the long page and in the 'page section' of the view add a the following line to the header:
<a name="top"></a>In the footer part of the same section enter:
<a href="#top" title="Back to top">Back to top of page</a>This should create a 'Back to Top' link in the footer section of that particular page view. Ofcourse, as mentioned earlier this is useful only if you have couple fo pages in your website that produce these long pages. If you have a larger website, then for consistency use Ben's solution.
Hope this helps.
Best way to make back to top link
The best way to make a link to go to the top of the page is to simply write #. That is
<a href="#">back to top</a>. This will take you directly to the top of the page, whereas when you use anchors, getting to the very top of a page is a bit harder.Hope this helps!
Computers are like air conditioners, once you open the windows, they both stop working.
Submitted as a feature request
Submitted this as a feature request here: http://drupal.org/node/775226
Hi Ben, Thanks for this ...
Hi Ben,
Thanks for this ... I've not worked it in yet. I've just checked in to see if any replies .. and to reply if so. But it's late, early morning here (Australia), and won't get to it for a few days.
I didn't realise the amount of mods required ... but since it will be contained within the theme I'm using (Foundation), it'll carry over when upgrading the core system.
I'll let you know how it goes.
Best,
Steve
It might help!!!
While using l() function for creating page link
l($text, $path, $options = array());
Additional $options elements used by the url() function.
There is "fragment" property described in url() options argument
'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
Now the final drupal statement as
for ex.
l('Comments', '/node/'.$node->nid, array('fragment' => 'comments'));
This will add "#comments" at the end of the url
like /node/28/#comments
I think it helps!!
Bhimrao
Block and Javascript
Is this of any help.
- Create a new block and place it either in the content (last position), content_bottom or footer region.
- Hide the block title with
- Add this to the source of the block
**************************************************
<script language="JavaScript">
page_height = (document.documentElement.scrollHeight);
if (page_height > 1000) {
document.writeln('<a href="#top">Top of page</a>');
}
</script>
<noscript>
<a href="#top">Top of page</a>
</noscript>
***************************************************
You can change the 1000 to suit your needs but basically any page that has a scroll height of over 1000 pixel will show the 'Top of Page' link. If a user has JS turned off that the link still shows but will show on every page regardless of height.
I've also added id=''top" to the page H1 inside the page.tpl.php file. You may want to put it higher in the template.
CSS code is:
*****************************************************
#block-block-XX {
padding-top:1em;
text-align: right;
}
****************************************************
Change the XX to your block ID number.
Certified Drupal Site Builder 7 & 8