Я получил фрагмент кода с JS, HTML и CSS3. Как мне вставить его в WordPress HTML-виджет? - PullRequest
0 голосов
/ 06 ноября 2019

В настоящее время я помогаю создавать веб-сайт с помощью WordPress. Я получил от разработчика фрагмент кода, который покажет сообщения LinkedIn пользователя на сайте WordPress. Теперь я получил фрагмент кода с JS, HTML и CSS3 и хочу вставить его в виджет HTML из плагина построителя сайтов на Wordpress.

Я пытался вставить код 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;}

Я ожидаю, что в выводе должен быть показан канал LinkedIn пользователя на сайте Wordpress.

Ответы [ 2 ]

0 голосов
/ 06 ноября 2019

Правильная структура вашего кода должна выглядеть следующим образом: вы забыли поместить теги скрипта и стиля для браузера, чтобы интерпретировать

    <!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-код внутри виджета, который тоже будет работать

0 голосов
/ 06 ноября 2019

Загрузите файл CSS на свой сервер и включите его:

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

Затем загрузите файл JS на свой сервер и также включите его:

<script type="text/javascript" src="https://your-site.com/js/myjs.js"></script>

Итак, в конце концовкод для HTML module должен выглядеть примерно так:

<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>
<div id='linkedin-feed'>
<div id='linkedin-user'></div>
</div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...