Hide a custom input element if category - php

I'm using the Custom Field Plugin to add a custom Field and call it to the content using the_field(); in my single.php
I just want to display this field on the all the categories except the "Articles which is 13", so I was trying somethin' like this:
<?php if !is_category( '13' ); { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
But it is not working. I'm wondering how to do this.

You should use in_category, and put the conditional statement in parentheses.
<?php if (!in_category( $cat )) { ?>
<h3 class="single-product__price"><?php the_field('precio'); ?></h3>
<?php } ?>
Make sure $cat is either ID (integer), name or slug (string) of the category.
Edit: I'm assuming you actually want to display the input on posts that are not in a particular category, hence in_category. If you do indeed want to display it on all other Category archive pages, then is_category would be correct.

Related

Wordpress / PHP: check if post is in only in one category

Basically, I want to hide the category list if the author hasn't chosen a category for the post yet (so that it doesn't show 'uncategorised' on the archive page).
I need to check if the post is ONLY in the 'uncategorised' category in Wordpress.
I am checking if the post is in this category with:
<?php if (!in_category( 'uncategorised' )) { ?>
<div class="category">
<?php the_category(', '); ?>
</div>
<?php } ?>
Which works great - however any post that is in 'uncategorised' AND another category is also not appearing. So, i need to also check if that is the ONLY category it is in. So something like '&& is only in one category' is what i'm looking for, but my google searches have not come up with any solutions.
Please could someone point me in the right direction with this?
There is a built in function that could help you here:
https://codex.wordpress.org/Function_Reference/get_the_category
Example of ”only one category”:
$categories = get_the_category()
if ( count($categories) == 1 ) { ... }

Style post archive blog preview based on category

I have a page showing your typical series of small post teasers - images, excerpt etc which advertise properties for lease.
I would like to have the ability to add a 'new' or 'featured' icon based on its corresponding 'new' or 'featured' category or tag - either will do.
I have added these categories but they do not appear in the code when output and so I cannot target them.
How would I be able to perform the action:
If a post thumbnail has category of 'new' add the class 'new' so I can then target and style - repeating for each category.
I found this which I think is similar but does not work
There will be multiple categories displaying on the archive page, but I want to style only the previews that have a certain category - I do not want to style the individual post page.
Unfortunately my php skills are limited
Thanks
$post = $wp_query->post;
if ( in_category('new', $post->ID) ) { ?>
<body <?php body_class('new'); ?>>
<?php
}
elseif ( in_category('featured', $post->ID) ) { ?>
<body <?php body_class('featured'); ?>>
<?php
}
else { ?>
<body <?php body_class('class-name-generic'); ?>>
<?php
}
?>
here you go get_the_category($post->ID); will return the array of categories of that post you need to loop through the array
$category_detail=get_the_category('4');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}

Advanced Custom Fields after the main content

I am using a ACF select field to enable the page admin to select a category of posts to display under the page content. For example, if he selects "Televisions" in the ACF select field, all posts in that category will display after the page content. Here is the code for that bottom part of the page (after the main content), from the page template:
<h2>Learn more about <?php the_field('main_page'); ?></h2>
<ul>
<?php
$main_page=get_field('main_page');
$related_systems = new WP_Query( 'category_name='.$main_page );
while( $related_systems->have_posts() ) : $related_systems->the_post();
if($post->post_type == 'post'):
?>
<li><?php the_title();?></li>
<?php
endif;
endwhile; ?>
</ul>
Here is the ACF settings screenshot
The select field shows up fine on the admin side in all four pages that have the Main four page template, but both get_field('main_page') or the_field('main_page') end up blank (I tested get_field with echo and nothing shows up). How To get the field value in the page template?
I'm using WordPress 3.8.1 and ACF Version 4.3.5
Inside the main content, add $page_id = get_the_ID();.
And in the custom loop, call the fields:
the_field( 'main_page', $page_id );
get_field( 'main_page', $page_id );
Those functions work without an ID if used inside the loop, otherwise we need to specify what post we are requesting.
Also, you can filter the post type when calling WP_Query:
WP_Query ( 'post_type=post&category_name=' . $main_page );

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.

Display Only One Category

On my WordPress theme, it displays all of the categories a post is in on the homepage for the post block, I only want to display one category even if a post is under multiple categories.
i.e. Category One, Category Two, Category Three
I want it to be Category One...
This is the code that is in place at the moment:
<h2><?php the_category(', ') ?></h2>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
What do you mean by first category? The following code will show only the one category, but it will sort the categories by name.
<?php wp_list_categories('orderby=name&show_count=1'); ?>

Categories