• Resolved philipt18

    (@philipt18)


    I’m trying to display multiple custom fields in a post. I’d like to build a template, essentially a table where the custom field data is pulled into the right spots. In many posts the text of that table will be identical, just calling to get the different data from the custom fields. Is there a way to create a shortcode of the template, so I don’t need to generate the same table in each post (and can edit one template as needed to change all the posts)?

    For example, let’s say I have custom fields firstname and lastname. I want a table like:

    <table>
    <tr><td>First Name:</td><td>[content field=”firstname”]</td></tr>
    <tr><td>Last Name:</td><td>[content field=”lastname”]</td></tr>
    </table>

    to show up on multiple pages. Is there a way to create a shortcode I can use instead of the above? thanks.

    https://ww.wp.xz.cn/plugins/custom-content-shortcode/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Eliot Akira

    (@miyarakira)

    Yes, there are several ways you could make a template and display in multiple posts.

    The easiest way is to put the template code in a separate page, and include that page wherever you need it: [content type="page" name="table-template"]. If you plan to have many templates, you can create a custom post type for it.

    Or, you can put the template code in a file and include it: [load dir="theme" file="table-template.html"]

    Inside the template, you can use the [if] shortcode to display different things depending on which post is using the template. For example, you can display an extra row for posts in a specific category.

    <table>
        <tr><td>First Name:</td><td>[content field="firstname"]</td></tr>
        <tr><td>Last Name:</td><td>[content field="lastname"]</td></tr>
        [if category="special"]
            <tr><td>Category:</td><td>Special person</td></tr>
        [/if]
    </table>
    Thread Starter philipt18

    (@philipt18)

    Thank you. Interesting using the IF. Can I use IF to display content if a field in non-NULL?

    I also noticed the Live Edit support in your documentation. Not exactly sure how that works, but would be interested to integrate that as well.

    I couldn’t find the documentation on using IF, and if there is a way to test for the existence of a field. I’m also not sure where to insert the Live Edit tags in the code. Do I need it for each field? Do I just surround the [content] tags with [liveedit] tags?

    Thank you.

    Plugin Author Eliot Akira

    (@miyarakira)

    To check if a field is empty or not:

    [if flag="field_name"]
      The field exists.
    [/if]

    For description of the [if] shortcode, please see under Settings -> Custom Content, under the IF tab.

    About Live Edit..I’m not sure how practical it would be for your purpose, but you can see how it works. Its documentation is a bit hidden: under the Overview tab, click on Advanced Custom Fields (its developer is the one who made Live Edit) and at the bottom of that page, there’s a description of how to install Live Edit and use the shortcode.

    Thread Starter philipt18

    (@philipt18)

    Thank you.

    Thread Starter philipt18

    (@philipt18)

    So I’ve been working on the data to import to all the custom fields, which I imported from a CSV. The custom fields seem to have been imported properly. I have the content of each page set to:

    [load dir=”content” file=”table.html”]

    and it appears to load that file properly, because I see the table in the post.

    However, none of the custom fields seem to have been loaded into the table. In the table code, I have something like:

    <tr><th>First Name</th><td>[content field=”first_name”]</td></tr>

    By the way, is that the same as:

    <tr><th>First Name</th><td>[field first_name]</td></tr>

    I’m guessing this has something to do with using ACF. Does using an ACF-created field need a different syntax? I saw the documentation on using special ACF features such as galleries, but couldn’t find any documentation on using simple fields. Thanks.

    Plugin Author Eliot Akira

    (@miyarakira)

    Yes, [field first_name] and [content field="first_name"] are the same.

    I have several fields created by ACF, and they work just like any other custom field. To be honest, I haven’t tested with ACF 5 yet, but it should be backward compatible.

    Have you confirmed that the field name is correct, under Dashboard -> Content?

    Thread Starter philipt18

    (@philipt18)

    How do I confirm the field names are correct there? I don’t actually see the custom field names on that page, should I? I’m actually using ACF 4.3.8. I did notice that act has its own short code, so I switched to [acf field=”first_name”] and that worked.

    I’m currently using srp (special-recent-posts) as well, although I suspect I could build a replacement for that within your plugin. they’re not working so well together. I wonder if it has to do with the order that each short code is rendered.

    Plugin Author Eliot Akira

    (@miyarakira)

    Hmm, if you don’t see the field listed in the Content Overview, then possibly the field is a hidden one, with the slug starting with underscore, “_”. But, from my experience, fields made in ACF should be listed like any other custom field.

    I’ll look into this, and get back to you if I find what could be a cause of missing field. Please let me know if you figure something out.

    I want to do something similar, and I’ve sort of gotten it to work but not really. When I call the page from within a custom post type “project,” the table displays what seem to be the IDs of the custom fields. (See it at http://www.editorialpartnersllc.com/project/test/ if that’s helpful. I’ll format it later, after I figure out what I’m doing wrong.)

    The “tools” and “skills” come from the custom fields of the same name, which rely on custom taxonomy terms for their values. For instance, I’ve got under tools things like MS Word, Illustrator, and other software. When I use the shortcodes on the project post itself, these names appear. But their IDs appear here. So something is working because the IDs appear. But something is notworking. (I also tried to use the IF logic from above, and that just made me a blank page!)

    Thanks for your help. This plugin has really helped me in all sorts of ways already!

    After playing around with this a bit, I discovered something that worked, so I am sharing it in case anyone else could benefit.

    For the page I’m calling within a custom post called projects, I did this for those custom taxonomy terms (skills and tools) area on that page:

    <strong>Tools:</strong> [for each="tools" current="true"] • <a href="[url]/tools/[each slug]">[each name]</a> [/for]
    <strong>Skills:</strong>[for each="skills" current="true"] • <a href="[url]/skills/[each slug]">[each name]</a>[/for]

    This works to make the names appear as well as creates a link that will pull up an archive page for that tool or skill. (The bullets are just ways for me to separate the terms and make them look nice.)

    Using a separate page to put all the shortcode elements I want to appear on every post is really helpful–I just have to remember to add just one line of shortcode ([content type=”page” name=”{the name of the page}”] instead of a bunch.

    Plugin Author Eliot Akira

    (@miyarakira)

    Thank you for sharing your solution!

    The final code makes sense, to display custom taxonomies and their links, depending on the current project being displayed.

    That’s a good point about using a template to display something on every post. It’s always nice to find an efficient way to solve something. 🙂

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Template of multiple custom fields’ is closed to new replies.