为什么“了解更多”链接未链接到页面?

时间:2019-05-02 14:59:11

标签: php wordpress custom-wordpress-pages

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

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

$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;
}

1 个答案:

答案 0 :(得分:0)

您已经在使用$slide->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;
}