Archived topic
Best way to show custom field values on pages
7 replies · Started by Carlo on May 21, 2021
Hi there,
we are redesigning our website.
There are about 30 pages (they are pages, not posts) on which we want to show a section of data.
Each page has its own data and we take those data from a bunch of custom fields related to each page.
FIRST SOLUTION:
we can create an element block - hook
get dynamic data from the custom fields (dynamic post type: post meta)
In this case scenario, what's the best way to assign this element to only those 30 pages (in the display rules section)?
Is there any alternative to manually select each of those 30 pages in the display rules section? (something like a category or else as I'd do if they were posts instead of pages)
SECOND SOLUTION:
As an alternative, in each single page I could use acf shortcodes to get those values. But if I had to modify the design or adding a new data element, I'd have to modify each single page instead just one unique element as in the first solution.
What you guys would suggest?
Thank you :)
Hi there,
You can fetch the custom field through its shortcode if you're using a custom field plugin. (ACF).
Or you can use the get_post_meta() function for the built in one.
Example: get_post_meta(get_the_ID(), 'subtitle', TRUE);
And you can make this into a portable shortcode.
Example:
add_shortcode('display_meta', function($atts){
$atts = shortcode_atts( array(
'field' => ''
), $atts, 'display_meta' );
echo '<div class="display-meta-wrapper">'. get_post_meta(get_the_ID(), $atts['field'], TRUE).'</div>';
});
Which can work as [display_meta field="custom_field_slug"]. But the good thing about this is you can modify the shortcode to change the wrapper element and classes. It's a bit like your second solution, only more specific and customized.
Thank you Elvin,
really useful in many situations. :)
So you would prefer this to solution 1? Any particular reason?
Hi there,
both methods have their merits. The benefit of option 2 is you can add it inside the Post Content, which cannot be done with a Block Element. If its outside of the post content the option 1 is the way to go.
See this topic here for creating a custom taxonomy specifically for Element display rules:
https://generatepress.com/forums/topic/header-layout-control-for-client/#post-1421567
Thank you David, your solution for creating a custom taxonomy specifically for Element display rules is awesome!
I have just implemented it and it works great. :)
Do you think it could also work for wpshowposts pro somehow? I mean, to "tell" wpshowposts to show all the pages with the same custom taxonomy used for element display rules?
I can't see why not - in the WPSP list it should show your new Taxonomy in the tax dropdown.
yes... it works!
Thank you again David you solved me 2 issues at the same time :D
Awesome - glad to be of help