I am currently trying to create a plugin using Pinterest for Wordpress. I have ran into a slight problem, I've been trying to return an image with a link but only been able to only return a link.
<?php
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request['body'], true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}
?>
Anyone experienced with Wordpress Plugins and Pinterest can help me with this problem ?
Try to change your code as below. You will get text in $pin['note'] and image url in $img['url'].
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request, true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
$img = $pin['image']['original'];
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}
Related
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.
I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
I have created a loop to display the meta value but wanted to show it only once if they are the same of value. I have tried using array_unique but it doesn't seem to work
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
$menusInList = [];
while ( $_query->have_posts() ) {
$query->the_post();
$menu = get_post_meta($post->ID, 'awarded', true);
if (in_array($menu, $menusInList)) {
continue;
}
$menusInList[] = $menu;
echo '<li class="'.$menus .'" >' . $menu . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Save $menu in an array $menusInList and check via in_array. if it returns true use continueto skip.
$menusInList = [];
while ( $query->have_posts() ) {
$query->the_post();
$menu = get_post_meta($post->ID, 'award', true);
if (in_array($menu, $menusInList)) {
continue;
}
$menusInList[] = $menu;
// ...
}
I wrote this plugin that perfectly receives and shows data from an external API.
Code:
function get_posts_via_rest() {
$request = wp_remote_get( 'http://xxx' );
if( is_wp_error( $request ) ) {
return false; // Bail early
}
$body = wp_remote_retrieve_body( $request );
$risposta_json = json_decode( $body );
//print_r($data);
if( ! empty( $risposta_json ) ) {
foreach($risposta_json->data->offerte->data as $offerta ){ // now iterate through that array
if($offerta->struttura->id == aggancio_id_campo_uno()){ // check all conditions
echo '<ul>';
echo '<li>'.$offerta->struttura->nome .'</li>';
echo '<li>'.$offerta->struttura->url_completo .'</li>';
echo '<li>'.$offerta->titolo .'</li>';
echo '</ul>';
}
}
}
}
// Register as a shortcode to be used on the site.
add_shortcode( 'sc_get_posts_via_rest', 'get_posts_via_rest' );
Now I have created a type of custom post called bids, as this code shows all the bids for a given company id, which is set with an input field that I didn't put here because it was superfluous, my question is how do I now have the data from json to register them inside the db?
May this help you:
global $wpdb;
$table = $wpdb->prefix.'you_table_name';
$data = array('column1' => 'data one', 'column2' => 123);
$format = array('%s','%d');
$wpdb->insert($table,$data,$format);
$my_id = $wpdb->insert_id;
I have a coupon site, which basically has many vendors in it. I want to create a related stores widget kind of a thing which would display 5-6 stores on the side bar. and i want to them to relate to the current vendor page. till now i have managed to display 5-6 stores links randomly, but i am not able to display the related stores.
I am using clipper theme in wordpress, and i have kept all my stores in Coupons-Stores and not Post-categories.
<?php
$stores = get_terms('stores'); // Note: this returns NULL non-empty cats. It is not normal, could be because of the widget.
// Get all non-empty stores
$nonempty_stores = array();
foreach( $stores as $store )
if ( !empty( $store ) )
$nonempty_stores[] = $store;
$stores_count = count($nonempty_stores);
global $random_seed;
// Set initial seed value
$random_seed = $last_update;
if (DISPLAY_SAME_STORES_LIST_FOR_ALL_STORE_PAGES == false)
$random_seed = $random_seed + $store_catid;
function mtech_rand($min, $max)
{
global $random_seed;
$random_seed = ((1103515245*$random_seed)+12345)%(1<<31);
return (($random_seed%($max-$min+1))+$min);
}
echo "<div class=\"hdsboxbg\"> ";
$cntr = 0;
$displayed_cntr = 0;
while (($cntr < $stores_count) && ($displayed_cntr < NUM_OF_RANDOM_STORES_TO_DISPLAY))
{
$rand_store_idx = mtech_rand($cntr, $stores_count-1);
$rand_store = $nonempty_stores[$rand_store_idx];
if (($store_catid == 0) || ($rand_store->cat_ID != $store_catid)) // Random Stores Widget should not display a link to the current store page
{
$a_text = $rand_store->name . ' Coupons';
//$a_text = $rand_store->name;
echo "<div class=\"hdsmod\"> $a_text </div>";
$displayed_cntr++;
}
$nonempty_stores[$rand_store_idx] = $nonempty_stores[$cntr];
$nonempty_stores[$cntr] = $rand_store;
$cntr++;
}
echo "</div>";
?>
Any other methods are welcome. i have tried to display all the stores like this,
$terms = get_terms('stores');
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
it displays but i am not able to display the related stores, i am struggling to figure out on which grounds should i proceed. Any help would be appreciated.
got it.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>