【问题标题】:Why is the "Learn More" link not linking to the page?为什么“了解更多”链接没有链接到该页面?
【发布时间】:2019-05-02 14:59:11
【问题描述】:

我试图了解这段代码(来自其他开发人员)是如何编写的。它有一个错误,但我似乎无法修复它。了解更多链接不链接到自定义字段中的帖子。

我已尝试删除“了解更多”行,但它随后将幻灯片链接更改为链接到图像本身,而不是自定义链接字段中的内容。

$slides = ONS_Slide_Custom_Post_Type::find_all('DESC');
if (isset($slides) && count($slides > 0)) {
    $items = array();
    foreach ($slides as $slide) {
        //echo '<tt><pre>' . var_export($slide, true) . '</pre></tt>';
        $item = new stdClass();
        if (isset($slide->custom_data) && count($slide->custom_data) > 0) {
            if (isset($slide->custom_data['ons_slide_image'])) {
                $item->src = $slide->custom_data['ons_slide_image'];
            }
            if (isset($slide->custom_data['ons_slide_heading'])) {
                $item->heading = $slide->custom_data['ons_slide_heading'];
                $item->heading .= '<span class="punctuation">.</span><span class="learn_more">&nbsp;&raquo;</span>';
            }
            if (isset($slide->custom_data['ons_slide_caption'])) {
                $item->caption = $slide->custom_data['ons_slide_caption'];
                $item->caption .= '&nbsp;<a href="#" class="learn_more">Learn more &raquo;</a>';

            }
            if (isset($slide->custom_data['ons_slide_href'])) {
                $item->href = $slide->custom_data['ons_slide_href'];

            } else {
                $item->href = "#";
            }
        }
        $items[] = $item;
    }
    $carousel = new ONS_Bootstrap_Carousel($items);
    echo $carousel;
}

【问题讨论】:

  • 那是因为&lt;a href="#"&gt;改成&lt;a href="ARTICLE URL"&gt;
  • “文章 URL”应该由 custom_data 字段“ons_slide_href”动态填充
  • 对,那就这样吧
  • 这行 $item-&gt;href = $slide-&gt;custom_data['ons_slide_href']; 似乎应该将 URL 注入到幻灯片中。找到填充该数组的代码,了解它是如何工作的,并修复任何需要修复的东西,以便它完成它应该做的事情。
  • @cabrerahector 我更新了帖子以包含我在单独文件中找到的一个类。我附加的代码是新找到的文件的完整代码。您在此处看到任何可能导致问题的内容吗?

标签: php wordpress custom-wordpress-pages


【解决方案1】:

您已经在使用$slide-&gt;custom_data['ons_slide_href']; 做一些事情,但是在输出锚标记的代码行之后。

所以试着像这样切换处理

$slides = ONS_Slide_Custom_Post_Type::find_all('DESC');
if (isset($slides) && count($slides > 0)) {
    $items = array();
    foreach ($slides as $slide) {
        //echo '<tt><pre>' . var_export($slide, true) . '</pre></tt>';
        $item = new stdClass();
        if (isset($slide->custom_data) && count($slide->custom_data) > 0) {
            if (isset($slide->custom_data['ons_slide_image'])) {
                $item->src = $slide->custom_data['ons_slide_image'];
            }
            if (isset($slide->custom_data['ons_slide_heading'])) {
                $item->heading = $slide->custom_data['ons_slide_heading'];
                $item->heading .= '<span class="punctuation">.</span><span class="learn_more">&nbsp;&raquo;</span>';
            }

        // moved this code above the anchor tag line
            if (isset($slide->custom_data['ons_slide_href'])) {
                $item->href = $slide->custom_data['ons_slide_href'];
            } else {
                $item->href = "#";
            }

        // Now concatenate $item->href in the anchor tag line
            if (isset($slide->custom_data['ons_slide_caption'])) {
                $item->caption = $slide->custom_data['ons_slide_caption'];
                $item->caption .= '&nbsp;<a href="' . $item->href . '" class="learn_more">Learn more &raquo;</a>';

            }
        }

        $items[] = $item;
    }
    $carousel = new ONS_Bootstrap_Carousel($items);
    echo $carousel;
}

【讨论】:

  • 太棒了。这就像一个魅力。 @RiggsFolly 你能解释一下为什么这篇文章被否决了吗?抱歉,我是新手,但我很乐意为社区做出贡献。也许我只是还不知道礼仪?
  • 嗨,可能是因为它看起来很像一个 TYPO,即代码都在那里,但顺序错误,一个简单的眼球注意到 href 属性中只有一个 #。但你永远无法完全确定,有些人 DV 没有任何解释
  • 好消息有些人也无缘无故地投票了 :)
猜你喜欢
  • 1970-01-01
  • 2017-12-19
  • 2017-05-22
  • 1970-01-01
  • 2016-04-07
  • 2011-03-25
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多