EDIT:
If you use $post in a function, you have to define it of course:
function xyz(){
global $post;
//Your code
}
Initial answer, might help others:
It appears to me, you do not use it inside an action or if so, you use it to early. It should work inside the ‘init’ action. So you could do something like
<?php
add_action( 'init', 'enqueue_conditionally' );
function enqueue_conditionally(){
$mathjax = get_post_meta( get_the_ID(),'mathjax',true);
if($mathjax == 'y'){
wp_enqueue_script('mathjax');
}
}
?>
Ref.: https://codex.wordpress.org/Plugin_API/Action_Reference/init
Related Posts:
- Add validation and error handling when saving custom fields?
- Loading scripts to the Post Edit page only
- Validate data on attachment_fields_to_save
- How to load an assets based on custom field value?
- How to enable custom fields for pages (if not a bad practice)?
- Best way to programmatically remove a category/term from a post
- Add a Jquery Datepicker to custom field in post edit
- Add new “Insert Into Post” button with another function
- Custom per-page sidebar widgets .. possible?
- Multiple meta values for same meta_key adding on “Preview Changes” hit but not on saving or updating post
- How to add new custom page or post blocks?
- Transients vs CRON +Custom Fields: Caching Data Per Post
- How to update serialized data in the user meta data
- Unable to save datetime custom meta field using update_post_meta() function
- Guest Author – How can I use custom fields to create guest author link?
- Create additional short URL with custom field and 301 redirect
- enabling custom fields in admin?
- get_post_meta fields don’t show up on posts page
- Using custom fields in a filter hook
- Function to change meta value in database for each post
- Using WP Query to search by multiple meta fields
- Get aggregate list of all custom fields for entire blog
- WP_Query display next custom post from today’s date
- Can ordering post list by meta_value cause performance issue?
- How to Identify the Source of a Custom Field?
- Include woocommerce custom field value in front-end search result
- woocommerce product custom field
- MySQL Query that looks for post with Custom Field, then changes Category
- two custom fields question
- How to save multiple options from a dropdown in user profile
- How to display multiple images in custom field
- How to show a gloabl message on a user profile page (in back end)?
- Create Pop-Up Box with Custom Field Content Inside the Loop
- Display the contents of a custom field of a page on their child
- Read / Watch / Listen times – meta
- Login WP – Connect single field to an external api
- adding extra wordpress user info from registration form
- Custom Meta Box (SELECT) Not Saving
- jQuery – Automatic event trigger does not **really** check my form
- Display URL in a Custom Field
- Turn raw url into clickable links
- Advanced Custom Fields functionality without plugin
- Get post ids sorted by meta_key
- Advanced Custom Fields Plugin – Unable to get URL from post_object
- Plot location on Google map using full address specified in custom field
- Display custom field value as a hyperlink
- Unable to get specific value from post meta
- pre_get_posts : only get posts by wp_usermeta value
- Display list of terms having the same custom field value
- How to display custom field in woocommerce email orders? [closed]
- Custom meta is not being saved
- 3.8 Media Upload and Custom Meta Box
- Displaying Custom Field Array
- Query post by date (stored custom field meta as yyyymmdd) and differentiate across 12 months
- IF Custom field value equals ZERO
- Let’s Create Custom Field Template Documentation
- Custom Field Repeating When Using foreach
- Accessing Custom Fields on Pages
- Can I save post meta programatically without setting metaboxes?
- Save attachment ID instead of URL in custom field
- If metabox has content display content
- How to change permalink on headlines in rss feeds?
- Listing the most popular 8 city (custom fields) as used in posts
- How to delete file uploaded by Plupload
- Display Meta Data with HTML?
- How to order posts by number of Vimeo Likes
- Displaying a video on the homepage via a custom field inside a php widget
- Displaying custom field meta within WP_query loop
- Custom Field Date Problem
- Approach to creating a site with this structure
- how to create meta key name in wp_usermeta table in database
- Display the output from Custom User Fields from a WordPress user profile on a WordPress Custom Page
- Meta field bulk editing no longer working in WP 6.0 [closed]
- get_field values for each post on home page using wp_add_inline_style
- Delete user meta but only if found in array
- Is there a way to determine which user changed a custom field (and when)?
- Metabox/Custom fields are not saving input data
- meta_query to check all custom fields
- Show image depending on the number in the custom fields
- Show echo ready in single.php
- Save, update, get and sanitize post meta as HTML not plain
- Order categories by sum of custom fields
- how to modify a field’s size in a metabox : ROWS height, and scroll control?
- Saved Post Meta Array Returns as String
- display Flexible Fields from options
- Thirtieth century date for a post
- Saving metadata of related post on save_post
- Custom field in external Javascript
- Custom field value link title
- How to Order Posts by Custom Fields?
- Analyze uploaded images to get colour values
- Turn Current Date in Custom Field Ouput Green or Red
- added a meta box to post however when saving menu while debug on throws a warning
- How can i get the actor birthday by date?
- list pages by custom_field?
- Get custom field from page, in a post?
- Make Custom Fields Public in JSON – API
- Order by a meta field in query loop
- Multiple triggers when publishing, saving or updating a post in WordPress
- If possible a field ID transfom in a Custom Field?