Forum

Thread tagged as: Problem

Anchor navigation with a long scrolling page created with content blocks

I have a long scrolling web page made up of content blocks. I want an anchor navigation but I want the client to be able to add new blocks which automatically adds a new anchor link to the nav.

Blocks:

<perch:blocks>
  <perch:block type="txt" label="Text only" icon="circle-add">
    <perch:template path="content/assist_content_blocks/text.html" />
  </perch:block>
  <perch:block type="img" label="Full width image" icon="circle-add">
    <perch:template path="content/assist_content_blocks/full_image.html" />
  </perch:block>
  <perch:block type="imgtxt" label="Image & text" icon="circle-add">
    <perch:template path="content/assist_content_blocks/image_and_text.html" />
  </perch:block>
  <perch:block type="faqs" label="FAQ section" icon="circle-add">
    <perch:template path="content/assist_content_blocks/faqs.html" />
  </perch:block>
</perch:blocks>

All of the above block templates include a <perch:content type="text" label=“Title” id="title" title> tag and a <perch:content id="slug" type="slug" for="title"> which sets up what the anchor will jump to.

Template example:

<div class="alt-rows" id="<perch:content id="slug" type="slug" for="title">">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-10 text-center">
                <h2 class="title"><perch:content type="text" label=“Title” id="title" title></h2>
                <perch:content type="textarea" label="Description" id="desc" html="true" editor="ckeditor">
            </div>
        </div>
    </div>
</div>

I now what to make a navigation out of those tags by using perch_content_custom so this at the on top of the page:

<?php
perch_content_custom('Content Blocks', array(
        'page' => '/index.php',
        'template'=>'/content_blocks/nav.html'
));
?>

And this is the template I’ve set up to output the anchor link:

<a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>

The above is output to the page but it’s empty, no slug or title i.e. <a href="#"></a>. Can you see where I’m going wrong?

Emily Taylor

Emily Taylor 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Emily,

Since the content was created inside blocks, the links need to be outputted with blocks too:

<perch:blocks>
    <perch:block type="txt" label="Text only" icon="circle-add">
        <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>
    </perch:block>

    <perch:block type="img" label="Full width image" icon="circle-add">
        <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>
    </perch:block>

    .
    .
    .
</perch:blocks>

Thanks Hussein, I've added that to my nav.html template and it's now outputting the list but any idea what it's outputting the links twice?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Is it outputting the whole list twice? Or is each link outputted twice?

- link 1
- link 2
- link 3

- link 1
- link 2
- link 3

Or:

- link 1
- link 1
- link 2
- link 2
- link 3
- link 3

It's outputting the whole list twice. Like your first example.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Umm, check your PHP code and make sure you're not calling perch_content_custom() twice to output the links.

// you only need to call this once
perch_content_custom('Content Blocks', array(
'page' => '/index.php',
'template'=>'/content_blocks/nav.html'
));

There is only one instance of the perch_content_custom() code. Is there any other reason this could happen?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

So if you remove this instance, you get no links at all? Just to confirm you haven't missed another instance somewhere like a perch_layout().

Can you turn on debug and share the debug message for that page?

Yep, I've removed it and the whole lot disappears. Debug is below:

Debug Message - Perch 3.1.1
[30] SELECT DISTINCT settingID, settingValue FROM perch3_settings WHERE userID=0
[1] SELECT * FROM perch3_pages WHERE pagePath='/index.php' LIMIT 1
Using template: /templates/pages/attributes/default.html
Using sub-template: /templates/pages/attributes/seo.html
[1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch3_content_regions WHERE regionKey='Content Blocks' AND (regionPage='/index.php' OR regionPage='*')
[1] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=33 AND idx.itemRev=58)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID ORDER BY sortval ASC
[12] Using template: /templates/content/content_blocks/nav.html
[4] SELECT regionKey, regionHTML FROM perch3_content_regions WHERE regionPage='/index.php' OR regionPage='*' ORDER BY regionPage DESC
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I don't see anything of concern in the debug. What does your template content_blocks/nav look like?

<perch:blocks>
  <perch:block type="txt" label="Text only" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>
  </perch:block>
  <perch:block type="txt2col" label="Text in 2 columns" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>
  </perch:block>
  <perch:block type="imgtxt" label="Image & text" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>
  </perch:block>
  <perch:block type="grphtxt" label="Graphic & text" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>
  </perch:block>
  <perch:block type="faqs" label="FAQ section" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title" title></a>
  </perch:block>
</perch:blocks>
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Not sure what else to suggest. Perhaps someone else can help!

The only thing you haven't shared so far is your full PHP page code.

My page code is very straight forward, I can't see anything in it that would cause this to happen. The code I've shared is basically it really. I might try stripping it down and see what happens.

In the meantime if anyone else does want to weight in that would be great. I'm away until Monday now so I'll pick it back up then and see if anything has changed.

Thanks for your help Hussein, it's a weird one!

I've looked over this several times and I still can't figure out what is causing the list to appear twice. Could it be a bug?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show us the entire code for the page?

Yes I can. The nav is inside a javascript slide out panel, as you can see it's a really simple page.

<?php include($_SERVER['DOCUMENT_ROOT'].'/admin/runtime.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php perch_pages_title(); ?> | Evidence to Impact</title>
<?php perch_page_attributes(); ?>

<!-- Bootstrap -->
<link href="/assets/css/bootstrap.min.css" rel="stylesheet">

<link href="/assets/css/mystyles.css" rel="stylesheet">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ" crossorigin="anonymous">

<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>

<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()"><i class="fas fa-times"></i></a>
<div class="sidenav-items">
<?php
perch_content_custom('Content Blocks', array(
    'page' => '/index.php',
    'template' => 'content_blocks/content_nav.html',
    'count' => 1,
));
?>
<a href="#contact">Contact</a>
</div>
</div>

<div id="main">
<div id="header" class="assist">

<div class="container-fluid">
    <div class="row justify-content-center text-center">
        <div class="col-10 col-sm-10 col-md-6 col-lg-5">
            <img class="img-fluid assist-logo" src="/assets/img/ASSIST_logo.png" alt="ASSIST – Smoking Prevention Programme" />
        </div>
    </div>
    <div id="openNav">
        <span style="" onclick="openNav()"><i class="fas fa-bars"></i></span>
    </div>
</div><!--/.container-->

</div><!-- /#header -->

<div id="content">
<?php perch_content('Content Blocks') ?>
<?php perch_content('Contact Details') ?>
</div><!-- /#content -->

<div id="circle-row"></div>
<div id="footer">
    <div class="container">
        <div class="row justify-content-center os-animation" data-os-animation="fadeIn" data-os-animation-delay="0s">
            <div class="col-10 text-center">
                <?php perch_content('Footer Content') ?>
            </div>
        </div>
    </div>
</div><!-- /#footer -->
</div><!-- /.main -->


<a href="#" class="scrollToTop">Back <br />to the <br />top<br /><i class="fas fa-arrow-circle-up"></i></a>
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="/assets/js/bootstrap.min.js"></script>


<script>
function openNav() {
    document.getElementById("mySidenav").style.width = "300px";
    document.getElementById("main").style.marginRight = "300px";
    document.getElementById("openNav").style.display = "none";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginRight= "0";
    document.getElementById("openNav").style.display = "block";
}
</script>

<script>
// SMOOTH SCROLL TO ANCHOR
// Select all links with hashes
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function(event) {
// On-page links
if (
  location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') 
  && 
  location.hostname == this.hostname
) {
  // Figure out element to scroll to
  var target = $(this.hash);
  target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
  // Does a scroll target exist?
  if (target.length) {
    // Only prevent default if animation is actually gonna happen
    event.preventDefault();
    $('html, body').animate({
      scrollTop: target.offset().top
    }, 1000, function() {
      // Callback after animation
      // Must change focus!
      var $target = $(target);
      $target.focus();
      if ($target.is(":focus")) { // Checking if the target was focused
        return false;
      } else {
        $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
        $target.focus(); // Set focus again
      };
    });
  }
}
});

// SMOOTH SCROLL TO TOP
$(document).ready(function(){
//Check to see if the window is top if not then display button
$(window).scroll(function(){
    if ($(this).scrollTop() > 1600) {
        $('.scrollToTop').fadeIn();
    } else {
        $('.scrollToTop').fadeOut();
    }
});
//Click event to scroll to top
$('.scrollToTop').click(function(){
    $('html, body').animate({scrollTop : 0},1000);
    return false;
});
});
</script>

</body>
</html>

Content blocks template:

<perch:blocks>
  <perch:block type="txt" label="Text only" icon="circle-add">
    <perch:template path="content/content_blocks/text.html" />
  </perch:block>
  <perch:block type="txt2col" label="Text in 2 columns" icon="circle-add">
    <perch:template path="content/content_blocks/text2col.html" />
  </perch:block>
  <perch:block type="img" label="Full width image" icon="circle-add">
    <perch:template path="content/content_blocks/full_image.html" />
  </perch:block>
  <perch:block type="imgtxt" label="Image & text" icon="circle-add">
    <perch:template path="content/content_blocks/image_and_text.html" />
  </perch:block>
  <perch:block type="grphtxt" label="Graphic & text" icon="circle-add">
    <perch:template path="content/content_blocks/graphic_and_text.html" />
  </perch:block>
  <perch:block type="faqs" label="FAQ section" icon="circle-add">
    <perch:template path="content/content_blocks/faqs.html" />
  </perch:block>
  <perch:block type="boxes" label="Boxes" icon="circle-add">
    <perch:template path="content/content_blocks/boxes.html" />
  </perch:block>
  <perch:block type="testimonials" label="Testimonial slider" icon="circle-add">
    <perch:template path="content/content_blocks/testimonials.html" />
  </perch:block>
</perch:blocks>

Example content block template:

<div class="alt-rows" id="<perch:content id="slug" type="slug" for="title">">
    <div class="container">
        <div class="row justify-content-center valign-row os-animation" data-os-animation="fadeIn" data-os-animation-delay="0s">
            <perch:if exists="title">
            <div class="col-10 text-center">
                <h2 class="title"><perch:content type="text" label="Heading (required for navigation)" id="title" required title></h2>
            </div>
            </perch:if>
            <div class="col-10 col-md-12 margtop">
                <perch:content type="textarea" label="Description" id="desc" html="true" editor="ckeditor">
            </div>
            <perch:if exists="url">
            <div class="col-10 text-center margtop">
                <p><a href="<perch:content type="text" label="Link url (optional)" id="url">" class="btn"><perch:content type="text" label="Link text (required if the above link url field is filled)" id="link"> &nbsp;<i class="fas fa-plus-circle"></i></a></p>
            </div>
            </perch:if>
        </div>
    </div>
</div>

My perch_content_custom content_nav.html template:

<perch:blocks>
  <perch:block type="txt" label="Text only" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title"></a>
  </perch:block>
  <perch:block type="txt2col" label="Text in 2 columns" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title"></a>
  </perch:block>
  <perch:block type="imgtxt" label="Image & text" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title"></a>
  </perch:block>
  <perch:block type="grphtxt" label="Graphic & text" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title"></a>
  </perch:block>
  <perch:block type="faqs" label="FAQ section" icon="circle-add">
      <a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title"></a>
  </perch:block>
</perch:blocks>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You're displaying the region Content Blocks twice. Once with perch_content_custom() and once with perch_content()

But that's the whole point Drew, I want to display some of the content from the blocks in a 'nav' at the top of the page. The content forms links that then smooth scroll to that block on the page.

I've now taken the content which I want to populate the 'nav' outside of the blocks tags and used 'Allow multiple items' so each block is now a region item and it's working but the user can still chose the type of content they want to display in that region item (i.e. my blocks). If that makes sense, it's difficult to describe. So my blocks template now looks like this:

<div class="alt-rows" id="<perch:content id="slug" type="slug" for="title">">
<div class="container">
<div class="row justify-content-center valign-row os-animation" data-os-animation="fadeIn" data-os-animation-delay="0s">
<div class="col-10 text-center">
    <h2 class="title<perch:if exists="hide"> hidden</perch:if>"><perch:content type="text" label="Heading (required for navigation)" id="title" required title order="1"></h2>
    <perch:content type="checkbox" id="hide" label="Hide this title on the page but keep it in the navigation." value="yes" suppress="true" order="2">
</div>
    <perch:blocks>
      <perch:block type="txt" label="Text only" icon="circle-add">
        <perch:template path="content/assist_content_blocks/text.html" />
      </perch:block>
      <perch:block type="txt2col" label="Text in 2 columns" icon="circle-add">
        <perch:template path="content/assist_content_blocks/text2col.html" />
      </perch:block>
      <perch:block type="imgtxt" label="Image & text" icon="circle-add">
        <perch:template path="content/assist_content_blocks/image_and_text.html" />
      </perch:block>
      <perch:block type="grphtxt" label="Graphic & text" icon="circle-add">
        <perch:template path="content/assist_content_blocks/graphic_and_text.html" />
      </perch:block>
      <perch:block type="faqs" label="FAQ section" icon="circle-add">
        <perch:template path="content/assist_content_blocks/faqs.html" />
      </perch:block>
    </perch:blocks>
</div>
</div>
</div>

Example block template:

<div class="col-10 col-md-12 margtop">
    <perch:content type="textarea" label="Description" id="desc" html="true" editor="ckeditor">
</div>
<perch:if exists="url">
<div class="col-10 text-center margtop">
    <p><a href="<perch:content type="text" label="Link url (optional)" id="url">" class="btn"><perch:content type="text" label="Link text (required if the above link url field is filled)" id="link">  <i class="fas fa-plus-circle"></i></a></p>
</div>
</perch:if>

nav template is now simply:

<a href="#<perch:content id="slug" type="slug" for="title">"><perch:content type="text" id="title"></a>

That's the only way I've made it work which is fine but not quite a neat as just using the blocks.