我有一个JS,HTML和CSS3的代码段。如何将其插入wordpress html-widget?

目前,我正在帮助使用WordPress创建网站。我从开发人员那里获得了一个代码片段,该片段将显示WordPress网站上用户的LinkedIn帖子。现在,我有了JS,HTML和CSS3的代码片段,并希望将其从Wordpress上的网站构建器插件插入HTML窗口小部件中。

我尝试将JS代码插入HTML正文部分的末尾。就像JS Code一样。然后,我将整个代码段插入到网站构建器的HTML小部件中。但是到目前为止,它还没有奏效。

    <!DOCTYPE HTML>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
 </head>

 <body>
 <div id='linkedin-feed'>
 <div id='linkedin-user'></div>
 </div>



 <footer>
 </footer>

 </body>

 </html>

  const url = 'https://linkedin-feed.richdevelops.com'
  const proxyurl = "https://cors-anywhere.herokuapp.com/";
  fetch(proxyurl + url)
  .then(response => response.json())
  .then(data => {
    // console.log(data) // Heres the feed array 
  let userImage = data[0].profileImageSource
  let userDescription = data[0].profileDescription
  let username = data[0].profileName
  let user = `
  <div>
    <img src='${userImage}'></img>
    <p>${username}</p>
    <p>${userDescription}</p>
  </div>
  `
  let linkedinUser = document.querySelector('#linkedin-user')
  linkedinUser.innerHTML = user
  let feed = ''
    for (item of data){ //Itterate through each post.
    let title = item.title
        let subTitle = item.subTitle
    let imageSource = item.imageSource
    let postUrl = item.postUrl
    let postedUrl = item.postedUrl
    let body = item.body
    if (item.type==='link'){
      feed = `
        <h3 class='title'>${title}</h3>
        <h4 class='subtitle'>${subTitle}</h4>
        <div class='image'>
        <img src='${imageSource}'></img></div>
        <a href='${postUrl}'><p>Linkedin Post Link</p></a>
        <a href='${postedUrl}'><p>Posted Url</p></a>
      `
    } else if (item.type==='post'){
      feed = `
        <div class='subtitle'>${body}</div>
        <a href='${postUrl}'><p>Linkedin Post Link</p></a>
      `
    }
    let div = document.createElement('div')
        div.classList.add('feed-item')
    div.innerHTML = feed
    let linkedinFeed = document.querySelector('#linkedin-feed')
    linkedinFeed.appendChild(div)
    }
 })
 .catch(error => console.error(error))



// CSS CODE in a seperate file


.feed-item {
  border: 1px black solid; 
  max-width: 600px;
  padding: 10px;
}

img{max-width:150px;}

我希望输出显示Wordpress网站上用户的LinkedIn提要。

ili21477 回答:我有一个JS,HTML和CSS3的代码段。如何将其插入wordpress html-widget?

将CSS文件上传到您的服务器并包含它:

df3

然后将JS文件上传到您的服务器并包含它:

<link rel="stylesheet" type="text/css" href="https://your-site.com/css/mycss.css">

因此,<script type="text/javascript" src="https://your-site.com/js/myjs.js"></script> 的代码应该看起来像这样:

HTML module
,

正确的代码结构应如下所示: 您忘记放置脚本和样式标签供浏览器解释

    <!DOCTYPE HTML>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
 </head>

 <body>
     <div id='linkedin-feed'>
        <div id='linkedin-user'></div>
     </div>


    <script type="text/javascript">
          const url = 'https://linkedin-feed.richdevelops.com'
          const proxyurl = "https://cors-anywhere.herokuapp.com/";
          fetch(proxyurl + url)
          .then(response => response.json())
          .then(data => {
            // console.log(data) // Heres the feed array 
          let userImage = data[0].profileImageSource
          let userDescription = data[0].profileDescription
          let userName = data[0].profileName
          let user = `
          <div>
            <img src='${userImage}'></img>
            <p>${userName}</p>
            <p>${userDescription}</p>
          </div>
          `
          let linkedinUser = document.querySelector('#linkedin-user')
          linkedinUser.innerHTML = user
          let feed = ''
            for (item of data){ //Itterate through each post.
            let title = item.title
                let subTitle = item.subTitle
            let imageSource = item.imageSource
            let postUrl = item.postUrl
            let postedUrl = item.postedUrl
            let body = item.body
            if (item.type==='link'){
              feed = `
                <h3 class='title'>${title}</h3>
                <h4 class='subtitle'>${subTitle}</h4>
                <div class='image'>
                <img src='${imageSource}'></img></div>
                <a href='${postUrl}'><p>Linkedin Post Link</p></a>
                <a href='${postedUrl}'><p>Posted Url</p></a>
              `
            } else if (item.type==='post'){
              feed = `
                <div class='subtitle'>${body}</div>
                <a href='${postUrl}'><p>Linkedin Post Link</p></a>
              `
            }
            let div = document.createElement('div')
                div.classList.add('feed-item')
            div.innerHTML = feed
            let linkedinFeed = document.querySelector('#linkedin-feed')
            linkedinFeed.appendChild(div)
            }
         })
         .catch(error => console.error(error))
    </script>


    <style type="text/css">
        /* CSS CODE in a seperate file */
        .feed-item {
          border: 1px black solid; 
          max-width: 600px;
          padding: 10px;
        }

        img{max-width:150px;}
    </style>

 <footer>
 </footer>

 </body>

 </html>

现在,您只需要在任何接受html的小部件中添加以下代码:

     <div id='linkedin-feed'>
        <div id='linkedin-user'></div>
     </div>


    <script type="text/javascript">
          const url = 'https://linkedin-feed.richdevelops.com'
          const proxyurl = "https://cors-anywhere.herokuapp.com/";
          fetch(proxyurl + url)
          .then(response => response.json())
          .then(data => {
            // console.log(data) // Heres the feed array 
          let userImage = data[0].profileImageSource
          let userDescription = data[0].profileDescription
          let userName = data[0].profileName
          let user = `
          <div>
            <img src='${userImage}'></img>
            <p>${userName}</p>
            <p>${userDescription}</p>
          </div>
          `
          let linkedinUser = document.querySelector('#linkedin-user')
          linkedinUser.innerHTML = user
          let feed = ''
            for (item of data){ //Itterate through each post.
            let title = item.title
                let subTitle = item.subTitle
            let imageSource = item.imageSource
            let postUrl = item.postUrl
            let postedUrl = item.postedUrl
            let body = item.body
            if (item.type==='link'){
              feed = `
                <h3 class='title'>${title}</h3>
                <h4 class='subtitle'>${subTitle}</h4>
                <div class='image'>
                <img src='${imageSource}'></img></div>
                <a href='${postUrl}'><p>Linkedin Post Link</p></a>
                <a href='${postedUrl}'><p>Posted Url</p></a>
              `
            } else if (item.type==='post'){
              feed = `
                <div class='subtitle'>${body}</div>
                <a href='${postUrl}'><p>Linkedin Post Link</p></a>
              `
            }
            let div = document.createElement('div')
                div.classList.add('feed-item')
            div.innerHTML = feed
            let linkedinFeed = document.querySelector('#linkedin-feed')
            linkedinFeed.appendChild(div)
            }
         })
         .catch(error => console.error(error))
    </script>


    <style type="text/css">
        /* CSS CODE in a seperate file */
        .feed-item {
          border: 1px black solid; 
          max-width: 600px;
          padding: 10px;
        }

        img{max-width:150px;}
    </style>

但是您可以将css添加到外部css文件中,并将js添加到javascript文件中,然后只需在小部件内添加html代码即可

本文链接:https://www.f2er.com/3153825.html

大家都在问