I'm using the Advanced Custom Fields plugin for WP which I really like, because it gives me the ability to add a whole bunch of custom meta boxes to individual pages within my site, and implementation within page templates is very straightforward. This is generally how I would use it with a template:
<?php if( get_field('FIELD-NAME-HERE') ): ?>
<h6><?php the_field('FIELD-NAME-HERE'); ?></h6>
<?php endif; ?>
In this case, I have a field for my Contact Form 7 Shortcode, and I've turned formatting off for this field, so it should return exactly what is entered (ie. [contact-form-7 title="Contact Form"] ).
I know that I can use the do_shortcode(); to pull this shortcode into my page template, but I want to be able to populate that do_shortcode(); with the_field(); from above.
I've tried this:
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode("<?php the_field('form_shortcode'); ?>");?>
<?php endif; ?>
And this:
<?php
var formCode = the_field('form_shortcode');
echo do_shortcode(formCode);
?>
I'm still pretty new to PHP, but I feel like I'm not too far off. Any help would be greatly appreciated!
You're not far off. You need to use get_field() rather than the_field(): the_field() actually echoes out the content, so that's why it's not working as currently using it. It's the equivalent of trying to echo it twice.
Another thing to look out for is that you need to include the square brackets when using do_shortcode() but since your code already includes them you don't need to worry about it in this instance.
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode( get_field('form_shortcode') );?>
<?php endif; ?>
Related
Please bear with me on this question. I understand that it may not be as clear but and suggestions are helpful.
So I created a template page and within that template page I have two tags.
One field is the default:
<?php the_content(); ?>
and the other one is
<?php the_field('test_advertisement_two', 25017); ?>
that I created using ACF (Advanced Custom Fields) which includes the Post id. How can I include the post id in the <?php the_content(); ?> field?
I think I maybe confused how <?php the_content(); ?> works. Is there a way to assign a name to it a name to it?
This is what I am doing:
I am calling the template page (test-template.php) that contains:
<?php the_content(); ?>
<?php the_field('test_advertisement_two', 25017); ?>
from a different location by using
The post content in <?php the_field('test_advertisement_two', 25017); ?> displays but the content from <?php the_content(); ?> does not display. That's why I figure that I either need to include a post Id in <?php the_content(); ?>
The post content in displays but the content from does not display. That's why I figure that I either need to include a post Id in
Essentially.
the_content only functions right within The Loop, one of the most important concepts in WordPress. The the_field call is working because it's being explicitly passed a post ID in the optional second parameter.
You'll need to access it like this:
$page = get_post(25017);
echo $page->post_content;
(or get_page if you're on a page instead of a post)
Hi if you need post content using post id then below is the code for that.
echo get_post_field('post_content', $post_id);
I have to use some WordPress short-codes for a project - but I want them in very specific places, and not in the main content. I need the client to just put them in specific Custom fields. Using ACF, I built a text field specifically for this.
// normal in-code use
<?php echo do_shortcode('[example_shortcode]'); ?>
// ACF field
<?php the_field('my_main_shortcode'); ?>
// ACF field in the do_shorcode
<?php echo do_shortcode( the_field('my_main_shortcode') ); ?>
This renders out [example_shortcode] on the page instead of what I would expect.
I'm guessing this is a standard PHP thing - and I don't know how to escape or concatenate it properly. EDIT - below using get_fields gets me closer...
THEN... with a more complex short-code
<?php echo do_shortcode( '[tf_listview imagesize="medium1" exclude="calendar-link,list-view-title" dateformat="D n.j"]' ); ?>
It breaks somethings like the image by adding "" - which could totally be the way the short-code was written / but basically - I haven't found a solution to using a short-code in a custom field like this.
Bottom line,
I'd like to write: <?php shit_out('my_annoying_shortcode'); ?>
Or something as close to that as possible. AND have it actually work.
Can anyone give me some more direction?
Since you're using echo, you need to use get_field(), which returns the custom meta value:
<?php echo do_shortcode( get_field('my_main_shortcode') ); ?>
I am creating a WordPress plugin where a user can specify a custom loop repeater based on the value of a textarea.
The idea is, a user can use a predefined repeater that ships with the plugin or they can build their own repeater by simply adding html + PHP to the textarea.
Example of a custom repeater:
<h1><?php the_title(); ?></h1> <?php the_excerpt(); ?> - <?php the_time(); ?>
The issue is when I echo/print the textarea value, the WordPress functions within the <?php ?> tags do not execute.
Is what I'm attempting even possible?
Let me explain further...
I am building an installable plugin based on my Ajax Load More script.
This plugin will be only used by site admins and for total control of the display of posts I want to allow them to create a custom repeater.
On the plugin settings page, I want to have a textarea where the admins can add whatever html/php code they want in order to customize the repeater.
My issue is how do I execute the PHP entered within the textarea on the frontend as echoing the value does not work.
Use eval function to change plain text to php code. More about it here
eval('echo "Hello world!"');
will print out "Hello world!" instead of 'echo "Hello world!"'
BUT what if i write to textarea:
die();
One way of doing this is to use the eval() function as Justinas says, but I would strongly recommend you don't use it as I have found it to be unreliable and problematic.
What I would recommend is to write the PHP code in the string you extract from the text area to a file and then include that file where you want the PHP to execute.
So, I would recommend running
<?php file_put_contents($file_path, $contents); ?>
whenever the text area is updated to update the file, then
<?php include_once($file_path); ?>
where you want the code on the front end.
Those wordpress functions will not work unless they are in the Loop, Here is a basic Loop:
<?php if(have_posts()) { ?>
<?php while(have_posts()) { ?>
<?php the_post(); ?>
<?php // custom post content code for title, excerpt and featured image ?>
<?php } // end while ?>
<?php } // end if ?>
I'm wanting to display a WordPress nav that has the first few words of each page below it.
This is what I currently have:
<?php wp_list_pages('title_li=&link_after=<span>FIRST FEW WORDS HERE</span>'); ?>
I want the output to be like:
<li>Home<span>Welcome to the website</span></li>
Any help will be appreciated
The only thing I could think of is doing this:
use wp_get_pages to get all the titles (or the ones you want), and then access the database with a query that will get each post using the title. So, for each title from the array the get_pages returned, you get a corresponding post. In either the SQL or the PHP use the substring method to get the first few words, and display however you want.
That's just the logic, I'm a bit rusty with code so I don't want to confuse you. I'll try if you don't get it...
I don't think there's a way to put extra text inside the anchor by just passing params to wp_list_pages. You could however filter it and use preg_replace. For example...
add_filter('wp_list_pages', function($data) {
return preg_replace('/(<a[^>]+[^<]+)<\/a>/si', '\\1<span>FIRST FEW WORDS HERE</span></a>', $data);
});
Though I used preg_replace() you could also (and probably should) use DomDocument but this will work for most use cases.
Another way to do it would be to use a custom walker
Another option is to make a custom loop and add it in the "index.php" file in your theme where you want to display it.
A quick example of how that would look:
<div id="nav"><ul>
<!-- start the loop-->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="nav_post">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<span><?php formatPreview(the_content()); ?></span></a>
</li>
<!-- stop the loop-->
<?php endwhile; else: ?>
<p>No posts!</p>
<?php endif; ?>
</ul></div>
the_permalink(), the_title(), and the_content() are the key players here to populate your custom nav with the post information. Then just add appropriate css for the nav ID, nav_post class, and h2/p and you're good to go! You'll probably want to hard stop the loop after a certain number of posts - I doubt you want every post listed in the nav!
You'll have to make the "formatPreview()" function separately to control how to format the preview of each post's content - but it shouldn't be hard to just substr the first few words!
This is my first question on Stack Overflow so bear with me.
I have a WordPress site with the Advanced Custom Fields plugin installed. The site has a custom post type that has various ACF custom fields attached to it.
Not all of the custom fields are required, also some of the custom fields are grouped into their own tabular structure.
I need to check if either of 2 custom fields have content, and if they do display the table.
My PHP is fairly limited but I did some research and it should work, but its not.
The code is as follows:
<?php if (get_field( 'meeting_documents_agendas' && 'meeting_documents_minutes')) : ?>
<?php if ( get_field( 'meeting_documents_agendas' ) ) : ?>
Download file
<? endif ?>
<?php if ( get_field( 'meeting_documents_minutes' ) ) : ?>
Download file
<? endif ?>
<? endif ?>
Basically nothing is displaying, even if I have content in those custom fields.
Is the code right? Could it be a WordPress bug?
Try
<? if (get_field('meeting_documents_agendas') || get_field('meeting_documents_minutes')): ?>
This means: if one or both of the fields are set, display the table.
Don't use && because this means that both fields must be set. Use get_field() twice because you want to logically link the results of get_fields(), not the parameters themselves.
This was helpful for me as well. Though my issue was a bit different since I was using sub_fields. I needed the code to check if both sub_fields had data.
Here's my code.
<?php if (get_sub_field( 'additional_link_url' ) && get_sub_field('additional_link_text') ) : ?>
<br /><a target="_blank" href="<?php echo get_sub_field('additional_link_url')['url']; ?>"><?php echo get_sub_field('additional_link_text'); ?></a>
<?php endif; ?>