wordpress - PHP include only on homepage URL -


i need include external php file on homepage of website.

but, since pages on site use same page template (homepage template), cant filter them based on wondering there way include php file on homepage url (which www.domain.com/folder) , not show on other page (for example www.domain.com/folder/lorem).

i tried using snippet in header.php file:

<?php    if ($_server['php_self'] = '/')    include('some-file.php'); ?> 

and file gets included on other pages well.

i php newbie sorry if stupid question :)

update: did changed

<?php    if ($_server['php_self'] == '/')    include('some-file.php'); ?> 

and still isnt showing up.

you can use wordpress's is_front_page() function check.

thus, code should be:

<?php      // if code not work, adding next line should make work     <?php wp_reset_query(); ?>      if ( is_front_page() ) {     include('some-file.php'); }  ?> 

source: https://codex.wordpress.org/function_reference/is_front_page

alternatively, if above not working, can try:

if ( $_server["request_uri"] == '/' ) {     include('some-file.php'); } 

as last resort, try using plugins insert php directly pages, 1 such plugin https://wordpress.org/plugins/insert-php/.

update: after elaboration in comments, i've come alternate method, shown below.

in case, might work. code url first, parse directory, , assign directory $directory. if on homepage, $directory not set, include some-file.php.

<?php     // url    $link = "http://$_server[http_host]$_server[request_uri]";     // directory (eg. return 'folder' in example.com/folder/files)    $parts = explode('/', $link);    $directory = $parts[3];     // if $directory null, include php (eg. example.com, there no directory)    if ($directory == ''){       include('some-file.php');    }  ?> 

hope above methods help, thanks!


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -