如何重用 WordPress 网站的部分内容,例如多个 WordPress 网站的页眉、页脚、页眉的一部分?

发布于 2024-12-05 18:55:48 字数 1749 浏览 0 评论 0原文

我正在寻找一种解决方案,以在我的 WordPress 网站之一中重用页眉和页脚导航链接(当然带有样式)<几个其他 WordPress 网站

请注意,我正在尝试在 WordPress 站点之间共享页眉和页脚,而不是 从 WordPress 网站到 PHP 页面。 我所指的网站位于同一服务器上。我有以下目录结构:

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

如果可能的话,我非常感谢有关如何实现此目标和最佳实践的一些指导,因为我对 WordPress 仍然是新手。 我心里有一些想法,但我不确定哪一个是最佳编程实践,或者每种方法需要多少努力(用于成本效益分析)

1)在主站点的functions.php中编写一个自定义get_header()函数以允许提取导航链接数 file_get_contents() 从 wp-content/themes/my-theme/inc/footer.php 获取导航链接 在我使用的 some-other-site/

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

目前,我得到 "Background" 作为输出,所以它对我来说还不起作用。

我发现一个类似的主题但问题是我有点不清楚,使用绝对网址的解决方案不是一个好的做法,有人告诉我

2) 将这些导航链接公开为 Web 服务。我有一种感觉,网络服务在这里甚至不相关,但我仍然把它放在这里以防万一。

3) 使用多站点设置或为我的所有 WordPress 站点创建一个网络。虽然这似乎是最好的方法,但它似乎相当复杂,而且当前在网络中设置我的主站点实际上存在问题。我怀疑是否有必要克服这个复杂的情况来实现我的目标。

据我所知,WordPress 网络中的站点共享某些数据库,因此我非常担心会丢失主站点中的部分或全部海量数据。

如果您足够友善,那么指出在 WordPress 站点之间共享 CSS 样式表和 Javascript 文件的最佳实践绝对是相关的:)

更新
我决定

  • 尽可能坚持使用 WordPress 多站点
  • 放弃了上面提到的不良实践层次结构 - 嵌套 WordPress 目录

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="<?php bloginfo('siteurl'); ?>/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2024-12-12 18:55:48

如果您想在父级和所有子级 WordPress 博客中包含整个 header.phpfooter.php 文件,则无需上传副本的唯一方法是是使用绝对路径,或者为您想要包含它的每个区域单独创建一个嵌套的相对路径。

您的站点层次结构的布局方式通常被认为是......不好的做法。也许我误解了其背后的原因,但我建议与网站所有者评估当前情况并提出替代方案(具有一些主要好处 - 用户体验和结构)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文