Wordpress custom field - Hide blank field - php

I am using a custom field plugin for Wordpress so that users of the CMS can keep post items organized. I setup the fields and in my loop I display the like this..
<p><strong>Business Phone:</strong> <?php the_field('business_phone'); ?></p>
How would I hide or change the color of the title if there is no information entered into the field? I've gotten this far. Am I on the right track?
<?php
if ( get_group('business_phone',TRUE) ) {
echo 'I see a phone number!!';
} else {
echo 'I don\'t see a phone number!!';
}
?>
ok.. I'll check out the Wordpress forum.

Related

How can I add to shortcodes?

I have plugin A that needs to verify two pieces of shortcodes before an action takes place within another plugin.
<?php
function mepr_add_some_tabs($user) {
?>
<span class="mepr-nav-item prem-support <?php MeprAccountHelper::active_nav('member-profile'); ?>">
<!-- KEEPS THE USER ON THE ACCOUNT PAGE -->
Profile
</span>
<?php
}
add_action('mepr_account_nav', 'mepr_add_some_tabs');
function mepr_add_tabs_content($action) {
//Listens for the "member-profile" action on the account page, before rendering the FF view shortcode.
if($action == 'member-profile') {
echo do_shortcode('[frm-condition source=frm-stats id=80 type=count user_id=current greater_than=0] [display-frm-data id=5728] [/frm-condition]');
}
}
add_action('mepr_account_nav_content', 'mepr_add_tabs_content');
The problem is I'm not a developer and I don't know how to add the second shortcode in the echo do_shortcode function.
The current shortcode basically says: if the user has an entry on the field ID: 80, then show them the content of the entry.
What I need to add is another, second shortcode which will say almost the same thing, but about another field ID.
How can I add it while following this requirement?

How do I add a php if condition to this wordpress code?

I have a modified code to pull the wordpress custom post fields, because I want to be able to use a shortcode to display a form inside the custom post fields. So the following code works well. Now, I just need to figure out how I can display something else if that custom post field is empty...
This is my code that displays my custom post field info:
<?php echo apply_filters('the_content', get_post_meta($post->ID, 'form', true)); ?>
And if it's empty I want to display something else... (my custom post field key is called 'form')
How do I do that?
I tried to search for it but my code looks a bit different since I need to be able to add shortcodes to the custom fields.
Just use and if/else statement.
<?php
$result = apply_filters('the_content', get_post_meta($post->ID, 'form', true));
if(empty($result)) {
echo 'something else';
} else {
echo $result;
}
?>

Using Advanced Custom Fields and Contact Form 7 to display a form

I want my users to be able to put a Contact Form 7 shortcode into a custom field in the Wordpress editor. I've created the custom field using ACF and I can pull the value onto the page, but when I try to include it in the shortcode, it comes back with a 404.
This code:
<?php echo do_shortcode(get_field('contact_form_shortcode')); ?>
Returns:
[contact-form-7 404 "Not Found"]
If I create a variable out of the value like this:
<?php
$formCode = get_field('contact_form_shortcode');
echo $formCode;
?>
The echo returns:
[contact-form-7 id="473" title="Learn More Form"]
But I get the same 404 after putting that value into the echo do_shortcode function list this:
<?php echo do_shortcode($formCode); ?>
What am I missing?
To do it With ACF pro plugin and without other extra plugins.
Create a relation field ( example: contact_form )
add the below code into your page loop:
<?php $posts = get_field('contact_form');
if( $posts ):
foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT)
$cf7_id= $p->ID;
echo do_shortcode( '[contact-form-7 id="'.$cf7_id.'" ]' );
endforeach;
endif; ?>
I was able to resolve this issue by using the technique I discussed in my comment above. By using the WYSWIG field set to 'Run filter "the_content"' I'm able to pull the field value in the way I want it. The only drawback is that users could type something else in there besides a form shortcode.
Here's my final code:
<?php
if (get_field('contact_form_shortcode')):
echo get_field('contact_form_shortcode');
else:
echo do_shortcode('[contact-form-7 id="473" title="Learn More Form"]');
endif;
?>
Instead of using a WYSIWYG field, you can simply set a Text field to "Convert HTML into tags" under the Formatting setting on the field. This will stop the 404 errors from CF7 and properly process the form. The WYSIWYG fields tend to be too hard to control from bad user input.
Here's my solution:
Create a "Post Object" type Field which will returns the post ID.
the Filter by Post Type for this field should be sett to "Contact Form" and Filter by Taxonomy leave empty.
Then in page template: (php)
<?php $contact_form = get_field('contact_form'); ?>
HTML:
<div class="col-12 col-md-8 offset-md-2">
<?php echo do_shortcode('[contact-form-7 id="'. $contact_form .'"]'); ?>
</div>
acf field screenshot
Another Solution :
1) Install https://github.com/taylormsj/acf-cf7
Installation
> 1 Copy the acf-cf7 folder into your wp-content/plugins folder
> 2 Activate the Contact Form 7 plugin via the plugins admin page
> 3 Create a new field via ACF and select the Contact Form 7 type
> 4 Please refer to the description for more info regarding the field type settings
2) Insert following code into template :
<?php if( get_field('field_acf') ):
the_field('field_acf');
endif; ?>
This applies to create a contact form 7 in a modal or Pop Up, multiple forms on the same page.
To paste CF7 shortcode (or any other generated shortcode) into ACF field as you were asking, just use the #armadadrive solution, which is exactly what question was about and it works.
create ACF text field with some name, eg hero_form
paste your shortcode into it
display contents like so <?php echo do_shortcode(get_field('hero_form')); ?>
Thanks #armadadrive
Here you can try please check the code for the shortcode below:
<?php echo do_shortcode(get_field('YourACFfieldNameHere')); ?>
I tried it on my website and it works.

Whats the url that displays all posts in wordpress?

I simply want a link to my blog archives. They are just normal posts, but I cannot seem to find the right url for them to add to my menu.
right url for them to add to my menu.
http://yourwebsite.com/?post_type=post
You don't have to necessarily use a category to every post.
Actually the file to list all posts of any category and date is the index.php. You just write 'the loop' as told on codex.
So if you changed your index.php to make it as a fancy page and not the post list only, now you're trying to create another page to do that for you.
So, if you follow me, you're doing this the wrong way. What you should do is to create a separate page and assign it as the home page of your blog. It would then free the index.php file for you to use it as the blog list, as it is by default.
Assuming that you did it the correct way (as mentioned by Guilherme), you should have a page designated as the blog list page.
The URL for the blog list if using the page 'My Blog' to display posts and pretty links for the url should be something like http://mywebsite.com/my-blog.
<?php if ( is_front_page() && is_home() ) {
// Default homepage
echo "Default homepage";
} elseif ( is_front_page()){
echo "Static homepage";
// Static homepage
} elseif ( is_home()){
echo "Blog page";
// Blog page
} elseif ( is_page( 'cart' ) || is_cart()){
echo "cart";
// Blog page
} elseif (is_single()){
echo "is_single";
// Blog page
} elseif (is_product_category()){
echo "is_product_category";
}
else {
echo "Everything else";
// Everything else
} ?>

Keep a customer Anonymous - Magento

I have created a module in Magento which takes certain views from customers about any product and displays them as extra information under each product when its approved by the admin from back end.
Now I have got three fields Name, Email N Views. I would like to know how to keep a customer anonymous in this form if he wishes not to display his name when his views are approved and posted on the product page.
Thanks.
I found a solution by stumbling upon original core files. I added a checkbox in the form on the front end.
<li class="control"><input type="checkbox" name="is_anonymous" id="anonymous" value="1" title="<?php echo $this->__('Is Anonymous') ?>"<?php if($this->getIsAnonymous()): ?> checked="checked"<?php endif; ?> class="checkbox" /><label for="anonymous"><?php echo $this->__('Is Anonymous') ?></label></li>
I added the is_anonymous column in the table and in the core controller file I added this to save the preference of each user
if($post = $this->getRequest()->getParam('is_anonymous'))
{ $record->setIsAnonymous(1);
else
{ $record->setIsAnonymous(0); }
And then to display it on front end I added an extra condition in list.phtml
<small class="by"><?php echo $this->__('By: '); ?><?php if($entry->getIsAnonymous()==1) {echo 'Anonymous';} else { echo $entry->getName();} ?></small>
And this lets me have a check box which if ticked displays the post as 'Anonymous' by user who has posted or else displays his name on the front end.
Hope this helps some one!

Categories