How to include PHP file in Wordpress that is outsi

2019-08-12 07:27发布

问题:

I am using a custom CMS for a website that deals with 90% of the site's pages and content.

However, for the news/blog section of the site, I am using Wordpress.

I'm able to include the necessary Wordpress files to pull in posts to the main site, but cannot do the reverse.

I want to be able to call a php file into the header of Wordpress so I have access to the CMS's classes, primarily for navigation.

I've tried a search but can't seem to find an answer.

Additional Information:

  • Wordpress is installed in site.com/wordpress
  • The file I want to include is in site.com/includes

Clarification:

I don't want to create a custom template or include Wordpress functionality outside of Wordpress. I want to pull external classes from a CMS, which is a level above Wordpress, into Wordpress so that I can use it throughout the Wordpress installation.

回答1:

You should go with custom page template.

Its an easy way to include php file in wordpress because in thisway you can easily include wordpress theme header and footer.

To create a custom page template.

You have to simply create new .php page and at start of page just include this code.

<?php
/*
Template Name: My custom page template name
*/
?>
// your code 

To read in detail about Wordpress custom page template click here

I hope it will help you.



回答2:

Method 1 :

<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>

Method 2 :

<?php
define('WP_USE_THEMES', false);
require('./wp-load.php');
?>


回答3:

I managed to fix this by altering the include line to reference the folder above the Wordpress root.

So, the line became require_once "../path/to/file" instead of just require_once "/path/to/file"



标签: php wordpress