首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在特定的Wordpress页面上运行JavaScript函数?

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

Stack Overflow用户
提问于 2018-07-14 06:46:04
回答 1查看 253关注 0票数 0

我试图在特定的Wordpress页面上运行以下脚本,但它不起作用。该脚本可以工作,但当不应用于特定页面时。是的,页面id是正确的。我做错什么了?提前谢谢。

代码语言:javascript
复制
<?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; ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-14 07:09:00

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

根据您的代码示例,您应该使用一个整数作为您的帖子ID,而不是字符串,也不是'page-id‘部分。

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

下面是一些示例用法:

代码语言:javascript
复制
// 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' );

在您的情况下,您应该使用:

代码语言:javascript
复制
<?php if (is_page(48857) ):?>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51333685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档