I am importing fields from a Real Estate site.
The Import of one Custom Field looks like this...
Field Name after Import ---> assistedLiving
Field Value ---> false
to output the display in wordpress i use this...
<?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 echo get_post_meta($post->ID, 'assistedLiving' , true); ?> </span>
</li>
<?php wp_reset_query(); ?>
<?php endif; ?>
The Result on Site looks so...
**Seniorengerechtes Wohnen:false**
How can i display False/True Values to display Yes/No or in German Ja/NEIN?
The Funktion to display all Field (Display it Right with yes/no):
function immopress_the_fields( $args ) {
global $post, $ImmoPress;
extract( wp_parse_args( $args, array (
'id' => $post->ID,
'exclude' => $exclude,
'echo' => TRUE
) ), EXTR_SKIP );
$fields = immopress_get_fields( $args );
if ( !$fields )
return false;
$output = '';
$output .= '<ul class="immopress-fields">';
foreach ( $fields as $key => $value) {
$entry = $ImmoPress->values[$value];
if ( $entry == '')
$entry = $value;
$output .= "<li class='immopress-field-$key'>";
$output .= "<strong class='immopress-key'>{$ImmoPress->fields[$key]}: </strong>";
$output .= "<span class='immopress-value'>$entry</span>";
$output .= "</li>";
}
$output .= '</ul>';
if ( $echo ) {
echo $output;
} else {
return $output;
}
But i need to display only Single Name & Values not all importet fields.
Code for the Shortcode..
function immopress_fields_shortcode( $atts ) {
$args = wp_parse_args( $atts, array (
'echo' => FALSE
) );
return immopress_the_fields( $args );
}
add_shortcode( 'immopress_fields', 'immopress_fields_shortcode' );
Hope someone can Help me with that.
Thanks
Tony
How about if you simply use if / else statement like this:
<span class="value"><?php $assist=get_post_meta($post->ID, 'assistedLiving' , true); if ($assist=="") echo "keine Daten"; else if(strtolower($assist)=='true') echo "Ja"; else if(strtolower($assist)=='false') echo "Nein"; ?> </span>
Note:
For the code above to work,values for assistedLiving must use true/false and not 1 or 0.
Update
This is my first attempt at modifying shortcodes, so I'm not sure if it will work.
I'm also not sure if this is what you need:) - I modified it in a way so that you can specify a parameter fieldname and when used it should show only that field.
Change your shortcode function to this:
function immopress_fields_shortcode( $atts ) {
$args = wp_parse_args( $atts, array (
'echo' => FALSE,
'fieldname' => ""
) );
return immopress_the_fields( $args );
}
add_shortcode( 'immopress_fields', 'immopress_fields_shortcode' );
In function immopress_the_fields() after
foreach ( $fields as $key => $value) {
add this:
if ($fieldname!="")//only look for a field when it is used in the shortcode
if ($fieldname!=$key)//skip until you find the value in the shortcode
continue;
Not that this code is not efficient since $fields gets filled with all the fields all the time. Modified code is just a workaround.
I recommend learning Smarty and then your code will be:
<span class="value">
{if get_post_meta($post->ID, 'assistedLiving' , true)}
Yah
{else}
NEIN
{/if}
</span>
To create this output:
<span class="value">Yah</span>
Related
I'm setting up a site where a friend of mine can create new color pattern for her shop, but I would like to link it to a contact order form.
Currently, she has to create them both in the custom fields I set up AND in the contact form.
example: She create the color red for people to choose, but then she has to type 'red' in the select tag of contact form 7. "farve" = "color" (in danish) ;)
<div class="form-group">
<label>Color</label>
[select* menu-farve class:form-control "red" "blue"]
Now I have the custom post type slug called. "color", but how do I create an array that I can impliment into the form? and how to I get it into the form?
I have an array to display the color name from my custom post type if that helps:
<?php
$args = array( 'post_type' => 'color' );
$posts = get_posts( $args );
foreach ( $posts as $post ) :
$image = get_field('color_image', $post->ID);
setup_postdata( $post );
if ( get_field( 'sold' ) ): ; else: ?>
<div class="col-md-3">
<img class="img-responsive" src=" <?php echo $image['url'] ?> ">
<h2 class="">
<?php the_title(); ?>
</h2>
</div>
<?php endif ?>
<?php endforeach;
wp_reset_postdata(); ?>
There are several ways to accomplish dynamically generating Contact Form 7 selects.
Option 1: PHP
A great solution found both on the WordPress StackExchange and a blog by Lee Willis with the following being from the StackExchange as follows:
/** Dynamic List for Contact Form 7 **/
/** Usage: [select name term:taxonomy_name] **/
function dynamic_select_list($tag, $unused){
$options = (array)$tag['options'];
foreach ($options as $option)
if (preg_match('%^term:([-0-9a-zA-Z_]+)$%', $option, $matches))
$term = $matches[1];
//check if post_type is set
if(!isset($term))
return $tag;
$taxonomy = get_terms($term, array('hide_empty' => 0));
if (!$taxonomy)
return $tag;
foreach ($taxonomy as $cat) {
$tag['raw_values'][] = $cat->name;
$tag['values'][] = $cat->name;
$tag['labels'][] = $cat->name;
}
$tag['raw_values'][] = 'Other';
$tag['values'][] = 'Other';
$tag['labels'][] = 'Other - Please Specify Below';
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_select_list', 10, 2);
This is for taxonomies but can be edited to use the the array you provided as follows
$options = (array) $tag[‘options’];
foreach ( $options as $option ) {
if ( preg_match( ‘%^posttype:([-0-9a-zA-Z_]+)$%’, $option, $matches ) )
{
$post_type = $matches[1];
}
}
//check if post_type is set
if(!isset($post_type))
return $tag;
$args= array(
'post_type' => $post_type
);
$colors = get_posts($args);
if ( ! $colors )
return $tag;
foreach ( $colors as $color ) {
$tag['raw_values'][] = $color->post_title;
$tag['values'][] = $color->post_title;
$tag['labels'][] = $color->post_title;
}
return $tag;
}
Option 2: Plugin
The Contact Form 7 Dynamic Text Extension may have the functionality you're seeking.
I found my solution somewhere else, but the code is almost the same
function dynamic_field_values ( $tag, $unused ) {
if ( $tag['name'] != 'colorfield' )
return $tag;
$args = array (
'numberposts' => -1,
'post_type' => 'color',
'orderby' => 'title',
'order' => 'ASC',
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$tag['raw_values'][] = $custom_post->post_title;
$tag['values'][] = $custom_post->post_title;
$tag['labels'][] = $custom_post->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2);
I am using advanced custom fields pro to store and display information on estate sale dates. These are held in a repeater field, each row in the field has a start time, end time and a date. I need to display the information ordered by the date in the repeater field row. Currently it is ordered by date modified. I have tried several solutions but none seem to work for what I need to do.
This is what I have currently:
<?php
$latestes = new WP_Query(
array(
'post_type' => 'estate-sales',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'modified',
'order' => 'DESC'
)
);
while ( $latestes->have_posts() ) : $latestes->the_post();
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo ' ';
$rowses = get_field('estate_sale_dates');
$first_row = $rowses[0];
$first_row_date = $first_row['es-date'];
$first_row_start = $first_row['es-start-time'];
$first_row_end = $first_row['es-end-time'];
echo ' - ';
if ($first_row_date) {
echo date('F j, Y',strtotime($first_row_date));
}
else {
echo 'Sale Dates TBA';
}
endwhile;
wp_reset_query();
?>
The most common answer when searching for a solution is to do something like this:
$latestes = new WP_Query(
array(
'post_type' => 'estate-sales',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'modified',
'order' => 'DESC'
)
);
while ( $latestes->have_posts() ) : $latestes->the_post();
$repeater = get_field('estate_sale_dates');
foreach( $repeater as $key => $row )
{
$column_id[ $key ] = $row['es-date'];
}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ) {
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo ' ';
$rowses = get_field('estate_sale_dates');
$first_row = $rowses[0];
$first_row_date = $first_row['es-date'];
$first_row_start = $first_row['es-start-time'];
$first_row_end = $first_row['es-end-time'];
echo ' - ';
if ($first_row_date) {
echo date('F j, Y',strtotime($first_row_date));
}
else {
echo 'Sale Dates TBA';
}
}
endwhile;
wp_reset_query();
?>
However, when I try that, it lists each sale 3 times, does not order by the date at all, and if no date is entered (which needs to just default to Sale Dates TBA) it throws an error.
Any help greatly appreciated.
Note, that your integration isn't complete, signaled by having two get_field('estate_sale_dates');, and the name $first_row being out of place when iterating all of them. Without the sort, you're basically doing
$repeater = get_field( 'estate_sale_dates' );
...
foreach ( $repeater => $row_that_isnt_used )
{
$rowses = get_field( 'estate_sale_dates' );
$first_row = $rowses[0];
}
I think something like this is what you're after:
<?php
while ( $latestes->have_posts() ) {
$latestes->the_post();
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo '</a> ';
if ( ! count( $sale_dates = get_field('estate_sale_dates') ) )
echo '<span>Sale Dates TBA</span>';
else
{
// $order = array_column( $sale_dates, 'es-date' ): (php 5.5+)
foreach( $sale_dates as $ignore => $row )
$order[] = $row['es-date'];
array_multisort( $order, SORT_ASC, $sale_dates );
echo "<ul>";
foreach( $sale_dates as $row ) {
$date = $row['es-date'];
$start = $row['es-start-time'];
$end = $row['es-end-time'];
echo "<li>" . date('F j, Y',strtotime($row_date)) . " $start-$end</li>";
}
echo "</ul>";
}
echo "</li>";
}
I have the same problem with a calendar. There is no solution to do this in the right way with a repater field. The best way to do this, is to use an custom field in an hidden post type and a relation field.
Because any PHP solution is inperformant.
Make a Custom Post Type "Sales Dates" an in this a custom field "date" and a second "Post Relation".
Then select from Post Type "Sales Dates" all Posts order by meta key "dates" with the relation field you get the data from the not hidden post type.
Is there a better way to get the category names for a custom post type in wordpress?
<?php // get the portfolio categories
$terms = get_the_terms( $post->ID, 'filters' );
if ( $terms && ! is_wp_error( $terms ) ) :
$names = array();
$slugs = array();
foreach ( $terms as $term ) {
$names[] = $term->name;
$slugs[] = $term->slug;
}
$name_list = join( " / ", $names );
$slug_list = join( " category-", $slugs );
endif;
?>
<!-- BEGIN portfolio-item-->
<li class="portfolio-item third column category-<?php echo $slug_list; ?>" data-filter="category-<?php echo $slug_list; ?>">
<?php
$taxonomy = 'filters';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Or in fuctions.php place this:
function get_the_category_custompost( $id = false, $tcat = 'category' ) {
$categories = get_the_terms( $id, $tcat );
if ( ! $categories )
$categories = array();
$categories = array_values( $categories );
foreach ( array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
return apply_filters( 'get_the_categories', $categories );
}
and call the function as:
<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?>
My answer seems too simple, but I used this to list the categories from a wordpress plugin called DW Question Answer that has separate categories from the standard wp categories.
I am assuming that Design Wall used custom post types to create the q&a part and taxonomies to create the categories.
<ul>
<?php wp_list_categories('taxonomy=dwqa-question_category&hide_empty=0&orderby=id&title_li=');?>
</ul>
Assuming your custom taxonomy is recipegroups, i have implemented and tested this code in functions.php and i am sure that it will work in plugins too.
$recipeTerms = get_terms(array(
'taxonomy' => 'recipegroups',
));
foreach($recipeTerms as $recipeTerm){
if($recipeTerm->parent==0){
echo "<div class='termsBox'>"; // remove these div's to suit your needs..
$termLink =get_term_link( $recipeTerm );
echo "<a href='$termLink'><div class='termParent'>".$recipeTerm->name."</div></a> ";
$termChilds = get_term_children($recipeTerm->term_id, 'recipegroups' );
foreach($termChilds as $child){
$chTerm = get_term_by( 'id', $child, 'recipegroups');
$termLink =get_term_link( $chTerm );
echo "<a href='$termLink'><div class='top-cat-items'>".$chTerm->name."</div></a>";
}
echo "</div>"; // end of termsBox div
}
}
function get_people_cats($taxonomy) {
$output = '';
$terms = get_terms($taxonomy);
$count = count($terms);
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= "'". $term->name ."'". '=>';
$output .= "'". $term->term_id."',";
endforeach;
endif;
return $output;
}
This function returns a list of custom taxonomies and which words are found if the function is called in a template. But I want to assign the the function values to a variable in functions.php, and it's returning nothing.
If your Taxonomies are actually creaetd with a plugin instead of creating them with code in your functions.php the result of get_terms will be empty untill the taxonomies have initiated, which is on plugin level initiation better yet, most probably using the 'init' hook, so you would have to hook your function after the hook and use it only in your theme template files (not in functions.php) basicly you do something like:
add_action('init', 'get_people_cats', 9999);
Then you would call it normaly: $cats = get_people_cats('person_category');
Hope this solves your issue (I know it took me around an hour to solve this when I ran into it).
If you're not getting terms back it's probably due to where the taxonomy is being registered. If it's being registered in an init hook then you're likely trying to print them out before the taxonomy has actually been registered.
You can test it with the following:
function get_people_cats( $taxonomy ) {
$output = '';
$terms = get_terms('category');
$count = count( $terms );
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= $term->name.'=>'.$term->term_id;
endforeach;
endif;
return $output;
}
function echo_cats() {
echo get_people_cats('taxonomy_name', array('hide_empty' => 0) );
}
add_action('wp_footer', 'echo_cats');
By hooking into wp_footer it's not being called until well after any plugins that might have registered the taxonomy.
// UPDATE //
Got it. To create an array just do this:
$terms = get_terms($taxonomy, array('hide_empty' => false) );
if( !is_wp_error( $terms ) ) {
foreach( $terms as $term ) {
$types[$term->term_id] = $term->name;
}
}
return $types;
}
That will give you an array of $term->id => $term->name -- you may want to reverse that depending on how you're using the array.
Try this, works fine in my functions.php file on a local site:
function get_people_cats( $taxonomy ) {
$output = '';
$terms = get_terms( $taxonomy );
$count = count( $terms );
if ( $count > 0 ):
foreach ( $terms as $term ):
$output .= $term->name.'=>'.$term->term_id;
endforeach;
endif;
return $output;
}
echo get_people_cats('category');
I can not echo each Array. Dont know what i am doing wrong. What would be the way to echo each Array? Have tried some ways like below to echo but no success.
function simple_social_sharing( $attr_twitter = null, $attr_items = null ) {
// parse variables
$twitter_account = $attr_twitter;
$item_toggles = $attr_items;
// get post content and urlencode it
global $post;
$browser_title_encoded = urlencode( trim( wp_title( '', false, 'right' ) ) );
$page_title_encoded = urlencode( get_the_title() );
$page_url_encoded = urlencode( get_permalink($post->ID) );
// create share items array
$share_items = array ();
// set each item
$item_facebook = array(
"class" => "facebook",
"href" => "http://www.facebook.com/sharer.php?u={$page_url_encoded}&t={$browser_title_encoded}",
"text" => "Share on Facebook"
);
$item_twitter = array(
"class" => "twitter",
"href" => "http://twitter.com/share?text={$page_title_encoded}&url={$page_url_encoded}&via={$twitter_account}",
"text" => "Share on Twitter"
);
$item_google = array(
"class" => "google",
"href" => "http://plus.google.com/share?url={$page_url_encoded}",
"text" => "Share on Google+"
);
// test whether to display each item
if($item_toggles) {
// explode into array
$item_toggles_array = explode( ",", $item_toggles );
// set each item on or off
$show_facebook = $item_toggles_array['0'];
$show_twitter = $item_toggles_array['1'];
$show_google = $item_toggles_array['2'];
}
else {
$display_all_items = 1;
}
// form array of items set to 1
if( $show_facebook==1 || $display_all_items ) {
array_push( $share_items, $item_facebook );
}
if( $show_twitter==1 || $display_all_items) {
array_push( $share_items, $item_twitter );
}
if( $show_google==1 || $display_all_items) {
array_push( $share_items, $item_google );
}
// if one or more items
if ( ! empty( $share_items ) ) {
// create output
$share_output = "<ul class=\"ss-share\">\n";
foreach ( $share_items as $share_item ) {
$share_output .= "<li class=\"ss-share-item\">\n";
$share_output .= "<a class=\"ss-share-link ico-{$share_item['class']}\" href=\"{$share_item["href"]}\" rel=\"nofollow\" target=\"_blank\">{$share_item['text']}</a>\n";
$share_output .= "</li>\n";
}
$share_output .= "</ul>";
// echo output
echo $share_output;
}
}
// add shortcode to output buttons
function simple_social_sharing_shortcode( $atts, $content = null ) {
// parse variables / set defaults
extract( shortcode_atts( array(
'twitter' => '',
'display' => '1,1,1',
), $atts ) );
// output buttons
ob_start();
simple_social_sharing( $twitter, $display );
$output_string = ob_get_contents();
ob_end_clean();
return force_balance_tags( $output_string );
}
Developers call function is not working too
<?php simple_social_sharing('twitteraccount', '1,1,1'); ?>
I have tried to call each Array by this way but it's wrong.
<?php simple_social_sharing([1]); ?>
Down towards the bottom you have this snippet in your foreach block:
... href=\"{$share_item["href"]}\" ...
The double quotes around href are going to cause you a problem.
You need to have it as
... href=\"{$share_item['href']}\" ...
Function should RETURN values and does not ECHO it..
Instead of echo $share_output;, you should have it return $share_output;
Echo the function's return value (if array, then print_r it else echo it) to see output
<?php echo simple_social_sharing('twitteraccount', '1,1,1'); //NOTE THE 'echo' HERE.. ?>