0

I'm working on a nuxt.js project, and I have a page loading content (basically a text) from a WordPress post, with an $http request on create() Hook.

I'd like to use the title as og:title meta, but I can't use it since the data is not loaded when the head method is executed.

How can I use the WordPress post title loaded by $http request as a oh:title meta ?

1 Answer 1

1

Don't use the created hook, use the asyncData for the cases where you want to render things from the server-side.

Example code:

async asyncData({$axios, params }) {
    const post = ($axios.get(`/api/posts/${params.id}`)).data
    return {
      post
    }
  }

After that, just use the variable title inside the head() {} that provided by nuxt.js.

Example Code:

head() {
      return {
        title: this.post.title,
        meta: [
          {
            hid: 'og:title',
            content: this.post.title,
            property: 'og:title'
          },
        ]
      }
    }

Here is one article, it may help you:

https://dripcoder.com/posts/how-to-add-open-graph-meta-tags-to-your-blog-post-in-nuxtjs/

Sign up to request clarification or add additional context in comments.

1 Comment

Hey buddy ! Thanks for the answer, this save me a lot of headhaches !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.