我是php新手。我想创建一个显示页面ID的条件,并设置我的图像名称是pageId并在我的标题横幅中显示此背景图像。如何获取页面ID使用它为我的横幅背景img。
获取图像这是我的代码:
<div class="splash">
<img src="<?php echo get_template_directory_uri(); ?>/images/<?php echo $page;?>.jpg" />
</div>
如果更改页面,它也会在不同的页面更改标题横幅图像中循环工作。
我在此代码中遇到问题。它适用于页面,但如果我去内页或页面的帖子,它显示横幅我想隐藏它。我怎么能这样做
答案 0 :(得分:0)
你很接近......假设你在一个帖子/页面模板上,你可以使用get_the_ID():
<img src="<?php echo get_template_directory_uri() . '/images/' . get_the_ID() . '.jpg'; ?>" />
答案 1 :(得分:0)
<?php $postid = get_the_ID() ?>
<div class="splash">
<img src="<?php echo get_template_directory_uri(); ?>/images/<?php echo $postid;?>.jpg" />
</div>
答案 2 :(得分:0)
页面ID和页面横幅
所需功能
image path
以及图像名称。id
。这里可以从URL获取id。如果您使用的是干净的网址,则需要扩展您的功能以映射page id
和url alias
。 答案 3 :(得分:0)
使用此代码...
<?php $postid = get_the_ID(); ?> //to get page id
<?php get_template_directory_uri(); ?> //to get directory uri..
并将这些结合起来
<div class="splash">
<?php $postid = get_the_ID(); ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/<?php echo $postid; ?>.jpg" />