Multiple PHP if statements - php

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();
}
}
}
?>

Related

only displays certain category

I'm building my own website just for fun and for learning.
I want to display only the post with a certain category # my homepage.
I've tried a number of things but they don't seem to work
at my home.php
this is what te code looks like.
get_header(); ?>
<?php $blog_layout = get_theme_mod('blog_layout', 'small-images'); ?>
<?php if ( ($blog_layout == 'masonry') || ($blog_layout == 'fullwidth') ) {
$layout = 'fullwidth';
} else {
$layout = '';
} ?>
<?php if ( $blog_layout == 'masonry' ) {
$masonry = 'home-masonry';
} else {
$masonry = '';
} ?>
<div id="primary" class="content-area <?php echo $layout; ?>">
<main id="main" class="site-main" role="main">
<span class="diensten">ONZE DIENSTEN..</span>
<div class="diensten-content">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<div class="home-wrapper <?php echo $masonry; ?>">
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ( $blog_layout == 'large-images' || $blog_layout == 'masonry' ) {
get_template_part( 'content', 'large' );
} else {
get_template_part( 'content', get_post_format() );
}
?>
<?php endwhile; ?>
</div>
<?php moesia_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!--diensten-content-->
<
</main><!-- #main -->
</div><!-- #primary -->
And at content.php
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?>>
<?php if ( has_post_thumbnail() ) : ?>
<div class="entry-thumb">
<a href="<?php the_permalink(); ?>" class="H1-posts" title="<?php the_title(); ?>" >
<?php the_post_thumbnail('moesia-thumb'); ?>
</a>
</div>
<?php endif; ?>
<?php if (has_post_thumbnail()) : ?>
<?php $has_thumb = ""; ?>
<?php else : ?>
<?php $has_thumb = ""; ?>
<?php endif; ?>
<div class="post-content <?php echo $has_thumb; ?>">
<header class="entry-header">
<?php the_title( sprintf( '<h1 class="entry-title">', esc_url( get_permalink() ) ), '</h1>' ); ?>
<?php if ( 'post' == get_post_type() ) : ?>
<?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
<?php if ( (get_theme_mod('full_content') == 1) && is_home() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
</div><!-- .entry-content -->
I've tried this..`
<?php
//refine your query to the category you desire either a slug(example below) or category id
$args = array(
'category_name' => 'my_category_slug',
);
//create the query using the arguments
$query = new WP_Query($args);
?>
//create the loop to show the posts
<?php if($query->have_posts()): ?>
<?php while($query->have_posts()): $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
did not work. How do I fix this?`If It displays only the posts with a certain class if also good.
Thanks..

Screaming frog sees a link to a page which is not there but shows

This is the selector script I believe below so what should happen is as you select a product then you select if its your an individual or couple or family then you click show me and it takes you to the correct link
<div class="finder__selector">
<?php /* Set first option as the default option */ ?>
<?php $count = 1; while ( have_rows('links') ) : the_row(); ?>
<?php $link_url = get_sub_field('link_url'); ?>
<?php if($product_name): ?>
<?php //echo $product_name; ?>
<?php //echo $link_url; ?>
<?php if(strpos($link_url, $product_name) !== false): ?>
<?php the_sub_field('link_title'); ?><i class="down-arrow"></i>
<?php endif; ?>
<?php else: ?>
<?php if($count == 1) : ?>
<?php the_sub_field('link_title'); ?><i class="down-arrow"></i>
<?php endif; ?>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>
<div class="finder__options">
<?php $count = 1; ?>
<?php while ( have_rows('links') ) : the_row(); ?>
<?php $link_url_select = get_sub_field('link_url'); ?>
<?php $link_title_select = get_sub_field('link_title'); ?>
<?php if($product_name): ?>
<?php if(strpos($link_url_select, $product_name) !== false): ?>
<?php echo $link_title_select; ?><i class="down-arrow"></i>
<?php else: ?>
<?php echo $link_title_select; ?><i class="down-arrow"></i>
<?php endif; ?>
<?php else: ?>
<?php echo $link_title_select; ?><i class="down-arrow"></i>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>
</div>
So the above works but whats its actually showing on view source and what creaming frog is 404 is
href="&audience=couples" class=finder__option>
my partner and I <i
class=down-arrow></i>
</a>
<a
href="&audience=families" class=finder__option>
my family <i
class=down-arrow></i>

WP Hide parent container when both child fields (acf fields) are empty

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; ?>

ACF repeater get specific row by name

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.

how to display odd number post in left side even number post in right side?

<?php $counter = 3; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('3') ): ?>
<?php else: ?>
<?php endif; ?>
<?php if($counter%2 == 0){echo 'floatRight';} else { echo 'floatLeft'; } ?>
<?php the_ID(); ?>
<h1 > <?php the_title(); ?></h1>
<?php the_post_thumbnail('full'); ?>
<?php the_content(__('(more...)')); ?>
<?php comments_template(); // Get wp-comments.php template ?>
<?php if($counter%2 == 0){ echo "<div class='clear'></div>";} ?>
<?php $counter++; ?>
<?php endwhile; else: ?>
<?php endif; ?>
i am trying to display in this way and i want to display particular category of post
post1
post2
post3
post4
please give me the solution ...
use the below code.
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
<?php $i = 0; rewind_posts(); ?>
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
You can use this css3 property to do it (This would take care odd and even elements, you don't have to write down the loop explicitly):
p:nth-child(odd) //you can do the same for div
{
float:left;
}
p:nth-child(even)
{
float:right;
}
You can use query_post() or Wp_query() in wordpress .
Use this code. to get the particular categories post .
<?php
// The Query
query_posts( 'cat=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>

Categories