Page 1 of 2
Load PHP code into Static Page
Posted: Thu Oct 17, 2024 4:24 pm
by neogeo
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?
Re: Load PHP code into Static Page
Posted: Thu Oct 17, 2024 9:43 pm
by Edi
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.
Re: Load PHP code into Static Page
Posted: Thu Oct 17, 2024 11:00 pm
by arfa
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.
Re: Load PHP code into Static Page
Posted: Fri Oct 18, 2024 5:17 pm
by neogeo
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?
Re: Load PHP code into Static Page
Posted: Fri Oct 18, 2024 5:25 pm
by neogeo
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
Re: Load PHP code into Static Page
Posted: Fri Oct 18, 2024 6:11 pm
by Edi
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?
Re: Load PHP code into Static Page
Posted: Fri Oct 18, 2024 7:29 pm
by neogeo
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
Re: Load PHP code into Static Page
Posted: Fri Oct 18, 2024 9:09 pm
by Kamikaze
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.
Re: Load PHP code into Static Page
Posted: Fri Oct 18, 2024 10:53 pm
by arfa
@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
Re: Load PHP code into Static Page
Posted: Fri Oct 18, 2024 11:35 pm
by Edi
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');
}