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
Related
I really need some help, My problem is that I cannot show the label from a group field using ACF
Script below is displaying the name and value, I need the "Label" to be displayed and its "value" and I can't find anything.
if( have_rows('product_specifications') ):
while( have_rows('product_specifications') ): the_row();
$subfields = get_field('product_specifications');
if( $subfields ) { ?>
<ul>
<?php
foreach ($subfields as $spec => $value) {
if ( !empty($value) ) { ?>
<li><?php echo $spec; ?> : <?php echo $value; ?></li>
<?php }
} ?>
</ul>
<?php }
endwhile;
endif;
Here is my current output:
lamp_type : E27
wattage : 1x 60W Max
globe_included : 1
colour_cord : Clear
when It should be:
Lamp Type : E27
Wattage : 1x 60W Max
Globe : 1
Colour Cord : Clear
Please anyone help me...
Use get_row() to get the sub fields:
$subfields = get_row();
And use get_sub_field_object() to get the sub field object:
$field = get_sub_field_object( $key );
So, try this: (no re-indentation so that you can easily compare with your code)
if( have_rows('product_specifications') ):
while( have_rows('product_specifications') ): the_row();
if( $subfields = get_row() ) { ?>
<ul>
<?php
foreach ($subfields as $key => $value) {
if ( !empty($value) ) { $field = get_sub_field_object( $key ); ?>
<li><?php echo $field['label']; ?> : <?php echo $value; ?></li>
<?php }
} ?>
</ul>
<?php }
endwhile;
endif;
What you are looking forward within the foreach loop is using the get_field_object() function.
Within here, you can get the label and value of any field.
For examples / uses of the get_field_object(), take a look at https://www.advancedcustomfields.com/resources/get_field_object/.
So, for example, you'll have:
$field = get_field_object($spec);
echo $field['label'] . ': ' . $field['value'];
Hope this helps.
Hello all, I'm currently working on a foreach loop that pulls images from a gallery from the backend of Wordpress. The gallery is working fine and as expected, however, I would like to prevent duplicate photos from appearing.
<?php if () $query->have_posts() ):
$thumbs = [];
?>
<?php
foreach( $query->posts as $gallery ):
$images = get_field('gallery_images', $gallery->ID);
if( $images ):
foreach( $images as $image ):
$cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
$img_id[] = $image['ID'];
$thumbs[] = $cropped_img;
?>
Unrelated HTML to the Problem
<?php endforeach; endif; endforeach; ?>
<?php foreach($thumbs as $thumbnail): ?>
<div class="gallery__thumb"><img src="<?= $thumbnail; ?>"></div>
<?php endforeach; ?>
Here is essentially what I'm trying to work out. I've thought to add two variables for the image ID so that they aren't duplicated in the first block of code. So that first if/foreach state looks like:
<?php if( $images ):
foreach( $images as $image ):
$cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
$img_id[] = $image['ID'];
$do_not_duplicate[] = $image['ID'];
$thumbs[] = $cropped_img;
?>
Followed by the followed foreach for the thumbnails
<?php foreach($thumbs as $thumbnail): ?>
<?php foreach($img_id as $img): ?>
<?php if ($img == $do_not_duplicate) : ?>
//do nothing
<?php else : ?>
<div class="gallery__thumb"><img src="<?= $thumbnail; ?>"></div>
<?php endif; ?>
<?php endforeach; endforeach; ?>
The problem I'm experiencing with this is I'm getting 22 images outputed 23 times because there are 22 images being pulled(with 1 duplicate). Essentially I'm trying to accomplish 22 images with no duplicates.
Any insight would be greatly appreciated, thank you in advance.
Simply edit your php code like that:
$thumbs[$image['ID']] = $cropped_img;
It will be an associative array in which the images are stored with ids as keys, so if an doubled image is pushed to array, it will just override the old one
Based on your code, it should be:
<?php if( $images ):
foreach( $images as $image ):
$cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
$img_id[] = $image['ID'];
$thumbs['url'][] = $cropped_img;
$thumbs['id'][] = $image['ID']
?>
then
<?php foreach($thumbs as $thumbnail): ?>
<?php if (!in_array($thumbnail['do_not_duplicate'], $img_id)): ?>
<div class="gallery__thumb"><img src="<?= $thumbnail['url']; ?>"></div>
<?php endif; ?>
<?php endforeach; ?>
You could check if the image id already exists in the $img_id array before adding it.
I modified your code below
<?php if () $query->have_posts() ):
$thumbs = [];
?>
<?php
foreach( $query->posts as $gallery ):
$images = get_field('gallery_images', $gallery->ID);
if( $images ):
// Initialize $img_id[]
$img_id = [];
foreach( $images as $image ):
// Only add to the $img_id array and $thumbs array if
// $image['ID'] is not in $img_id array
if( ! in_array($image['ID'], $img_id)):
$cropped_img = aq_resize( $image['url'], 1024, 576, true, true, true );
$img_id[] = $image['ID'];
$thumbs[] = $cropped_img;
?>
Hope this helps!
Depending on what your $image['ID'] values are, you may need to pass use the third argument of in_array() to do strict checking.
Reference: http://php.net/manual/en/function.in-array.php
Trying to add styling code for background image using WordPress wp_head hook but it doesn't work. Here is the code: (when code is used inside f1 function I get "error" message but outside f1 function code works fine and gets url for background image correctly, so problem is with wp_head. Any thoughts?
function f1() {
?> <style>
.boxes{
background-image:
url(
<?php
$images = rwmb_meta( 'f3' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo $image['url'];
}
}
else {
echo 'error';
}
?>
);
}</style>
<?php
}
add_action( 'wp_head', 'f1');
Try passing in the post_id to your rwmb_meta() call
function f1() { ?>
<style>
.boxes{
background-image:
url(
<?php
global $post;
$images = rwmb_meta( 'f3', array(), $post->ID );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo $image['url'];
}
}
else {
echo 'error';
}
?>
);
}</style>
<?php
}
add_action( 'wp_head', 'f1');
I'm using the WP post formats in my theme, and I want to echo parts of the previous posts in my single-post page. Echoing it works fine, however I only want previous posts from a specific post-format to show. I tried using an if statement with the has_post_format() function, but it wouldn't work:
php:
$prev_post = get_previous_post();
$prev_post_id = $prev_post->ID;
$prev_post_title = $prev_post->post_title;
function prev_post_item($object) {
if (!has_post_format('gallery', $prev_post_id)) {
echo $object;
}
}
html:
<h2><?php prev_post_item($prev_post_title); ?></h2>
Two options
<h2>
<?php
$prev_post = get_previous_post();
$prev_post_id = $prev_post->ID;
if ( ! has_post_format('gallery', $prev_post_id ) ) {
echo 'Is not a gallery'
}
?>
</h2>
or in a function
function your_prev_post_item() {
$prev_post = get_previous_post();
$prev_post_id = $prev_post->ID;
if (!has_post_format('gallery', $prev_post_id)) {
echo 'Is not a gallery'
}
}
then the HTML
<h2><?php your_prev_post_item(); ?></h2>
I was working on a site with categories as primary menu .I have a new post type and added an archive content as menu item with label 'City Guide'.Now when I on City Guide page the menu item is not highlighting.Or with code the class 'current' is not printing in the menu item class.Here is my code:
<?php
$menu_name = PRIMARY_NAVIGATION_NAME;
$menu_items = array();
if( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ){
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = $menu ? wp_get_nav_menu_items($menu->term_id) : array();
}
?>
<nav class="visible-sm visible-md visible-lg" role="navigation">
<ul id="menu-primary-navigation" class="main-navigation">
<?php if( !empty($menu_items) ): ?>
<?php
$current_category_object = null;
if( !empty($wp_query->query_vars['category_name']) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug($wp_query->query_vars['category_name']);
}
?>
<?php foreach( $menu_items as $item): ?>
<?php
$active_class = false;
if ($item->object == 'category' && !empty($current_category_object) ){
$cat_object = get_category($item->object_id);
if ($cat_object->term_id == $current_category_object->term_id || $cat_object->term_id == $current_category_object->parent) {
$active_class = true;
}
}
?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class) echo ' current'?>">
<a href="<?php echo $item->url; ?>" class="main-category"<?php if( $item->target) echo ' target="' . $item->target . '"'; ?>><?php echo $item->title ?></a>
<?php if( $item->object == 'category'): ?>
<?php
$sub_categories = get_terms( array('category'), array('child_of' => $item->object_id, 'hide_empty' => 0) );
if (count($sub_categories) <= 4) {
include(locate_template('partials/sub-menu-two-featured.php'));
} else {
include(locate_template('partials/sub-menu-one-featured.php'));
}
?>
<?php endif; ?>
<?php if ($item->title == 'City Guide'): ?>
<?php include(locate_template('partials/sub-menu-city-guide.php')); ?>
<?php endif; ?>
</li>
<?php wp_reset_query(); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</nav>
How can i implement it into it using a condition to add current for my archive page menu item in the above code .
I tried
<?php if ($item->title == 'City Guide'): ?>
but it was echoing in all instance not when the menu is active.
Please help.
One possible solution is to use get_queried_object() function for this.
Codex says
if you're on a single post, it will return the post object
if you're on a page, it will return the page object
if you're on an archive page, it will return the post type object
if you're on a category archive, it will return the category object
if you're on an author archive, it will return the author object
If you start using $qo = get_queried_object() then no need to use this code
if( !empty( $wp_query->query_vars['category_name'] ) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug(
$wp_query->query_vars['category_name']
);
}
because:
If you are in category-archive page, $qo will contain category object, and $qo->term_id and $qo->parent will be available.
If you are in CPT archive page, then $qo will contain CPT object, and properties like CPT name, query_var, label and so on will be available.
So when you have custom queried object and with the help of some conditional tags like is_tax(), is_tag(), is_category(), is_post_type_archive() you will be able to determine exactly where you are.
P.S. You must check the codex first, because there have some notes for precedence when using get_queried_object()
<?php
$active_class = false;
$this_category = get_queried_object();
$this_category->name;
if ($this_category->name == 'city-guide' ){
$active_class = true;
} ?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class ==1 && sanitize_title($item->title) =='city-guide') echo ' current'; ?>">
this worked thanks #pgk for pointing me out