PHP can not echo each array - php

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

Related

Wordpress ACF Get Block Field

I'm running into an issue with ACF, and I just can't figure out what's going on, and nothing on the internet is helping out.
I've added some fields to the Image Slider block:
But no matter what I try inside of our custom block code: image-slider.php I cannot get the values of any of the auto_play fields. get_field always returns null. I know the value is there, because if I dump out get_fields( $postID ), I can see the ['page_builder'][2] element has the value I want. I could get to it that way, but I can't seem to determine which index I'm on (the 2) programmatically.
So if you know either, how I can access the field directly, or figure out my current 'page_builder' index, that would be extremely helpful.
It's super confusing, because the have_rows( 'slide_setting' ) call obviously knows where to look, and works as expected.
The custom block php looks like:
<?php
if(have_rows( 'slide_setting' ) ) {
$digits = 3;
$randID = rand(pow(10, $digits-1), pow(10, $digits)-1);
echo '<div class="container"><div class="row"><div id="swiper_'.$randID.'" class="col-md-12 wiche-swiper-top-navigation-wrapper">';
echo '<div class="swiper-container wiche-swiper-top-navigation">';
// var_dump( get_fields( get_the_ID() )['page_builder'][2] );
// var_dump( get_post_field( 'auto_play' ) );
// var_dump(get_field('image_slider_settings_auto_play'));
// var_dump(get_row_index());
// var_dump(get_field_objects( $post->ID ));
// var_dump( get_row_index() );
// var_dump( acf_get_field_group( 'slide_setting' ) );
// die();
if ( get_field( 'auto_play' ) ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . get_field( 'auto_play_delay' ) . '" data-swiper-disable-on-interaction="' . get_field( 'auto_play_disable_on_interaction' ) . '">';
} else {
echo '<div class="swiper-wrapper">';
}
while( have_rows( 'slide_setting' ) ) {
the_row();
$title = get_sub_field( 'title' );
$image = get_sub_field( 'image' );
$content = get_sub_field( 'content' );
if ( $image || $content ) {
echo '<div class="swiper-slide swiper-banner-slide swiper-no-swiping">';
if ( $title ) {
echo '<div class="text-center slider-top-title">';
echo $title;
echo '</div>';
}
if ( $image ) {
echo '<div class="banner-image">';
echo wp_get_attachment_image( $image, 'full', '', array( 'loading' => false ) );
echo '</div>';
}
if ( $content ) {
echo '<div class="banner-content">';
echo $content;
echo '</div>';
}
echo '</div>';
}
}
echo '</div>';
echo '</div>';
echo '<div class="swiper-button-next swiper-button-next-outsite">Next</div><div class="swiper-button-prev swiper-button-prev-outsite">Prev</div>';
echo '</div></div></div>';
}
So I wasn't able to get a perfect answer to my question, looks like the API to get what I want doesn't exist (dumb).
What I ended up with - I set up a new function in my theme's functions.php file that looks like the following:
$post_slider_config_index = 0;
function get_the_slider_config( $post_id ) {
global $post_slider_config_index;
$page_builder = get_fields( $post_id )['page_builder'];
$slider_config = null;
foreach ($page_builder as $key => $value) {
if ( $value['acf_fc_layout'] === 'image_slider_settings' ) {
if ( $key > $post_slider_config_index ) {
$slider_config = $value;
$post_slider_config_index = $key;
break;
}
}
}
return $slider_config;
}
And then inside my image-slider.php file I call it like so:
$slider_config = get_the_slider_config( get_the_ID() );
if ( $slider_config[ 'auto_play' ] ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . $slider_config[ 'auto_play_delay' ] . '" data-swiper-disable-on-interaction="' . $slider_config[ 'auto_play_disable_on_interaction' ] . '">';
} else {
echo '<div class="swiper-wrapper">';
}
The $post_slider_config_index variable keeps track of the last index retrieved so that if there are multiple sliders on a page, it'll grab the right one as its rendered.
It's not perfect, it's not super efficient, but it does what I needed. Annoying WP doesn't just give you the information it obviously has already regarding where you are in the page.

Include html href code to shotcode with "show content on a date"

I want to use a shortcode to show determinate content depending on a date given, I have this code on function.php on WP:
add_shortcode( 'stime', 'stime_f' );
function stime_f( $atts, $content = '' ) {
$a = shortcode_atts( [
'type' => false,
], $atts );
$output = '';
if ( $a['type'] ) {
$a['type'] = array_map( 'trim', str_getcsv( $a['type'], ',' ) );
}
foreach ($a as $value){
$list0 = $value[0];
$list1 = $value[1];
}
$tm = strtotime($list1);
if ( time() < $tm ) {
$list = "SOON";
return $list;
}
$list = $list0;
return $list;
}
So I used when publish:
[stime type="http://site1.com
http://site2.com,2020-12-05"]
And works, when it's not the date give result: SOON
otherwise result:
http://site1.com
http://site2.com
Results are fine, but I want to use:
[stime type="SITE1
SITE2,2020-12-05"]
I suppose it's for quotes on href and target, there are some way to make it possible?
Thanks
You can use enclosing shortcodes for this.
Below is the updated code:
add_shortcode( 'stime', 'stime_f' );
function stime_f( $atts, $content ) {
if ( $content ) {
$a = array_map( 'trim', str_getcsv( $content, ',' ) );
$list0 = $a[0];
$list1 = $a[1];
$tm = strtotime($list1);
if ( time() < $tm ) {
$list = "SOON";
return $list;
}
$list = $list0;
return $list;
} else {
return '';
}
}
Then use shortcode like this:
[stime] SITE1
SITE2,2020-12-05 [/stime]

Ajax filter not returning page content with filter

I have a special function on some of my pages that returns a modified embed code. It works great:
if (/*is_page() &&*/ has_category('Krajské zprávy')) {
/* get subtitle */
$subtitle = apply_filters( 'plugins/wp_subtitle/get_subtitle', '', array(
) );
if ((strpos($subtitle , 'kraj') == false) && (strpos($subtitle , 'Praha') == false) && (strpos($subtitle , 'Vysočina') == false)) {
$subtitle = "Všechny kraje";
};
/* get page id with embed code */
$id_short = 357;
$tag_id = array (
'Ovzduší' => 357
);
foreach ($tag_id as $k => $v) {
if (has_tag($k)) {$id_short = $v;}
}
/* get embed code & replace */
$acf_kod = get_field('embed_kod', $id_short /*,false*/);
$replace_sub = "<param name='filter' value='Parameters.Kraj=" . $subtitle . "'>";
preg_match_all('/<param [^>]*>/', $acf_kod, $matches);
$m_1 = $matches[0][0]; $m_2 = $matches[0][1];
$pos = strpos( $acf_kod, $m_1) + strlen( $m_1 );
if (strpos($acf_kod,$m_2)-(strpos($acf_kod,$m_1)+strlen($m_1))<2) {
$before = substr ($acf_kod, 0, $pos);
$after = substr( $acf_kod, $pos, strlen( $acf_kod ) );
$whole = $before . $replace_sub . $after;
$content = $whole .'<!--more-->' . $content;
};
/*echo $whole;*/
};
return $content;
};
add_filter('the_content', 'add_embed_parameter');
However, I also have a filter on my site that filters pages by categories and tags and returns them via Ajax. That, too, works just fine - but only with "standard pages" that don't use the code above. For the pages that do, it returns nothing.
This is a snippet of the php code that is used by the filter:
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();
/*$content = get_post_field( 'post_content', get_the_ID() );*/
$content = get_the_content (/*get_the_ID()*/);
$content_parts = get_extended( $content );
echo '<h2>' . $query->post->post_title . '</h2>',
$content/*$content_parts['main'] /*'<p>' . $query->post->post_excerpt . '</p>'*/;
endwhile;
wp_reset_postdata();
else :
echo 'No posts found';
endif;
die();
This is the whole AJAX thing (it's not mine, I'm using the tutorial here):
jQuery(function($){
$('#filter').submit(function(){
var filter = $('#filter');
$.ajax({
url:filter.attr('action'),
data:filter.serialize(), // form data
type:filter.attr('method'), // POST
beforeSend:function(xhr){
filter.find('button').text('Processing...'); // changing the button label
},
success:function(data){
filter.find('button').text('Apply filter'); // changing the button label back
$('#response').html(data); // insert data
}
});
return false;
});
});
Any idea where the problem might be? Many thanks!

Connecting a link to an image

I am using MediaWiki in combination with Joomla. As I want to add icons to some links I need to connect both. I know that this is possible through putting the img tag inside the a tag.
BUT the problem is that some links are generated throgh a function called makeListItem that MediaWiki uses for more than just these links. Now my question is, can I somehow connect the img to the a without putting it inside the a tag?
This calls the function to create the elements:
<?php $this->renderNavigation( 'PERSONAL' ); ?>
The actual function (shortened):
foreach ( $personalTools as $key => $item ) {
?>
<div class="searchbox" style="clear:both;">
<img src="<?php echo $icon[$key] ?>" alt="p-Icons" class="iconnav"/>
<?php
echo $this->makeListItem( $key, $item );
?>
</div>
<?php
}
?>
The src of the image is defined in a array that is declared just above the foreach.
Thanks in advance
You have to modify your makeListItem() function (BaseTemplate class).
function makeListItem( $key, $item, $options = array() ) {
if ( isset( $item['links'] ) ) {
$links = array();
foreach ( $item['links'] as $linkKey => $link ) {
$links[] = $this->makeLink( $linkKey, $link, $options );
}
$html = implode( ' ', $links );
} else {
$link = $item;
// These keys are used by makeListItem and shouldn't be passed on to the link
foreach ( array( 'id', 'class', 'active', 'tag', 'itemtitle' ) as $k ) {
unset( $link[$k] );
}
if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
// The id goes on the <li> not on the <a> for single links
// but makeSidebarLink still needs to know what id to use when
// generating tooltips and accesskeys.
$link['single-id'] = $item['id'];
}
$html = $this->makeLink( $key, $link, $options );
}
$attrs = array();
foreach ( array( 'id', 'class' ) as $attr ) {
if ( isset( $item[$attr] ) ) {
$attrs[$attr] = $item[$attr];
}
}
if ( isset( $item['active'] ) && $item['active'] ) {
if ( !isset( $attrs['class'] ) ) {
$attrs['class'] = '';
}
$attrs['class'] .= ' active';
$attrs['class'] = trim( $attrs['class'] );
}
if ( isset( $item['itemtitle'] ) ) {
$attrs['title'] = $item['itemtitle'];
}
return Html::rawElement( isset( $options['tag'] ) ? $options['tag'] : 'li', $attrs, $html );
}

Custom Fields - Boolean Display of True/False to Yes/No

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>

Categories