【问题标题】:how to use the wordpress site url and concatination for link href如何使用 wordpress 网站 url 和链接 href 的连接
【发布时间】:2017-06-08 13:47:14
【问题描述】:

我在一个 wordpress 网站上工作,我在动态生成正确的 href 链接以链接到不同的网站时遇到了一些麻烦。如果您单击特定按钮/框,我的目标是链接到要滚动到的站点部分的 id。该框被包裹在一个 a 标记内,并且应该在 href 中获得一个动态链接以转到指定的部分。动态链接应包含站点 url 和要转到的部分的站点/#id

这是我到目前为止所做的!

我的刀片文件如下所示:

<?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>

我找到了

global $wp;
$site_url = home_url(add_query_arg(array(),$wp->request));

它返回正确的站点 url(我用 var_dump 检查过)。 现在我想将特定站点添加到 $site_url。我这样做:

$current_url = $site_url . "/technolgie";

这里 /technolgie 是链接应该指向的站点,稍后我将添加另一个变量,该变量包含 technolgie 站点上部分的 id。

如果我 var_dump $current_url 它返回一个带有正确地址的字符串。但是它没有链接到正确的页面,它只是重新加载并且什么都不做。如果我在浏览器中输入地址,它工作正常。所以我显然在这里遗漏了一些重要的东西,你们中的某个人正在向我暗示这件事。

问候丹尼尔。

【问题讨论】:

    标签: php wordpress laravel content-management-system blade


    【解决方案1】:

    我明白了,答案很简单。而不是使用您想要链接到的站点名称,您需要连接页面 id,如下所示:

    $site_url = home_url(add_query_arg(array(),$wp->request)) . '/' . '?p=44';
    

    有了这个,您可以在您的 href 中使用 $site_url 并可以链接到该站点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 1970-01-01
      • 2021-10-31
      相关资源
      最近更新 更多