advanced-custom-fields the_field function not work in wordpress - wordpress

I'm trying to use the the_field(); function in wordpress but did't work
also I tried to use the get_field(); function and same problem what I can do
<?php the_field('contact_form_short_code'); ?>
I am using advanced-custom-fields plugin the free version

the_field('contact_form_short_code') will try to get the field info from the current post in the loop. If you're not currently in a loop it will look to the current page/post.
If you want to reference a post from outside the loop you must specify the post ID, eg: the_field('contact_form_short_code', $post_id)
Hope that helps

Check if your field group location points to your template, taxonomy or custom post type, after that try to use echo to call your field.
Check this link www.advancedcustomfields.com

If you want to display the shortcode (contact_form_short_code) specified in the admin panel via a custom field (ACF), you need to use the do_shortcode(); function.
In your case, the code looks like this:
<?php
//In the admin panel we fill the shortcode of the contact form, for example CF7.
//[contact-form-7 id="1" title="Form"]
$cform = get_field('contact_form_short_code', $post_id);
//Output of shortcode
echo do_shortcode($cform);?>

Related

Can we replicate ACF functionality in our theme?

Hi i want to add advance custom fields in my wordpress theme. I don't need editor functionality but to provide the users the facility to enter the values to view in the frontend.
basically i want to add the custom fields in my theme just like ACF but i don't want to use the plugin. Is there anything anyone can help me out with this please do.
I'm using ACF plugin right now to add custom fields in my theme.
For example I'm getting the designation from user in the admin panel and our team members custom post type. and showing it on the home page by using this code.
<p><span class="fa fa-user-circle"></span> Designation</strong>:<?php echo the_field( 'designation' ); ?> </p>
I don't want to use ACF plugin to perform this task. I know about the wordpress custom field. The problem with wordpress custom field is I've to select the key every time when I create a post. Here's a sample of what I'm trying to do. I want to add the this in my add new post.
Right now I've to select the key value whenever a new post is created. I want something similar to the image attached in my add new post. Thanks.
You can use native WordPress function(s) to do the same thing. Of course, you lose the extensive ACF features and slick ACF admin panels.
Instead of using ACF's get_field(), you can use get_post_meta(get_the_ID(), 'field', true);
List all items in the meta field 'appartments' like so:
$meta_name = 'appartments';
$fields = get_post_meta( $post->ID, $meta_name, true );
foreach ( $fields as $fieldValue )
{
echo $fieldValue . ' ';
}

Wordpress CPT UI and ACF custom fields

i am using CPT UI for custom post type and ACF custom fields.
i have created custom post type (companies) and linked to its custom fields. In admin everything is okay. I want to display this in front end. The user can be enter the company details also. How can i display this in front end ?
<?php
$id = get_the_ID();
$data = get_field('custom_field_name', $id);
echo $data;
?>
Obviously this will need a lot of refinement to look good but that's the basics on how to retrieve data from an ACF field. You may need to var_dump($data); in order to figure out how to get the part you want (if it's not just storing a string). Without more details on what you're doing that's a good an answer as I can give you.

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

How to get related type post id in magic field plugin

I am using magic field plugin for my project and created two related type fields and I want to get selected post/page id. Please help me I am stuck here.
Inside the loop you can say:
<?php echo get('field_name') ?>
Replace field_name with the name of your field. This should print it's contents.

How can I create a link to a specific page in wordpress?

It seems like a simple task, but I'm having difficult time finding any information on this. Basically I have an "Events" page created in wordpress to which I want to create a link on my main page. I do not want it to be static like (https://mysite.com/events). Is there a way to do it dynamically?
You want to use the get_permalink() function to write the link - http://codex.wordpress.org/Function_Reference/get_permalink
This function accepts an argument which is the post or page ID (and defaults to the ID of the current page), so if your Events page is ID #11, it would look like to following to also have the link text match the page title (should it change to "Events!" in the future, for example):
<?php $events_page = get_post(11); ?>
<?php echo $events_page->post_title; ?>

Resources