wordpress if else syntax for multiple slugs? - php

I'm currently adding this to the header.php page to add a script to a single wordpress page:
<?php if( $post->post_name == "my-page" ) { ?>
//script here
<?php } ?>
Works great. how do I add another page to this? I tried:
<?php if( $post->post_name == array("my-page", "my-page-two") ) { ?>
//script here
<?php } ?>
but it didn't work.

Related

is_page() function not displaying content to specific page ID's

Im trying to simply just output some HTML onto these 3 specific page-ids that I am referring too in the code below. Does anyone have any ideas as to why this php isn't working? (its at the bottom of my functions.php file in the child theme.
if( is_page('96') || is_page('98') || is_page('61') ) { ?>
<div class="test"><h2>test content</h2></div>
<?php }
Any help would be appreciated as to why this 'test content' is not appearing on these pages.
Thanks!
Try the below code that will work properly.
if( is_page(96) || is_page(98) || is_page(61) ) : ?>
<div class="test"><h2>test content</h2></div>
<?php endif;
===================== or ==========================
$id = get_the_ID();
if( ($id == 96) || ($id == 98) || ($id == 61) ) : ?>
<div class="test"><h2>test content</h2></div>
<?php endif;
You can use
get_the_ID(); or $post->ID;. you will get the ID of the current post of the loop
like this
$ids = [96, 78];
if(in_array(get_the_ID(), $ids)) echo '<div class="test"><h2>test content</h2></div>';
Please take a look at the docs :
For is_page function
To get the ID from the loop

If else statement not working

I am using Meta Box Plugin
I have two options in Post Edit. One to add a Logo and one to add a Title. On the front end (in my template) I want to show either the Logo or the Title, but not both.
I added this code to my template however it is not working:
<?php if( rwmb_meta('restauranttheme_hero_small_logo', false, 'url') !== '' ) { ?>
<?php $images = rwmb_meta( 'restauranttheme_hero_small_logo', 'type=image_advanced&size=full' );
foreach ( $images as $image ) {
echo "<img src='{$image['url']}'>"; } ?>
<?php } else { ?>
<h1 class="mini-title"><?php echo rwmb_meta('restauranttheme_hero_title' ); ?></h1>
<?php } ?>
I also tried:
<?php if( '' != rwmb_meta('restauranttheme_hero_small_logo' ) ) { ?>
<?php $images = rwmb_meta( 'restauranttheme_hero_small_logo', 'type=image_advanced&size=full' );
foreach ( $images as $image ) {
echo "<img src='{$image['url']}'>"; } ?>
<?php } else { ?>
<h1 class="mini-logo"><?php echo rwmb_meta('restauranttheme_hero_title' ); ?></h1>
<?php } ?>
I guess the error is in my if else statement.
Can anyone help?
Thanks so much

Different template for second page of posts Wordpress

I'm trying to have a different template for the second page of posts, here's the code I came up with.
<?php
if ( is_home() ) { ?>
SOME HTML
<?php } else if (is_page('chi-sono')){ ?>
SOME HTML
<?php } else if (is_paged()){} else{}?>
It seems straightforward to me but the elseif is_paged() doesn't work, what am I doing wrong? I also tried
$paged = (get_query_var('paged'));
elseif ($paged >= 2)
is_home() return true on blog page so you are not going on is_paged().
try this
if (is_home()) {
if (is_paged()) {
echo 'second page';
} else {
echo 'home page';
}
}

Advanced Custom Fields - If/Else Statement with true/false checkbox

I have added a true/false checkbox using Advanced Custom Fields for Wordpress. I want to be able to select an option which amends the page template.
I am adding this option to the Product Category in WooCommerce / Wordpress. I have included this bit of logic in the code.
I have the following code but it does not work. I suspect it is because it is not within the loop. However the code I want to insert includes the loop. Any ideas/guidance on the code is much appreciated
<?php if (is_product_category() && get_field('field_name') == true) { ?>
<div class="custom-sidebar-right">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php } elseif (is_product_category() && get_field('field_name') == false ) { // Added close brace
<div> Empty Test </div>
}
Ok, I re-read the docs for ACF and found the following (http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/)
So I applied with some logic and now it works. Thanks for var_dump pointer as that helped me fix this.
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$is_field_name = get_field('field_name', $taxonomy . '_' . $term_id);
if (is_product_category() && $is_field_name == false) { ?>
missing bracket in the if statement ,
change
if (is_product_category() && get_field('field_name') == true)
with
if (is_product_category() && get_field('field_name') == true) )

Compare two php variables in Wordpress

I'm struggling to compare two php variables to display or not display some text depending if the variables match or not. This is what I have:
<?php $link = the_permalink();?>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<?php while(the_repeater_field('featured_companies')): ?>
<?php $company = the_sub_field('featured_company'); ?>
<?php if ($link == $company) { ?>
show news articles
<?php } else { ?>
don't show news articles
<?php } ?>
<?php endwhile; ?>
<?php endwhile; // end of the loop. ?>
I want to compare $link and $company and if they match then do the stuff within the if. Where am I going wrong?
I'm using the http://www.advancedcustomfields.com plugin in Wordpress if that helps.
UPDATE:
Firstly forgot to mention that the two variables are urls. At the moment it's echoing out 2 urls that are the same on the page I want, but it's also echoing out "show news articles" when the 2 urls don't match.
Underneath the twitter profile - http://www.mediwales.com/v3/members/mediwales/ shows the same two urls. But when you goto this page http://www.mediwales.com/v3/members/3m/ it shows two different urls yet shows "show news articles".
You have to be careful with Wordpress's native functions:
the_permalink() echoes out the permalink (see documentation examples)
get_permalink() returns it as a variable (see documentation examples)
So you need to be using:
$title = get_permalink();
Just solved it:
<h2>Latest News</h2>
<?php $link = get_the_title(); ?>
<?php $portfolioloop = new WP_Query( array( 'post_type' => 'news' ) ); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<?php $post_link = get_post_permalink(); ?>
<?php if (get_field('featured_companies') != "") { ?>
<?php foreach(get_field('featured_companies') as $post): ?>
<?php $company = get_the_title($post_object->ID); ?>
<?php if ($company == $link) { ?>
News item 1
<?php } ?>
<?php endforeach;?>
<?php } ?>
<?php endwhile; ?>

Categories