Custom Field (via Shortcode) inside Query Loop block
-
I use a couple of custom field in my posts.
To expose the value in the post, I’ve created a simple shortcode function, for example this for a custom field called ‘sector’:
add_shortcode('cf_sector', 'cf_sc_sector'); function cf_sc_sector($atts=[], $content=null){ global $post; return get_post_meta( $post_id=$post->ID, $key = 'sector', $single = true ); }
This works perfectly in the post, for example /test/test_post
However, it does not work when I add the same short code inside the Post Template block within a Query Loop example /test_loop
On further investigation it seems that the global variable $post is referencing the containing page, not the post inside the loop.
Am I doing something wrong ??
I’m new to WP and this forum so please send me to right location if this isn’t it, thanks.
- This topic was modified 2 years, 6 months ago by . Reason: code format fixed
- This topic was modified 2 years, 6 months ago by .
The page I need help with: [log in to see the link]
- The topic ‘Custom Field (via Shortcode) inside Query Loop block’ is closed to new replies.