Load PHP code into Static Page

User avatar
neogeo
Ssr. Bludit
Posts: 23
Joined: Tue Sep 29, 2015 7:06 pm
Has thanked: 8 times
Been thanked: 3 times

I would like to load a php-code into a specific static page:

This code lists all tags:

Code: Select all

<?php
    // Returns an array with all the tags
    $items = getTags();
    
    foreach ($items as $tag) {
        echo 'Tag name: '       . $tag->name();
    }
?>
How could I load that code on specific static page only. Is it possible?
User avatar
Edi
Site Admin
Posts: 3168
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 66 times
Been thanked: 90 times
Contact:

You have to add the code in a template.

In the template index.php you can define an if condition for the template with the php code.
arfa
Master Bludit
Posts: 98
Joined: Tue Jul 04, 2017 4:31 am
Location: New Zealand
Has thanked: 6 times
Been thanked: 14 times

There is a snippet plugin: PHP Shortcode Snippets ($4)
I haven't tried it but it could work for you.

On the template add:

Code: Select all

<?php $slug = $page->slug();
echo 'slug='.$slug;
 ?>
Load any page and this will show you/confirm which page you are on.
Then you can try in the template something like:

Code: Select all

<?php 
if ($page->isStatic() && $slug=='thePageYouWant') { 
include "pathToTheCode.php";
 }
?>
I haven't tested this but used versions of it so it should work.
User avatar
neogeo
Ssr. Bludit
Posts: 23
Joined: Tue Sep 29, 2015 7:06 pm
Has thanked: 8 times
Been thanked: 3 times

Edi wrote: Thu Oct 17, 2024 9:43 pm You have to add the code in a template.

In the template index.php you can define an if condition for the template with the php code.
That's interesting, it would solve my "problem". Do you know which is the php-condition to show the tags only to a specific page-url-slug?
Last edited by neogeo on Fri Oct 18, 2024 5:29 pm, edited 1 time in total.
User avatar
neogeo
Ssr. Bludit
Posts: 23
Joined: Tue Sep 29, 2015 7:06 pm
Has thanked: 8 times
Been thanked: 3 times

arfa wrote: Thu Oct 17, 2024 11:00 pm There is a snippet plugin: PHP Shortcode Snippets ($4)
I haven't tried it but it could work for you.

On the template add:

Code: Select all

<?php $slug = $page->slug();
echo 'slug='.$slug;
 ?>
Load any page and this will show you/confirm which page you are on.
Then you can try in the template something like:

Code: Select all

<?php 
if ($page->isStatic() && $slug=='thePageYouWant') { 
include "pathToTheCode.php";
 }
?>
I haven't tested this but used versions of it so it should work.
Thank you very much for the suggestion!

Few years ago I bought "PHP Shortcode Snippets", the problem is that it does not work with Php 8 (only does with php7) and their author don't reply so I can't use it at all :/

If anyone has php knowledge I can send him the Plugin to try to update it for Php 8.x
User avatar
Edi
Site Admin
Posts: 3168
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 66 times
Been thanked: 90 times
Contact:

neogeo wrote: Fri Oct 18, 2024 5:25 pm Few years ago I bought "PHP Shortcode Snippets", the problem is that it does not work with Php 8 (only does with php7) and their author don't reply so I can't use it at all :/
Who is the author?
User avatar
neogeo
Ssr. Bludit
Posts: 23
Joined: Tue Sep 29, 2015 7:06 pm
Has thanked: 8 times
Been thanked: 3 times

Edi wrote: Fri Oct 18, 2024 6:11 pm
neogeo wrote: Fri Oct 18, 2024 5:25 pm Few years ago I bought "PHP Shortcode Snippets", the problem is that it does not work with Php 8 (only does with php7) and their author don't reply so I can't use it at all :/
Who is the author?
@Zebraslive
Website: http://aestheticode.net
Github: https://github.com/Zebraslive/
Addon: https://plugins.bludit.com/plugin/php-shortcodes
Kamikaze
Jr. Bludit
Posts: 3
Joined: Mon Sep 30, 2024 7:33 pm
Been thanked: 1 time

index.php

Code: Select all

<?php
if($WHERE_AM_I == 'page' && !empty($page->template())) {
	include(THEME_DIR_PHP . $page->template() . '.php');
}
?>
Template name (for example: tag.php) - an additional file in the folder with the theme.

Code: Select all

<?php
$items = getTags();

foreach ($items as $tag) {
	echo '<p>' . $tag->name() . '</p>';
}
?>
When creating or editing an entry, specify the name of the template in the parameters (in my example, tag) without extension .php and everything works as you need.
arfa
Master Bludit
Posts: 98
Joined: Tue Jul 04, 2017 4:31 am
Location: New Zealand
Has thanked: 6 times
Been thanked: 14 times

@Kamikaze,

I would be happy to have a go at tweaking the snippet code. I had actually thought about exploring building a plugin that does that but... why reinvent the wheel?

You msg or mail me.

ak
User avatar
Edi
Site Admin
Posts: 3168
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 66 times
Been thanked: 90 times
Contact:

neogeo wrote: Fri Oct 18, 2024 5:17 pm Do you know which is the php-condition to show the tags only to a specific page-url-slug?
You have two possiblities.

1) Add an if condition to the template

You can enclose your code with the if condition:

Code: Select all

<?php
    if ($page->slug() == "page-url-slug") {    
        $items = getTags();
        foreach ($items as $tag) {
            echo 'Tag name: '       . $tag->name();
        }
    }
?>
2) Use an own template

You can use an own template, for example page-url-slug.php.

In this case you can modify the section $WHERE_AM_I in the index.php of your theme.

For example:

Code: Select all

if ($WHERE_AM_I == 'page') {
    if ($page->slug() == "page-url-slug") {
        include(THEME_DIR_PHP.'page-url-slug.php');
    }
    else {
        include(THEME_DIR_PHP.'page.php');
    }
} else {
	include(THEME_DIR_PHP.'home.php');
}
Post Reply