Category Archives: PHP

Concrete5: How to display a page’s unique ID

Here is the line of code I use to display a page’s unique ID.

<?php echo $c->getCollectionID() ?>

This is really useful for styling individual pages.

Concrete5: Display the page title of the top parent menu item on sub pages

Here is the block of code I use to display the page title of the top parent menu item on a sub (or child) page of that menu item.

Copy this code:

<?php
$title = $c->getCollectionName();
$parentPageId = $c->getCollectionParentID();
if ($parentPageId > 1) {
    $parentPage = Page::getById($parentPageId);
    $title = $parentPage->getCollectionName();
}
?>
<h1><?php echo $title; ?></h1>
And paste it into the template where you want the parent item page title to appear.
Using this code, the page title of the top-level menu item relative to the page the viewer is on will always be displayed. For example, if this were your site map:
Home
About
      History
      Mission
Contact
      Directions
      Contact Us
Using the code above, you could display the page title About when you are on the History page or the Mission page, and the page title Contact when you are on Directions or Contact Us.

WordPress Tricks: Get the titles of recent blog posts from a certain category using wp_query

Here is a simple block of code that you can use to get the most recent post titles and permalinks to a certain number of the most recent posts in a certain category:

 

<ul>
<?php
    global $cat;
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5&cat=389');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?></ul>

Simply change the number of posts and the category id in the code above to get the number of posts and the category of posts that you want.

 

You can also check out my post on how to display a list of the titles of your most recent blog posts from several categories if you are looking for a way to get the most recent posts from several different categories.

A simple PHP script to find your web root directory

Here is a simple line of code you can use to find the web root and path of your website.

Copy and paste this code into a text file:

<?php
print ($_SERVER['DOCUMENT_ROOT']);
?>

Save the file as findwebroot.php or whatever name you want, as long as it has a php extension.

Upload the file to your server, navigate to the file in a browser, and voilà - you will see the full path to that file!

Concrete5: How to add a unique body class to individual pages

Here is the easiest way I have found to add a unique body class to individual pages in a Concrete5 site.

First, in your dashboard go to “Pages and Themes,”then “Attributes,” and add a custom attribute (text field) called bodyclass.

Next, open your theme’s header.php file and add:

class="<?php echo $c->getCollectionAttributeValue('bodyclass')?>"

to your < body > tag, so it reads like this:

<body class="<?php echo $c->getCollectionAttributeValue('bodyclass')?>">

For any page that you want to apply a custom class to, simply add the Body Class custom attribute and type whatever class name you want into the Body Class Custom Attribute text field. When you save your changes, your opening < body > tag will now look like:

<body class="red">

How to add a unique class to each li item in Concrete5 autonav menu

First, copy and paste this code into a blank text file:

<?php
   defined('C5_EXECUTE') or die("Access Denied.");
   $aBlocks = $controller->generateNav();
   $c = Page::getCurrentPage();
   echo("<ul class=\"nav\">");

   $nh = Loader::helper('navigation');

   $isFirst = true;
   foreach($aBlocks as $ni) {
      $_c = $ni->getCollectionObject();
      if (!$_c->getCollectionAttributeValue('exclude_nav')) {

         $target = $ni->getTarget();
         if ($target != '') {
            $target = 'target="' . $target . '"';
         }

         if ($ni->isActive($c) || strpos($c->getCollectionPath(), $_c->getCollectionPath()) === 0) {
            $navSelected='nav-selected';
         } else {
            $navSelected = '';
         }

         $pageLink = false;

         if ($_c->getCollectionAttributeValue('replace_link_with_first_in_nav')) {
            $subPage = $_c->getFirstChild();
            if ($subPage instanceof Page) {
               $pageLink = $nh->getLinkToCollection($subPage);
            }
         }

         if (!$pageLink) {
            $pageLink = $ni->getURL();
         }

         if ($isFirst) $isFirstClass = 'first';
         else $isFirstClass = '';

         echo '<li id="'.$navSelected.'" class="item'.$_c->getCollectionID().' '.$isFirstClass.'">';

         if ($c->getCollectionID() == $_c->getCollectionID()) {
            echo('<a class="nav-selected" href="' . $pageLink . '"  ' . $target . '>' . $ni->getName() . '</a>');
         } else {
            echo('<a href="' . $pageLink . '"  ' . $target . '>' . $ni->getName() . '</a>');
         }   

         echo('</li>');
         $isFirst = false;
      }
   }

   echo('</ul>');

Save this file as new-nav.php or whatever you want it to be called. Upload this template to your concrete/blocks/autonav/templates.

Now, in your template file, use this block of code to call the new menu:

<?php
$nav = BlockType::getByHandle('autonav');
$nav->controller->displayPages = 'top';
$nav->controller->orderBy = 'display_asc';
$nav->controller->displaySubPages = 'all';  
 $nav->render('templates/new_nav');                 

Note: make sure that you use the correct name of your new template in the code above.

Or, if you are adding or editing an autonav block from the front end of the site, simply select the “New nav” custom template and apply it to the block.

Let me know if you run into any questions or have any issues with this.