如何获得WordPress模板文件以根据当前页面插入不同的图像?

发布于 2025-01-22 09:12:23 字数 625 浏览 0 评论 0原文

我有许多页面(100),它们都在视觉编辑器(Divi)中单独构建。他们每个都有一组10-15个不同的图像,插入了相同的“包装器”中。除了图像外,页面是相同的 - 这是我的第一个网站,因此我开始时我知道的方法没有比这种方法更好。据我了解,我可以使用这些页面的自定义页面模板,以更轻松地允许可扩展性/重新设计。

我了解如何创建一个模板页面,从而将必要的页面元数据填充每个页面的标题/子标题等。我正在考虑编写一个用于循环的循环,以将页面的每个图像插入重复的“包装器”中。但是,我不确定模板如何根据访问的页面知道哪些图像进入此循环?

我对如何做到这一点有一些想法(我非常经验不足):
a)重新结构我的wp-uploads文件夹以匹配我的站点地图,并获取模板将所有图像插入匹配slug
的路径中 b)将每个页面的所有图像上传到后端中的页面(不确定是否可能),并获取模板插入与该页面关联的所有图像
c)使模板通过数据库表循环循环,该数据库表包含所有匹配的图像名称与应找到的页面ID,并且仅插入匹配当前页面ID的图像名称,

如果发生任何更改,所有100页都是子页面pagex及其slug的s始于一个通用词。例如:

mysite.com/pagex/commonword-page1/

mysite.com/pagex/commonword-page2/

I have a number of pages (100) which are all build individually in a visual editor (DIVI). They each have a set of 10-15 different images on, inserted into an identical 'wrapper'. Other than the images, the pages are identical - This was my first website and so I knew no better than this approach when I began. As I now understand, I could use a custom page template for these pages to allow scalability/re-design with much greater ease.

I understand how a template.php page could be created that pulls necessary page metadata to populate the title/subheadings etc for each page. I am thinking of writing a for loop to insert each image for the page into the repeating 'wrapper'. However, I am not sure how the template would know which images to pass into this loop based on the page visited?

I had a few thoughts on how this could be done (I am very inexperienced):
a) Re-structure my wp-uploads folder to match my sitemap and get the template to insert all images in the path that matches the slug
b) Upload all images for each page to the page in the back-end (not sure if this is possible) and get the template to insert all images associated with the page in question
c) Get the template to loop through a database table containing all of the image names matched to the page id on which they should be found and only insert those that match the current page id

If this changes anything, all 100 pages are child-pages of pageX and their slug begins with a common word. eg:

mysite.com/pageX/commonword-page1/

mysite.com/pageX/commonword-page2/

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

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

发布评论

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

评论(1

白云悠悠 2025-01-29 09:12:23

由于您有很多页面,因此我不会使用if语句。处理此操作的最佳方法是使用高级自定义字段并设置一个图像字段以在页面类型上显示。然后,在您的代码中,调用自定义字段,然后每个页面都动态显示其各自的图像。您可以在WordPress网站上的插件存储库中找到ACF,也可以直接从其网站下载。

这是图像字段文档:
https://wwwwwwwwwwweccustomfields.com/resources/resources/image/image/image/

最清洁的方式是设置字段以使用图像的URL。这是您的代码中的内容:

<?php if( get_field('field_slug') ): ?>
    <img src="<?php the_field('field_slug'); ?>" />
<?php endif; ?>

Since you have so many pages, I wouldn't use an if statement. The best way to handle this would be to use Advanced Custom Fields and set up an image field to show on the pages post type. Then, in your code, call the custom field and that way every page dynamically show their respective image. You can find ACF in the Plugins repository on your WordPress site or download it directly from their website.

Here is the image field documentation:
https://www.advancedcustomfields.com/resources/image/

The cleanest way is to set the field to use the URL of the image. Here is what would go in your code:

<?php if( get_field('field_slug') ): ?>
    <img src="<?php the_field('field_slug'); ?>" />
<?php endif; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文