Forum

Thread tagged as: Problem

Perch_content_custom 'page'

I have some [master pages] set up, and I want to use perch_content_custom to change the background of a section depending on it's folder.

Cattle Folder index.php - [course-category.php] (includes original perch_content which sets the background image for this category) cattle-course.php - [course.php] (uses perch_content_custom to get it's backgorund)

Sheep Folder exactly as above in cattle folder

However I have to refer to the 'page' starting at the root of the site. I have tried just putting in index.php as the page url which results in php not outputting anything. Is there a clever way to select index.php in the same folder as the current page?

Thanks

Adam Menczykowski

Adam Menczykowski 1 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't really follow. Can you show us the code that isn't working, along with what result you're getting and what result you're hoping for?

Sure:

From course-category.php

<section class="courses" style="<?php perch_content('Course Navigation Background Image'); ?>">
    <?php 
    perch_pages_navigation(array(
        'from-path'=>'*',
        'from-level'=>2,
        'template' => 'course-grid.html'     
    )); ?>
</section>

And referring to that file is course-single.php

<section class="courses" style="<?php perch_content_custom('Course Navigation Background Image', array('page' => '*', 'template' => 'course-image.html') ); ?>">
    <?php 
    perch_pages_navigation(array(
        'from-path'=>'*',
        'from-level'=>2,
        'template' => 'course-grid.html'     
    )); ?>
</section>

Currently where the '*' is I would like to refer to the index.php in the same folder as the current page.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Probably the easiest way is to get the current page path, explode it on a forward slash, drop the last item from the array, then append index.php and implode it on a forward slash again.

After looking around the internet thanks to your explanation I have found:

To get the current page path i found here:

https://stackoverflow.com/questions/6768793/get-the-full-url-in-php

$current_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Explode that:

https://board.phpbuilder.com/showthread.php?10380358-RESOLVED-explode-is-causing-problems

$link_array = explode("/", $current_link); 

Take a look at the output using var_dump:

https://www.php.net/manual/en/function.var-dump.php

var_dump ($link_array);

Pop the last item off, then append 'index.php':

https://www.php.net/manual/en/function.array-pop.php

https://stackoverflow.com/questions/6797641/php-how-to-add-to-end-of-array

$remove_last_item = array_pop($link_array);
$link_array []= 'index.php';

Implode the array:

$url = implode("/", $link_array );
echo $url;

outputs: /courses/cattle/index.php

However, I can't get this next part to work:

style="<?php perch_content_custom('Course Navigation Background Image', array('page' => $url, 'template' => 'course-image.html') ); ?>"

Thanks, Adam

The reason is probably that the variable $url outputs /courses/cattle/index.php

however what I need to output is:

'/courses/cattle/index.php'

Manage to wrap the variable in quotes but perch_content_custom still doesn't work:

<?php 
    $current_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $link_array = explode("/", $current_link); 
    array_pop($link_array); 
    $link_array []= 'index.php';
    $url = implode("/", $link_array );
    $quote = "'";
    $echourl = $quote . $url . $quote;
?>

which outputs '/courses/cattle/index.php'

style="<?php perch_content_custom('Course Navigation Background Image', 
                array(
                'page' => $echourl, 
                'template' => 'course-image.html'
                    ) ); ?>"

by the way course-image.html outputs a css background style. It works elsewhere in the site.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Perch doesn't care about the domain name - just the path from the root of the site. Don't use the domain or protocol.

That's what I am doing, aren't I?

'/courses/cattle/index.php'

It seems that giving perch_content_custom a straight url to the page works, however a variable with exactly the same content as the url will not work.

This works

<?php perch_content_custom('Course Navigation Background Image', 
                array(
                'page' => 'courses/cattle/index.php', 
                'template' => 'course-image.html'
                    ) ); ?>

This doesn't work

<?php perch_content_custom('Course Navigation Background Image', 
                array(
                'page' => $taggedurl, 
                'template' => 'course-image.html'
                    ) ); ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Make sure the path starts with a slash - the top instance shouldn't actually work!

Sorry, it actually does work - that was a typo.

$echourl = '/courses/cattle/index.php'

like I said, typing in the url in quotes works fine, putting in the $echourl variable doesn't.

Thanks for all your help so far.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't understand why you're adding the quotes to the string. The path doesn't contain quotes.

To clarify, when using perch_content_custom, if a url is referenced it is normally put in quotes as standard. When obtaining the url dynamically in a variable using this method, it outputs without quotes.

I still haven't managed to get this working.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Quotes are used to denote a string. If you have a string, you don't put quotes into it.