【问题标题】:How to fetch the page id and use it for an image name如何获取页面 id 并将其用作图像名称
【发布时间】:2015-06-15 06:21:04
【问题描述】:

我是 php 新手。我想为显示页面 ID 设置条件并将我的图像名称设置为 pageId 并在我的标题横幅中显示此背景图像。如何获取页面 id 使用它为我的横幅背景 img 获取图像。

这是我的代码:

<div class="splash">
    <img src="<?php echo get_template_directory_uri(); ?>/images/<?php echo $page;?>.jpg" />
</div>

如果更改页面,它也会在不同页面更改标题横幅图像中循环工作。

我在这段代码中有问题。它适用于页面,但如果我进入内页或页面的帖子,它会显示我想隐藏的横幅。我该怎么做呢

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    你已经接近了......假设你在一个帖子/页面模板上,你可以使用get_the_ID()

    <img src="<?php echo get_template_directory_uri() . '/images/' . get_the_ID() . '.jpg'; ?>" />
    

    【讨论】:

      【解决方案2】:
      <?php $postid = get_the_ID() ?>
      <div class="splash">
                        <img src="<?php echo get_template_directory_uri(); ?>/images/<?php echo  $postid;?>.jpg" />
                      </div>
      

      【讨论】:

      • 回答工作正常,但如果我进入任何类别,它就不起作用,你能告诉我在这种情况下如何做吗?
      • 类内是什么意思?
      【解决方案3】:

      页面 ID 和页面横幅

      • 您可以拥有一个 MySQL(或任何 RDBMS)表“页面”,其中包含列 [id、title、body、image_path]
      • 我刚刚尝试假设“页面”表的基本字段 - 您可以根据您的要求进一步扩展它。

      所需功能

      • 表准备好后,您可以编写函数
      • getBannerByPageId(id) - 此函数将返回整个 image path 以及图像名称。
      • getPageId() - 此函数将返回当前显示页面的id。这里的 id 可以从 URL 中获取。如果您使用的是干净的 url,那么您将需要扩展您的功能来映射 page idurl alias

      【讨论】:

        【解决方案4】:

        使用此代码...

        <?php $postid = get_the_ID(); ?> //to get page id
        

        参考get page id

        <?php get_template_directory_uri(); ?> //to get directory uri..
        

        参考get directory uri

        将它们结合起来

        <div class="splash">
        
        <?php $postid = get_the_ID(); ?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/<?php echo $postid; ?>.jpg" />
        

        【讨论】:

          猜你喜欢
          • 2020-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多