Is it possible to load content/sections dynamically?

Hi @doughballs,

That’s not really possible. It’s stored in post meta of the page/post and as serialized data. It’s not stored like that as a HTML (<div class="e121-164 x-section">...</div>).

And serialized data shouldn’t be edited or touched since it’s system generated. You could corrupt it which will result to non-working page/post.

If you’re just planning to capture <div class="e121-164 x-section">...</div>, then you can simply load the actual page using ajax and parse it as XML/HTML. Example, https://stackoverflow.com/questions/48176377/scraping-a-webpage-that-uses-ajax. Though, we can’t provide customization or support related to this issue.

Thanks for understanding.

Thanks @Rad, I think that’s a little too advanced for me right now and I want to work within the architecture of PRO as much as possible. One other way I figured I can do it is to have the content I need to load stored in an array, in an external JS file. I read this thread and article:

Is this still the way I need to do it, even if I just want to to reference an array that I have put together? There is no functionality, just variable storage. I would write it in the JS customizer but it’s a little lengthy and I think it will be easier to edit later as a standalone JS file that contains an array object that made up of the various bits of text I need to pull in. It’s a bit hacky but might be quicker than figuring out AJAX.

Hi @doughballs,

Yes, you still need to do that. It’s Wordpress standard and not by the theme. You’ll have to enqueue it and use it to display your content to your preferred section. But yes, easier than the AJAX since it will require both ends to be coded (source and receiver).

Thanks!

Perfect @Rad. Enqueuing worked a treat :slight_smile:

Glad to hear that, cheers!

@Rad - is there a way to have the script available to a specific page only?

Hello @doughballs,

Thanks for updating the thread.

Yes, that can be achieved by using WordPress conditional tags. I am sharing the resource that you can take a look to learn more.

https://codex.wordpress.org/Conditional_Tags
https://www.isitwp.com/ultimate-guide-wordpress-conditional-tags/

Thanks.

@Prasant

So I’ve lost the JS file I created and the functionality with - I’m guessing through an update?

I created a JS folder in themes>pro and created my custom js file there. Now this folder has disappeared. Note I am not using a child theme for this project. Can you tell me how to reinstate the JS folder and file, and a location where it is safe from being overwritten?

Note I was following this thread, which was instructing use of child themes. To be honest I have read up on child themes and I still don’t understand why I would use one over a parent, and how updates work on children differently to the parent:

Hey @doughballs,

You will need to use a child theme for this and put the file and folder in your child theme in the same directory where you placed in in the parent theme before.

To learn the difference between a parent and child theme, please see https://developer.wordpress.org/themes/advanced-topics/child-themes/

We have a starter child theme available to help you get started. Please read https://theme.co/apex/forum/t/setup-how-to-setup-child-themes/57. Just note that all customizations that you will do to it would be your responsibility.

Hope that helps.

Thanks, @christian, again I read the docs but I don’t understand why you wouldn’t always use a child theme? What are the explicit benefits of using a parent theme? This is not covered in the article.

I already have a pro child theme loaded in my themes - if I switch to this, will this be the only change I need to make (beyond creating the JS folder and file)?

Thanks for your help.

Hi @doughballs,

It’s Wordpress standard, the purpose of the child theme is customization. If a user will not implement any customization, then the child theme has no use and it’s okay not to be installed. But, if you’re customizing it and adding codes, then you should use it since the parent theme (the original codes) are always overwritten by updates. It’s the same with plugins so you shouldn’t customize the plugins (but there are no child plugins).

A child theme is an empty theme, you’ll only add what you needed to implement. It serves just a container without a need for parent theme’s editing.

Thanks!

@Rad. Thankyou. That clears it up. Child theme for me from now on!

I have an issue now - I’ve switched to a child theme (I was using this briefly about two weeks ago - do child themes need updating? It is showing V1.00), created the JS folder in the pro-child folder, created the js file and updated the functions.php to enqueue the script, but there is an error. For some reason, the browser is looking for the script in themes/pro/js, instead of themes/pro-child/js:

Note previously I did create a JS folder in themes/pro, and had it working til the update - is this some cache issue? I did a hard reload of the page, no dice.

Hi,

You can try this code instead.

function wpb_adding_scripts() { 
    wp_register_script('nk_script', get_stylesheet_directory().'/js/nk.js', array('jquery'),'1.1', true); 
    wp_enqueue_script('nk_script');
}  
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  

Hope this helps

Hi @paul.r

This didn’t work - it’s added a crazy path before the directory. See secure note.

Thanks,
Sean

Hi @doughballs,

Ah I see what’s the problem and there must be typo error, please change this line

wp_register_script('nk_script', get_stylesheet_directory().'/js/nk.js', array('jquery'),'1.1', true);

to this

wp_register_script('nk_script', get_stylesheet_directory_uri().'/js/nk.js', array('jquery'),'1.1', true);

Thanks!

Spot on @Rad, thankyou.

Sean

You are welcome Sean :slight_smile:

@Prasant, you previously shared two articles about using conditionals - I am comfortable with the concept, but one thing I am not sure about is how they interact with the enqueuing. If I have more than one custom js script, do I add them individually, or within the same function(seperated by commas)? And then how would I use the conditional - is it on the add_action? Would I wrap that in a conditional?

Right now I would say it seems like I’d need to create a second function, say wp_add_second_script(), and reference the second script inside this, and then have the conditional wrapped around a second add_action function. Is this correct?

Hi @doughballs

This tutorial has a great example explaining doing that:

Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.