[英]how to use the wordpress site url and concatination for link href
I work on a wordpress site and i have some trouble to generate the correct href link dynamically to link to different sites. 我在wordpress网站上工作,在动态生成正确的href链接以链接到其他网站时遇到了一些麻烦。 My goal is to link to an id for a site section to scroll to, if you click a specific button/box.
我的目标是链接到网站部分的ID(如果您单击特定的按钮/框,则可以滚动到该ID)。 The box is wrapped inside an a tag and should get a dynamic link in the href to go to the specified section.
该框包装在一个标签内,应该在href中获得一个动态链接以转到指定的部分。 The dynamic link should consist of the site url and the site/#id for the section to go to
动态链接应包含该部分要转到的站点URL和站点/#id。
here is what i have done so far! 这是我到目前为止所做的!
My blade file looks like this: 我的刀片文件如下所示:
<?php
$techboxes = $block->getBox();
$mh_group = "technology-boxes-" . rand();
global $wp;
$site_url = home_url(add_query_arg(array(),$wp->request));
?>
<div class="section section--margin-md">
<div class="section__content">
<div class="technology-teaser">
<?php
$title = $block->getTitle();
$subtitle = $block->getSubtitle();
$description = $block->getDescription();
?>
<h2 class="technology-teaser__inner__title">{{$title}}</h2>
<h3 class="technology-teaser__inner__subtitle">{{$subtitle}}</h3>
<div class="technology-teaser__inner__description">{{$description}}</div>
</div>
<div class="technology-teaser__boxes">
@foreach ($techboxes as $techbox)
<?php
$title = $techbox['title'];
$description = $techbox['description'];
$link = $techbox['link'];
$current_url = $site_url . "/technolgie";
?>
<a href="{{$site_url}}" class="technology-teaser__boxes__box">
<div class="technology-teaser__boxes__box__image bg-image" style="background-image: url({{$techbox['background']}})">
</div>
<div class="technology-teaser__boxes__box__text">
<div class="js-matchheight" data-mh="{{ $mh_group }}">
<h2 class="technology-teaser__boxes__box__text__title">{{$title}}</h2>
<div class="technology-teaser__boxes__box__text__description">
{{$description}}
</div>
</div>
</div>
<div class="technology-teaser__boxes__box__text__link btn btn--yellow">
<i class="btn__icon white-icon"></i>
<span class="btn__text">mehr erfahren</span>
</div>
</a>
@endforeach
</div>
</div>
</div>
I found 我发现
global $wp;
$site_url = home_url(add_query_arg(array(),$wp->request));
which returns the right site url (i checked with var_dump). 返回正确的网站网址(我用var_dump检查)。 Now i would like to add a specific site to the $site_url.
现在,我想向$ site_url添加一个特定站点。 I do this with:
我这样做:
$current_url = $site_url . "/technolgie";
here /technolgie is the site the where the link should go to and i will later ad another variable which holds the id of the section on the technolgie site. 这里/ technolgie是链接应该转到的站点,稍后我将投放另一个变量,该变量包含technolgie站点上该部分的ID。
If i var_dump $current_url it gives back a string with the right address. 如果我var_dump $ current_url它返回一个带有正确地址的字符串。 However it links not to the right page, it just reloads and do nothing.
但是,它没有链接到正确的页面,它只是重新加载而没有执行任何操作。 If i typ in the adress in my browser, it works fine.
如果我在浏览器中输入地址,则效果很好。 So im obviously missing something important here and someone of you is having a hint for me about this.
因此,我在这里显然错过了一些重要的事情,你们当中有人向我暗示了这一点。
regards daniel. 问候丹尼尔。
I got it and the answer is quite easy. 我明白了,答案很简单。 Instead of using the sitename you would like to link to you need to concatinate the page id, like so:
您无需链接站点名称,而需要链接页面ID,如下所示:
$site_url = home_url(add_query_arg(array(),$wp->request)) . '/' . '?p=44';
With this you can use $site_url inside your href and can link to the site. 这样,您可以在href内使用$ site_url并可以链接到该站点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.