Skip custom Field if not exsist? - wordpress

i have a code like this, to display a custom field on my Page...
<?php
global $wp_query;
$postid = $wp_query->post->ID;
if($immopress_property['assistedLiving']): ?>
<li class="assistedLiving">
<span class="attribute" style="width:200px;display:block;float:left;"><?php echo 'Seniorengerechtes Wohnen' ; ?><span class="wpp_colon">:</span></span>
<span class="value"><?php if (get_post_meta($post->ID, 'assistedLiving' , true)) echo "Ja"; else echo "Nein"; ?> </span>
</li>
<?php wp_reset_query(); ?>
<?php endif; ?>
But when the custom field NAME & VALUE not exist it shall skip to the next Value and Name. I try different ways to do that but no one works.
Hope someone can help me with that.

changed to this and it works...
if ( get_post_meta($post->ID, 'assistedLiving', true) ) { ?>

Related

$_GET Parameter already exist, replace parameter

I have a question im busy with a filter inside of wordpress. Now i have problem i try to explain.
If a visitor select a "single merk" url gonna be ?merk=Brand1 and than option 2 is select "Soort" than the url is gonna be "?merk=Brand1&soort=Soort1" that works.
But if you have select both, than want to change only ?merk=Brand2 than the url is "?merk=Brand2&soort=Soort1&merk=brand1" how can i make it if you switch between the "merk" that "Brand1" replaced to "Brand2".
Here is the code:
foreach ( $allemerken as $merksingle ) { ?>
<?php if(isset($_GET['merk']) && $_GET['merk'] == $merksingle->slug){?>
<i class="fa fa-times" aria-hidden="true"></i> <?php echo $merksingle->name; ?>
<?php }else{ ?>
<?php echo $merksingle->name; ?>
<?php }?>
<?php }?>
<p class="mt-30"><strong>Soort:</strong></p>
<?php
$soorten = get_terms('warmtepompcategorie');
// echo '<pre>'.print_r($allemerken,true).'</pre>';
foreach ( $soorten as $soort ) { ?>
<?php if(isset($_GET['soort']) && $_GET['soort'] == $soort->slug){?>
<i class="fa fa-times" aria-hidden="true"></i> <?php echo $soort->name; ?>
<?php }else{ ?>
<?php echo $soort->name; ?>
<?php }?>
<?php }?>
Ps. sorry for my bad English, but hope someone can help me out!
I have made a function.
And i did it like this:
function replace_get($newname,$newvalue){
$oldvars = $_GET;
$varsnew = $newname.'='.$newvalue;
foreach($oldvars as $name=> $value){
if($name != $newname){
$varsnew .= '&'.$name.'='.$value;
}
}
return $varsnew;
}
To prevent the values from duplicating I would operate on a $_GET array, or its copy for adding or removing the parameters like:
$_GET['soort'] = 'new value'; // adding, modifying value
unset($_GET['soort']); // removing value
and then using http_build_query() for building the URL-encoded query string like this:
$query = http_build_query(array('aParam' => $data));

Wordpress ACF repeater field > radio button value and babel

I have a repeater field in wich I have a radio field. I need to output the label and the value.
In ACF field, I putted value:label such as :
red: Red Carpet
green: Green leaf
I tried a piece of code :
$field = get_sub_field_object(‘field_name’);
$value = get_sub_field(‘field_name’);
$label = $field[‘choices’][ $value ];
I tried to replace the fieldname by the field_id, but it returns "Array" instead of the value.
I need to use the value in a class, and the label in a title. Can you help me ?
get_sub_field_object() has to be used within a has_sub_field() loop, such as this:
<?php while( has_sub_field('repeater_fields_name') ): ?>
<?php
// vars
$select = get_sub_field_object('radio_field_from_your_code');
$value = get_sub_field('radio_field_from_your_code');
?>
<ul>
<?php foreach( $select['choices'] as $k => $v ): ?>
<li>
<?php if( $k == $value ): ?>
<span class="selected">Selected!</span>
<?php endif; ?>
<?php echo $v; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endwhile; ?>
You're probably close to getting the right values. Just tweak things to follow this general pattern. More info on this function can be found at ACF's docs site: https://www.advancedcustomfields.com/resources/get_sub_field_object/

Wordpress - List all the post's categories with custom code in the single page

First of all it's supposed to be used in the single page. After the single post I want to display the categories that the post belongs to.
The basic code for this is <?php the_category(' | '); ?> which outputs a simple link. Then we have <?php echo get_the_category_list(); ?> which outputs a more specific code (http://codex.wordpress.org/Function_Reference/get_the_category_list):
<ul class="post-categories">
<li>
Business
</li>
</ul>
However I need to decompose this code. For example, I want the <a> tag to be before the <li> tag. I've got a code that does what I want, but it's supposed to display all the categories available in my page, which is:
<?php
$categories = get_categories();
foreach($categories as $category)
{
?>
<li><?php echo $category->name ?> <span class="lower">(<?php echo $category->count ?>)</span></li>
<?php
}
?>
Any idea how I can make this work?
Thanks!
You can use get_the_category() its return you category objects that you can loop and do with them what you want.
$category = get_the_category( /* $post_id */ );
print_r($category);
foreach($category as $cat) {
?>
<?php echo $cat->name; ?>
<?php
}
<?php
$category = get_the_category($post_id);
foreach($category as $cat)
{
?>
<li><?php echo $cat->name ?></li>
<?php
}
?>

Displaying a custom post type title within another loop

I have a Custom Post Type set up called Venues. I'm also using a plugin called Event Organiser, and I want to display the title and link to the venue within one of the Event Organiser templates.
The code is:
<?php if( $eo_event_loop->have_posts() ): ?>
<ul <?php echo $id; ?> class="<?php echo esc_attr($classes);?>" >
<?php while( $eo_event_loop->have_posts() ): $eo_event_loop->the_post(); ?>
<?php
//Generate HTML classes for this event
$eo_event_classes = eo_get_event_classes();
//For non-all-day events, include time format
$format = ( eo_is_all_day() ? $date_format : $date_format.' '.$time_format );
?>
<li class="<?php echo esc_attr(implode(' ',$eo_event_classes)); ?>" >
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_title(); ?></a> at VENUE NAME HERE <?php echo __('on','eventorganiser') . ' '.eo_get_the_start($format); ?>
</li>
<?php endwhile; ?>
</ul>
Where VENUE NAME HERE is, I want the title and link to my custom post type. Might be just something really simple I'm missing, but any help much appreciated.
I used Advanced Custom Fields plugin and used 'Relationship' field so that I could choose a Venue for each event. Then used this code to bring through the Venue link:
<?php $venue = get_field('location_venue'); ?>
<?php foreach( $venue as $venue ): ?>
<?php echo get_the_title( $venue->ID ); ?>
<?php endforeach; ?>
More info available here:
http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/

How can i have custom fields on the posts page in wordpress?

First I've created a home.php page to replace index.php and can add some custom fields on this new one and to have in it lastest 3 posts.
On home.php page I put:
<?php echo get_post_meta($post->ID, 'test', true); ?>"/>
but it doesn't works cause it tries get the post id and not the id of the page. If i put 18 (id of the page) directly it works, but i want it dinamicaly:
<?php echo get_post_meta(18, 'test', true); ?>"/>
And this condition is not satisfied for test:
if($post->post_type == 'page'){
echo 'This item is a page and the ID is: '.$post->ID;
}
Heyo,
I think your problem might be that you should have that within a loop.
For example if you were displaying several post from a category and displaying each custom field it might look like this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<span class="artist-name">
<?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?>
</span>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
</li>
<?php endwhile; endif; ?>
Otherwise (not sure how your theme is set up), what you might have to do is edit content.php and add:
<?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?>
Try:
if ($post->post_type == 'page') {
<?php echo get_post_meta($page_id, 'test', true); ?>
}
Hope this helps :)

Resources