简体   繁体   中英

How to reuse parts of WordPress site e.g. header, footer, part of header for multiple WordPress sites?

I am looking for a solution to reuse the header and footer navigation links ( with style , of course) in one of my WordPress website for several other WordPress sites .

Please note that I'm trying to share header and footer among WordPress sites, not from WordPress site to a PHP page . The sites I'm referring to are on the same server. I have the following directory structure:

example.com/ #main site is here
   some-other-site/
      wp-admin/
      wp-content/
      wp-include/
      ...
   wp-admin/
   wp-content/
   wp-include/
   ...

I would really appreciate some direction on how to achieve this goals and best practices, if possible since I am still new to WordPress. I have a few ideas in mind but I am not sure which one is best programming practice or how much effort each approach requires (for cost benefit analysis)

1) Write a custom get_header() function in the main site's functions.php to allow extraction of navigation links file_get_contents() to get the navigation links from wp-content/themes/my-theme/inc/footer.php in some-other-site/ I use

define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');

Currently, I get "<a href="&lt;?php bloginfo('siteurl'); ?&gt;/articles/category/editorial_team">Background</a>" "<a href="&lt;?php bloginfo('siteurl'); ?&gt;/articles/category/editorial_team">Background</a>" as output so it doesn't work for me yet.

I found one similar topic but the question is a bit unclear to me and the solution of using absolute urls is not a good practice, I was told .

2) Expose those navigation links as web service. I have a feeling that web service is not even relevant here but I still put it here just in case.

3) Use Multisite settings or create a network for all my WordPress sites. While this appears to be the best way, it seems quite complicated and there are actually issues with my main site being setup in a network currently. I doubt it's necessary to got through this complication to achieve my goal.

As far as I know, sites in WordPress network shares certain databases and therefore I'm so afraid of losing some or whole of the huge data in my main site.

It would definitely be relevant to point out the best practices in sharing CSS stylesheets and Javascripts file among WordPress sits as well, if you are kind enough :)

Updates
I've decided to

  • Stick to wordpress multisite as much as possible
  • Abandoned the poor practice hierarchy mentioned above - nested WordPress directories

If you'd like to include the entire header.php and footer.php files in the parent and all child wordpress blogs, the only way of doing that without uploading copies would be to use an absolute path, or create a nested relative path individually for each area that you'd like to include it in.

The way your site hierarchy is laid out is generally considered... poor practice. Perhaps I am misunderstanding the reasoning behind it, but I would suggest evaluating the current situation with the site owner and suggesting an alternative (with some major benefits - ux and structurally).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM