Could do with some help as I've tried a few things but no luck...
I have a container div that has two fields inside it (wordpress acf). I can show the fields if they have content and they don't dhow if the are empty. What I need now is to show the container div if one or both fields have content or hide the container field if both fields are empty.
Current code
<div class="header-contact">
<?php if( get_field('header_tel', 'option') ): ?>
<p>Tel No: <?php the_field('header_tel', 'option'); ?></p>
<?php endif; ?>
<?php if( get_field('header_email', 'option') ): ?>
<?php the_field('header_email', 'option'); ?>
<?php endif; ?>
</div>
Any help would be great...
Possible solution:
<?php if( get_field('header_tel', 'option') || get_field('header_email', 'option') ): ?>
<div class="header-contact">
<?php if( get_field('header_tel', 'option') ): ?>
<p>Tel No: <?php the_field('header_tel', 'option'); ?></p>
<?php endif; ?>
<?php if( get_field('header_email', 'option') ): ?>
<?php the_field('header_email', 'option'); ?>
<?php endif; ?>
</div>
<?php endif; ?>
Related
I added a new custom field to a fields group to be used only to render the homepage. When trying read and display the contents of this field with the code below, nothing happens. I am sure the way to read custom field is how it should be. I don't get any error message. Could you please help fix this? This is being done on a WordPress site.
<div class="bg-white-base relative pt-row pb-row pl pr">
<div class="wrap-x">
<div class="inside mini">
<?php if ( $headline = get_sub_field( 'slider_-_h1' ) ) : ?>
<h2 class="mb color-purple-base">
<?php echo $headline; ?>
</h2>
<?php endif; ?>
<?php if ( $subheadline = get_sub_field( 'slider_-_h2' ) ) : ?>
<h3 class="mb alt-heading h5">
<?php echo $subheadline; ?>
</h3>
<?php endif; ?>
<?php if ( $text_area = get_sub_field( 'slider-_shortcode' ) ) : ?>
<article class="body-area mt2x">
<?php echo $text_area; ?>
</article>
<?php endif; ?>
</div>
</div>
</div>
get_sub_field() is supposed to be use inside a loop. When you use a group, you may use a loop like :
while( have_rows('hero') ): the_row();
........
endwhile;
Everything is explained here : https://www.advancedcustomfields.com/resources/group/
I'm using ACF repeater and i'm trying to list only the files from Relco Components which you can see highlighted in the picture. How can i get only the specific row?
<?php if( have_rows('categorie_documentatie', 8) ): ?>
<?php while( have_rows('categorie_documentatie', 8) ): the_row();
// vars
echo $nume_categorie_doc = get_sub_field('nume_categorie_doc');
?>
<?php if( have_rows('subcategorie_doc') ): ?>
<?php while( have_rows('subcategorie_doc') ): the_row();
// vars
echo $nume_subcategorie_doc = get_sub_field('nume_subcategorie_doc');
$imagine_resursa = get_sub_field('imagine_resursa');
?>
<?php if( have_rows('fisier_doc') ): ?>
<?php while( have_rows('fisier_doc') ): the_row();
// vars
$nume_fisier_doc = get_sub_field('nume_fisier_doc');
$file = get_sub_field('file');
?>
<?php if ($nume_subcategorie_doc='Relco Components'): ?>
<a href="<?php echo $file; ?>" target="_blank">
<h5><?php echo $nume_fisier_doc; ?></h5>
</a>
<?php endif ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif; ?>
Nothing is failing in the code, i just want to get the field nume_fisier_doc just from the "Relco Components" subcategory.
My code below is checking to see if a Wordpress member is male or female, and the displaying certain code based on this. I am trying to optimise the code below to avoid having to have 2 copies of the entire code block, as it appears to me that I only need to conditionally check the first piece of ACF if code, as this is referring to the gender specific content? How can I achieve this?
The current code below is working correctly, but results in lots of duplicate code. The attempt below does not work, it appears to be getting confused with the <? endif; ?> tags?
CURRENT
<?php if ($memberGender == "male") : ?>
<section>
<?php if( have_rows('accordion_section_boys') ): ?>
<?php while( have_rows('accordion_section_boys') ): the_row(); ?>
<div class="accordion-section">
BOY SPECIFIC CONTENT
</div>
<?php endwhile; ?>
<?php endif; ?>
</section>
<?php endif; ?>
<?php if ($memberGender == "female") : ?>
<section>
<?php if( have_rows('accordion_section_boys') ): ?>
<?php while( have_rows('accordion_section_boys') ): the_row(); ?>
<div class="accordion-section">
GIRL SPECIFIC CONTENT
</div>
<?php endwhile; ?>
<?php endif; ?>
</section>
<?php endif; ?>
ATTEMPT
<section>
<?php if ($memberGender == "male") : ?>
<?php if( have_rows('accordion_section_boys') ): ?>
<?php while( have_rows('accordion_section_boys') ): the_row(); ?>
<?php endif; ?>
<?php if ($memberGender == "female") : ?>
<?php if( have_rows('accordion_section_girls') ): ?>
<?php while( have_rows('accordion_section_girls') ): the_row(); ?>
<?php endif; ?>
<div class="accordion-section">
GENDER SPECIFIC CONTENT (BOY OR GIRL)
</div>
<?php endwhile; ?>
<?php endif; ?>
</section>
<?php endif; ?>
<section>
<?php if ($memberGender == "male") : ?>
<?php $val = 'accordion_section_boys';?>
<?php endif; ?>
<?php if ($memberGender == "female") : ?>
<?php $val = 'accordion_section_girls';?>
<?php endif; ?>
<?php if( have_rows($val) ): ?>
<?php while( have_rows($val) ): the_row(); ?>
<div class="accordion-section">
BOY SPECIFIC CONTENT
</div>
<?php endwhile; ?>
<?php endif; ?>
<section>
I would suggest something like:
<?php
$genders = array(
'male' => 'accordion_section_boys',
'female' => 'accordion_section_girls',
);
foreach ( $genders as $gender => $rows_id ) {
while( have_rows( $rows_id ) ) {
// Here use a template to print the content, by name them like "template-male" and "template-female"
include 'template-' . $gender . '.php';
}
}
?>
If you notice the code, I told you to use a template to show the HTML, so you can call them dynamically and the content will be:
<section>
<div class="accordion-section">
CONTENT
</div>
</section>
Because they have the same structure you can do something like this:
<?php
if ($memberGender == 'male' || $memberGender == 'female'){
$indicator = ($memberGender == 'male')? 'boys' : 'girls';
if( have_rows('accordion_section_'.$indicator) ){
while( have_rows('accordion_section_'.$indicator) ){
the_row();
}
}
}
?>
To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});
I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks