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; ?>
Related
This is my first post on stackoverflow, as I am quite new to PHP. I am learning the language to help me customize my online portfolio in Wordpress, and I usually manage to make the changes I need - but not this time, apparently.
I am trying to use get_post_meta to read a meta tag in my portfolio pages, and avoid displaying the page thumbnail. This is the code I am using:
<?php $disable_thumb = get_post_meta( get_the_ID(), 'minimal_portfolio_page_thumb', true );
if( $disable_thumb !== 'on' ): ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<?php endif; ?>
Using a meta tag management plugin, I added the following tag to all my "portfolio"-type pages:
<meta name="minimal_portfolio_page_thumb" content="on">
I am currently checking if this works in this page of my web: egozalor.com/portfolio/hansel-gretel/
Long story short, the trick does not work as expected. I guess there is something I am doing wrong or not realizing, due to my little knowledge of PHP. Any indications, tips or recommendations are very welcome!
Also please let me know if further or more specific information is necessary to assess my issue.
Thanks in advance!
The function get_post_meta has nothing to do with the <meta> elements of your site. With this function you can only get metadata of the post itself. You can set the metadata on each post at the end (custom fields) as key / value pairs.
These custom fields are not visible on the site itself. You can create a custom field on every post with key minimal_portfolio_page_thumb and value on (or another value like 0/1).
Looks like the custom fields are disabled by default on WordPress. But you can enable the custom fields without an additional plugin. On the top right of your post you can find three dots to open a menu. At the end of the menu there is the entry "Options". On the options you can enable the custom fields.
You can enable the custom fields for posts and pages.
I can hardcode the shortcode for Id in template but I read that this is not a good practice, my question is how to dinamicly pass Id of form so that one day when user needs a new form he can change it without calling a developer(me)?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo do_shortcode("[gravityform id='1' title='true' description='true']"); ?>
<?php endwhile; else : ?>
Put it in an (advanced) custom field, or simply in the content.
Use Advanced Custom Fields and create an Options page where the shortcode can be edited, examples are available here:
https://www.advancedcustomfields.com/resources/options-page/
Then in your theme you put something like the_field('contact_form, 'option'); instead of the echo do_shortcode(...); part you have now. Afterwards the user can edit the field and insert a new form.
If you're already using ACF or are open to using it, you might consider this plugin which will allow the user to select from a list of Gravity Forms:
https://github.com/stormuk/Gravity-Forms-ACF-Field
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; ?>
I'm trying to add a custom profile avatar to Wordpress. I have the field added to the profile and it saves everything fine, but I'm trying to make a conditional that if the user does not upload a custom file, it defaults to the gravatar.
I have this so far:
<?php if(get_the_author_meta('da_avatar') != ''): ?>
<p>Exists!</p>
<?php else: ?>
<p>Does not exist!</p>
<?php endif; ?>
da_avatar is the ID of the custom field within the profile. This function always yields "DOES NOT EXIST" even if the field is not blank.
Any idea on why it is not working and how I can make it so that if the custom avatar field is empty, it will display "does not exist" and if it is not empty, have it display "Exists!"
I'm guessing it's because of where you're running this code. Try adding the User ID as the second parameter. I've also updated your code slightly to make it easier to use 'da_avatar' once you've performed the check.
My suggestion would be to do the following:
<?php if ( $da_avatar = get_the_author_meta( 'da_avatar', $user_id ) ) : ?>
<p>Exists!</p>
<?php else : ?>
<p>Does not exist!</p>
<?php endif; ?>
Make sure you replace $user_id with a valid user ID. If that works let me know where you're running this code and I'll talk you through setting it up properly.
I have been working on a WP theme (based on twentyeleven) for a while that has a spotlight (featured) area for sticky posts: http://www.designated.net.au/testbed/wordpress/
The problem I am having is that the posted_on declaration does not display for the sticky post. I am a total noob when it comes to PHP so I don't really know what I'm doing.
To feature the sticky posts I used this guide: http://line25.com/tutorials/how-to-create-a-featured-post-layout-in-wordpress
But I changed the display of the post to use the code from content.php. So I am using the following:
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php twentyeleven_posted_on(); ?>
</div><!-- .entry-meta -->
<?php endif; ?>
That is the part that doesn't display.
I'm happy to supply more code if you need me to.
If you need to display the post date, use <?php echo get_the_date(); ?>
You can display the date in any format. Check this Codex for Formatting Date and Time
Have you tried using get_post_type( $post->ID ).
I figured it out. It was a problem with the CSS. For some reason it was set to absolute with zero left and top.