如何在特定的Wordpress页面上运行JavaScript函数?

时间:2018-07-13 22:46:04

标签: javascript php wordpress

我正在尝试在特定的Wordpress页面上运行以下脚本,但无法正常工作。该脚本可以工作,但是不适用于特定页面。是的,页面ID是正确的。我做错了什么?提前致谢。

<?php if (is_page('page-id-48857') ):?>
    <script>
        jQuery(document).ready(function ($) {
        $(function () {
            var $content = $('#jsonContent');
            var data = {
                rss_url: 'https://inside.calpoly.edu/feed'
            };
            $.get('https://api.rss2json.com/v1/api.json', data, function (response) {
                if (response.status == 'ok') {
                    var output = '';
                    $.each(response.items, function (k, item) {

                        output += '<div class="post-card category-medium published">';
                        //output += '<h3 class="date">' + $.format.date(item.pubDate, "dd<br>MMM") + "</h4>";
                        var tagIndex = item.description.indexOf('<img'); // Find where the img tag starts
                        var srcIndex = item.description.substring(tagIndex).indexOf('src=') + tagIndex; // Find where the src attribute starts
                        var srcStart = srcIndex + 5; // Find where the actual image URL starts; 5 for the length of 'src="'
                        var srcEnd = item.description.substring(srcStart).indexOf('"') + srcStart; // Find where the URL ends
                        var src = item.description.substring(srcStart, srcEnd); // Extract just the URL
                        output += '<p class="post-meta">';
                        //output += '<span class="published">' + item.pubDate + '</span>';
                        output += '<a href="https://inside.calpoly.edu/" target="_blank">@inside.calpoly.edu</span></a>';
                        output += '</p>';

                        output += '<h2 class="entry-title">' + item.title + '</h2>';
                        //output += '<div class="post-meta"><span>By ' + item.author + '</span></div>';
                        var yourString = item.description.replace(/<img[^>]*>/g,""); //replace with your string.
                        var maxLength = 300 // maximum number of characters to extract
                        //trim the string to the maximum length
                        var trimmedString = yourString.substr(0, maxLength);
                        //re-trim if we are in the middle of a word
                        trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))

                        output += '<div class="excerpt">'+trimmedString + '</div>';
                        output += '<a href="'+ item.link + '" class="more-link cpc-button activeghostdark small">Read More</a>';
                        output += '<a class="entry-featured-image-url" href="'+ item.link + '"><img src="' + src + '"></a>';

                        output += '</div>';
                        return k < 1;
                    });
                    $content.html(output);
                }
            });
        });
    </script>

<?php endif; ?>

2 个答案:

答案 0 :(得分:4)

您正在向is_page()函数传递无效的ID。

根据您的代码示例,您应为帖子ID使用整数,而不是字符串,也不要使用“ page-id”部分。

https://developer.wordpress.org/reference/functions/is_page/

以下是一些用法示例:

// When any single Page is being displayed.
is_page();

// When Page 42 (ID) is being displayed.
is_page( 42 );

// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );

// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );

您应该使用的是:

<?php if (is_page(48857) ):?>

答案 1 :(得分:2)

WordPress的is_page()函数需要“页面ID,标题,子句或此类的数组”。 'page-id-48857'是正文类,您只需使用is_page( 48857 ),因为实际ID仅为48857

还请注意,您应该认真考虑使用wp_enqueue_script(),而不是在自定义脚本标签中进行编码。它将为您节省未来无数的头痛。