Two different get_posts() returning same post - php

I am using Wordpress and trying to create some boxes retrieving information from Adanvec Customs Fields on a custom post type.
I have this code:
<div class="col-sm-2 hidden-xs">
<?php
$box1 = get_front_page_box("Box 1");
$style_front = get_box_style($box1->ID, "front");
echo $box1->ID;
?>
<div id="front-box-1" class="front-box height-low <?php echo $box1->ID; if(has_back_panel($box1->ID)) echo "flip"; ?>">
<div class="front" style="<?php echo $style_front; ?>">Banana</div>
<?php
if(has_back_panel($box1->ID)):
$style_back = get_box_style($box1->ID, "back");
?>
<div class="back" style="<?php echo $style_back; ?>"></div>
<?php endif; ?>
</div>
</div>
<div class="col-sm-5 hidden-xs">
<?php
$box2 = get_front_page_box("Box 2");
$style_front = get_box_style($box2->ID, "front");
echo $box2->ID;
?>
<div id="front-box-2" class="front-box height-low <?php echo $box2->ID; if(has_back_panel($box2->ID)) echo "flip"; ?>">
<div class="front" style="<?php echo $style_front; ?>">Banana</div>
<?php
if(has_back_panel($box2->ID)):
$style_back = get_box_style($box2->ID, "back");
?>
<div class="back" style="<?php echo $style_back; ?>"></div>
<?php endif; ?>
</div>
</div>
And these functions:
function get_front_page_box($name) {
$args = array(
'post_title' => $name,
'post_type' => 'front-page-box',
'post_status' => 'publish'
);
$box_array = get_posts($args);
$box = $box_array ? $box_array[0] : false;
print_array($box);
return $box;
}
function get_box_style($id, $side) {
$style = "";
if(get_field($side."_panel_background_color", $id)) $style = "background-color:".get_field($side."_panel_background_color", $id).";";
return $style;
}
But for some reason both boxes have the same details (both from Box 2).
Any idea why these are returning the same info? Both posts ("Box 1" and "Box 2" exist under the front-page-boxcustom post type.

Seems the post_title is not a valid filter.
But found out there's a specific function to get posts by title.
get_page_by_title
Used $box1 = get_page_by_title("Box 1", NULL, "front-page-box");

Just for check, if you add another Box named "Box 3", all boxes have results from "Box 2" or "Box 3" ?

A better way to do this would be to use the post ID as the parameter in get_front_page_box().

Related

Add links to images gallery

I need to link some gallery images to different external webistes. After some research I'm not able to found a solution that isn't using a plugin. Is this possible in wordpress without using plugins? I can't install plugins for this project so any suggestion will be appreciated thanks.
Here is my code:
$args = array(
'post_type' => 'post',
'name' => 'partners'
);
$logo_img = new WP_Query( $args );
?>
<div class="container-fluid" id="">
<div class="row" style="margin-top:1em;margin-bottom:1em;">
<?php if( $logo_img->have_posts() ): while( $logo_img->have_posts() ): $logo_img->the_post();
$logo_gallery = get_post_gallery_images( $post->ID );
if( $logo_gallery ): ?>
<div class="col-sm-12 col-md-12 col-lg-12 text-center">
<?php foreach($logo_gallery as $logo ): ?>
<img class="img-fluid" src="<?php echo $logo; ?>" alt="" width="60" id="partner-logo" style="margin:0 .5em 0 .5em;"/>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
Depending on how many images we are talking about, if these images are named you could use some logic to create a URL based on the name, if they are not named and there aren't that many, you could rename them.
Here's an example;
<?php
// If there is a matching string, set a specific URL
if (preg_match('/site1/',$logo)) { $set = "yes"; $link = "https://website1.com"; }
if (preg_match('/site2/',$logo)) { $set = "yes"; $link = "https://website2.com"; }
if (preg_match('/site3/',$logo)) { $set = "yes"; $link = "https://website3.com"; }
// If there is no matching string, set a default URL
if ($set !== "yes") { $link = "https://default.com"; }
// Now encase your image in a URL
echo "<a href='$link'>
<img class='img-fluid' src='$logo' alt='' width='60' id='partner-logo' style='margin:0 .5em 0 .5em;'/>
</a>";
?>
By the way, id='partner-logo' - the id should be unique.

music widget in page template - select options

I am using the Rockit Now wordpress theme, which comes with a music player widget. When adding the widget to a sidebar, you have the followings options-
Title- (a section to type text..)
Artist for playlist- (a dropdown menu with all artist post names)
No of tracks to play- (a section to type a number..)
I am trying to display this widget in my artist page template so that it appears on each artist page with the music for that particular artist post.
I have added the following to my single-artists.php page template-
<?php the_widget( 'cs_music_player' ); ?>
This has successfully called the widget as I am now receiving the message 'No Results Found' on the page.. What I need to do is be able to select the options as above in the php.. I unfortunately can't work out how to do this..
Here is the cs_music_playlist_widget.php code-
<?php
class cs_music_player extends WP_Widget
{
function cs_music_player()
{
$widget_ops = array('classname' => 'cs_music_player', 'description' => 'Select artist to Play Your Playlist.' );
$this->WP_Widget('cs_music_player', 'ChimpS : MusicPlayList', $widget_ops);
}
function form($instance)
{
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = $instance['title'];
$get_post_slug = isset( $instance['get_post_slug'] ) ? esc_attr( $instance['get_post_slug'] ) : '';
$numtrack = isset( $instance['numtrack'] ) ? esc_attr( $instance['numtrack'] ) : '';
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">
<span>Title: </span>
<input class="upcoming" id="<?php echo $this->get_field_id('title'); ?>" size="40" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
</label>
</p>
<br />
<p>
<label for="<?php echo $this->get_field_id('get_post_slug'); ?>">
<span>artist for Playlist:</span>
<br /><br />
<select name="<?php echo $this->get_field_name('get_post_slug'); ?>" style="width:225px;">
<?php
global $wpdb,$post;
$args = array( 'post_type' => 'artists', 'posts_per_page' => -1,'post_status'=> 'publish');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<option <?php if($get_post_slug == $post->post_name){echo 'selected';}?> value="<?php echo $post->post_name;?>">
<?php echo substr(get_the_title(), 0, 20); if ( strlen(get_the_title()) > 20 ) echo "...";?>
</option>
<?php endwhile; ?>
</select>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('noot'); ?>">
<span>No Of Tracks To Play: </span>
<input class="upcoming" id="<?php echo $this->get_field_id('numtrack'); ?>" size="2" name="<?php echo $this->get_field_name('numtrack'); ?>" type="text" value="<?php echo esc_attr($numtrack); ?>" />
</label>
</p>
<div class="clear"></div>
<?php
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['get_post_slug'] = $new_instance['get_post_slug'];
$instance['numtrack'] = $new_instance['numtrack'];
return $instance;
}
function widget($args, $instance)
{
global $cs_transwitch;
extract($args, EXTR_SKIP);
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
$get_post_slug = empty($instance['get_post_slug']) ? ' ' : apply_filters('widget_title', $instance['get_post_slug']);
echo $before_widget;
$args=array(
'name' => $get_post_slug,
'post_type' => 'artists',
'post_status' => 'publish',
'showposts' => 1,
);
$get_posts = get_posts($args);
if( $get_posts ) {
$get_post_id = $get_posts[0]->ID;
}else{
$get_post_id = '';
}
// WIDGET display CODE Start
if (!empty($title))
echo $before_title . $title . $after_title;
global $wpdb;
if($get_post_id <> ""){
$artist_buy_amazon_db ='';
$artist_buy_apple_db = '';
$artist_buy_groov_db ='';
$artist_buy_cloud_db = '';
$cs_artist = get_post_meta($get_post_id, "cs_artist", true);
if ( $cs_artist <> "" ) {
$xmlObject = new SimpleXMLElement($cs_artist);
$artist_release_date_db = $xmlObject->artist_release_date;
$artist_buy_amazon_db = $xmlObject->artist_buy_amazon;
$artist_buy_apple_db = $xmlObject->artist_buy_apple;
$artist_buy_groov_db = $xmlObject->artist_buy_groov;
$artist_buy_cloud_db = $xmlObject->artist_buy_cloud;
enqueue_alubmtrack_format_resources('widget');
?>
<script>
jQuery(document).ready(function($){
new jPlayerPlaylist({
jPlayer: "#jquery_jplayer_<?php echo $get_post_id;?>",
cssSelectorAncestor: "#jp_container_<?php echo $get_post_id;?>"
}, [
<?php
$my_counter = 0;
foreach ( $xmlObject as $track ){
if ( $track->getName() == "track" ) {
if ( $my_counter < $instance['numtrack'] ) {
$artist_track_title = $track->artist_track_title;
$artist_track_mp3_url = $track->artist_track_mp3_url;
echo '{';
echo 'title:"'.$artist_track_title.'",';
echo 'mp3:"'.$artist_track_mp3_url.'"';
echo '},';
}
$my_counter++;
}
}
?>
], {
swfPath: "<?php echo get_template_directory_uri()?>/scripts/frontend/Jplayer.swf",
supplied: "mp3",
wmode: "window"
});
});
</script>
<!-- Now Playing Start -->
<div class="nowplaying">
<?php $cs_by = __('By: %s', CSDOMAIN); ?>
<h5><?php if($get_post_id <> ''){echo get_the_title($get_post_id);}?></h5>
<p><?php printf($cs_by, get_the_author()); ?> - <?php if(isset($artist_release_date_db)){ if($cs_transwitch =='on'){ _e('Release Date',CSDOMAIN); }else{ echo __CS('release_date', 'Release Date'). ' : '.$artist_release_date_db; }}?></p>
<div id="jquery_jplayer_<?php echo $get_post_id;?>" class="jp-jplayer"></div>
<div id="jp_container_<?php echo $get_post_id;?>" class="jp-audio">
<div class="jp-type-playlist">
<div class="jp-gui jp-interface">
<ul class="jp-controls">
<li>previous</li>
<li>play</li>
<li>pause</li>
<li>next</li>
<li>stop</li>
<li>mute</li>
<li>unmute</li>
<li>max volume</li>
</ul>
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
<div class="jp-volume-bar">
<div class="jp-volume-bar-value"></div>
</div>
<div class="jp-current-time"></div>
<div class="jp-duration"></div>
<ul class="jp-toggles">
<li>Shuffle</li>
<li>Shuffle off</li>
<li>Repeat All</li>
<li>Repeat off</li>
</ul>
</div>
<div class="jp-playlist">
<ul>
<li></li>
</ul>
</div>
</div>
</div>
</div>
<?php if($artist_buy_amazon_db <> '' or $artist_buy_apple_db <> '' or $artist_buy_groov_db <> '' or $artist_buy_cloud_db <> ''){?>
<!-- Buy Now Start -->
<div class="buynow">
<h5 class="white"><?php if($cs_transwitch =='on'){ _e('BUY NOW',CSDOMAIN); }else{ echo __CS('buy_now', 'BUY NOW'); } ?></h5>
<ul>
<?php if($xmlObject->artist_buy_cloud <> ""){?><li> </li><?php }?>
<?php if($xmlObject->artist_buy_amazon <> ""){?><li> </li><?php }?>
<?php if($xmlObject->artist_buy_apple <> ""){?><li> </li><?php }?>
<?php if($xmlObject->artist_buy_groov <> ""){?><li> </li><?php }?>
</ul>
<!-- Buy Now End -->
<div class="clear"></div>
</div>
<div class="clear"></div>
<?php } //Buy now Condition end?>
<?php }else{?>
<div class="list-thumb">
<ul>
<li>
<h2><?php _e("No results found.",CSDOMAIN); ?></h2>
</li>
</ul>
</div>
<?php
}
} // if artist is not Selected
else{
echo '<div class="box-small no-results-found"> <h5>';
_e("No results found.",CSDOMAIN);
echo ' </h5></div>';
}
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("cs_music_player");') );?>
the_widget accept three arguments:
$widget : the widget name, in your case cs_music_player
$instance : the widget instance settings. It's an array where your widget options goes. You can do something like this: array('title' => 'Your widget title', 'get_post_slug' => 'slug_of_your_artist', 'numtrack' => 10)
$args : an array of options used to display your widget. You can send an empty array to use the defaults, or just ignore that parameter.
In the end, your widget call will look like that:
global $post;
the_widget(
'cs_music_player',
array(
'title' => 'Your widget title',
'get_post_slug' => $post->post_name,
'numtrack' => 10
)
);

Images not visible in search results

I have this custom search function to search trough custom meta keys in Wordpress. The function works well.
However, all the images that I have added to my post using Advanced Custom Fields are now having their URL's changed to the attachment ID.
This should not happen according to the settings page in ACF:
The same field works ok on other pages, just not on the search results page. Check how it changes the image source on the search results:
How and why is the image URL here changed to the attachment ID? Kindly check out my code below:
function custom_search_function($pieces) {
// filter to select search query
if (is_search() && !is_admin()) {
global $wpdb;
$custom_fields = array('regio','provincie');
$keywords = explode(' ', get_query_var('s'));
$query = "";
foreach ($custom_fields as $field) {
foreach ($keywords as $word) {
$query .= "((mypm1.meta_key = '".$field."')";
$query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
}
}
if (!empty($query)) {
// add to where clause
$pieces['where'] = str_replace("((({$wpdb->posts}.post_title LIKE '%", "( {$query} (({$wpdb->posts}.post_title LIKE '%", $pieces['where']);
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
//$pieces['groupby'] = "{$wpdb->posts}.ID";
}
}
return ($pieces);
}
add_filter('posts_clauses', 'custom_search_function', 20, 1);
EDIT: Here's the code that displays my post results, the "foto" field is the field which is responsible for displaying the image:
<?php foreach( $posts as $post ):
//fusion-column-last, or none for normal class
$lastclass = '';
if(++$counter % 2 === 0) {
$lastclass = ' fusion-column-last';
}
setup_postdata( $post )
?>
<div class="fusion-one-half fusion-layout-column fusion-spacing-yes<?php echo $lastclass?>" style="margin-top:0px;margin-bottom:20px;background-color:white;">
<div class="fusion-column-wrapper">
<div class="bw-search-picture">
<?php $postid = get_the_ID(); ?>
<?php //echo $postid; ?>
<img src="<?php the_field('foto', $postid); ?>" alt="<?php the_title(); ?>"/>
</div>
<div class="bw-search-content">
<h2>
<a class="green" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
</h2>
<p class="bw-regio">Regio <?php the_field('regio'); ?></p>
<p>
<a style="color:#9C9E9F;" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">LEES VERDER ></a>
</p>
</div>
</div>
</div>
<?php endforeach; ?>
Try for the thumbnail:
$imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id( $post_id ));
echo $imgsrc[0];
Edit, use this code with the attachment id:
$imgsrc = wp_get_attachment_image_src(get_field('foto', $postid));
echo $imgsrc[0];
Rather than using
<img src="<?php the_field('foto', $postid); ?>" />
Try using
<?php $foto_url = get_field('foto', $postid); ?>
<img src="<?php echo $foto_url; ?>" />

Wordpress get content of page (with php inside)

I would like to be able to have the html content of a wordpress page. I try to use this kind of code :
$my_id = 5369;
$post_id_5369 = get_post($my_id);
$content = $post_id_5369->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
But it returns empty an empty string whereas when I access to the page, the content is not empty. Indeed, there is php code inside the content of the page I would like to get. So, how I can do to get the content of this page knowing that there is php code inside?
data content :
<div id="content" class="oe">
<?php $loop = new WP_Query( array('post_type'=>'mcm')); ?>
<div id="slid" class="div-slider">
<?php
/* get value */
$appercu_collection = get_post_custom_values ( "o" );
if ($appercu_collection) { // si appercu
class Image {
public $link;
public $image;
}
/* init value */
$imageCollection = array ();
/* complet collection */
foreach ( $appercu_collection as $img ) {
$img_tmp = new Image ();
$img_tmp->image = wp_get_attachment_image_src( $img , 'medium' )[0];
array_push ( $imageCollection, $img_tmp );
}
display_slider ( $imageCollection );
}
?>
</div>
<div id="content-meta">
<div class="meta-detail-header"><?php echo get_post_custom_values ( "_at" )[0]; ?></div>
<div class="meta-detail-title"><?php the_title(); ?></div>
<div class="audio">
<div id="audio-inner">
<?php $au = get_post_custom_values("_au");
if ($au) {
$urlguid = wp_get_attachment_url ( $au [0] );
if ($urlguid) {
display_audio ( $urlguid );
}
}
?>
</div>
</div>
</div>
<div id="ex">
<?php
$newdebut = get_long_formatted_date( $date_debut[0] );
$newfin = get_long_formatted_date( $date_fin[0] );
?>
<div id="expo-temp-dates"><?php echo sprintf( __( 'From %s to %s' , 'wptouch-pro' ) , $newdebut , $newfin );?></div>
<?php
$tar = get_post_custom_values("_tarif");
if($tar) { ?>
<div id="exprice"><?php echo $tar[0]; ?></div>
<?php }?>
</div>
<div id="content-o" >
<!-- GET Content -->
<div id="content-description" class="detail-container">
<?php display_zoom_controls(); ?>
<?php echo wpautop( wptouch_the_content() ); ?>
</div>
<!-- GET BONUS -->
<div id="content-bottom-sticky">
<a href="<?php echo $url_guide;?>"
class="tappable-button tappable dark" role="Button"><?php _e( 'TUIDE' , 'wptouch-pro' ); ?></a>
</div>
</div>
<?php endif; ?>
<!-- inside intervalle -->
<?php endwhile;
wp_reset_postdata();?>
<?php if( ! $content) { ?>
<div id="title" class="title">
<h2><?php _e( 'N' , 'wptouch-pro' ); ?></h2>
</div>
<?php }?>
I finally resolve my problem by doing an ajax request to my page and get the content thanks to
wp_remote_get . Indeed, It was a page with a custom type post and custom fields.
Here is my code :
$resp = wp_remote_retrieve_body( wp_remote_get($url,$option));
$doc = new DOMDocument();
$doc->loadHTML($resp);
$element = $doc->getElementById('content');
$content = $doc->saveHTML($element);
$content contains the html content of the element with the id "content" of the page whose url is $url.

Define number of images per row in PHP generated image gallery?

Currently my image gallery has 4 images per row. If the screen is minimized below the width of those 4 images, one image will drop to the next line and there will be a line break before the next row. Is there any way to make gallery continuous instead of having the break in images when the screen is resized? Ideally, I would like to start with 5 images per row, then if the viewer has a smaller screen, it will automatically adjust the number of images per row to fit whatever size window they are using.
Here is a link to the gallery: http://rabbittattoo.com/?gallery=gallery
And the PHP:
$pp_gallery_style = get_option('pp_gallery_style');
if($pp_gallery_style == 'f')
{
include_once(TEMPLATEPATH.'/gallery-f.php');
exit;
}
if(!isset($hide_header) OR !$hide_header)
{
get_header();
}
$caption_class = "page_caption";
$portfolio_sets_query = '';
$custom_title = '';
if(!empty($term))
{
$portfolio_sets_query.= $term;
$obj_term = get_term_by('slug', $term, 'photos_galleries');
$custom_title = $obj_term->name;
}
else
{
$custom_title = get_the_title();
}
/**
* Get Current page object
**/
$page = get_page($post->ID);
/**
* Get current page id
**/
if(!isset($current_page_id) && isset($page->ID))
{
$current_page_id = $page->ID;
}
if(!isset($hide_header) OR !$hide_header)
{
?>
<div class="wrapper_shadow"></div>
<div class="page_caption">
<div class="caption_inner">
<div class="caption_header">
<h1 class="cufon"><?php echo the_title(); ?></h1>
</div>
</div>
</div>
</div>
<!-- Begin content -->
<div id="content_wrapper">
<div class="inner">
<!-- Begin main content -->
<div id="gallery_wrapper" class="inner_wrapper portfolio">
<div class="standard_wrapper small">
<br class="clear"/><br/>
<?php
}
else
{
echo '<br class="clear"/>';
}
?>
<?php echo do_shortcode(html_entity_decode($page->post_content)); ?>
<!-- Begin portfolio content -->
<?php
$menu_sets_query = '';
$portfolio_items = 0;
$portfolio_sort = get_option('pp_gallery_sort');
if(empty($portfolio_sort))
{
$portfolio_sort = 'DESC';
}
$args = array(
'post_type' => 'attachment',
'numberposts' => $portfolio_items,
'post_status' => null,
'post_parent' => $post->ID,
'order' => $portfolio_sort,
'orderby' => 'date',
);
$all_photo_arr = get_posts( $args );
if(isset($all_photo_arr) && !empty($all_photo_arr))
{
?>
<?php
foreach($all_photo_arr as $key => $portfolio_item)
{
$image_url = '';
if(!empty($portfolio_item->guid))
{
$image_id = $portfolio_item->ID;
$image_url[0] = $portfolio_item->guid;
}
$last_class = '';
$line_break = '';
if(($key+1) % 4 == 0)
{
$last_class = ' last';
if(isset($page_photo_arr[$key+1]))
{
$line_break = '<br class="clear"/><br/>';
}
else
{
$line_break = '<br class="clear"/>';
}
}
?>
<div class="one_fourth<?php echo $last_class?>" style="margin-right:24px;margin-bottom:24px;margin-top:-20px">
<a title="<?php echo $portfolio_item->post_title?>" href="<?php echo $image_url[0]?>" class="one_fourth_img" rel="gallery" href="<?php echo $image_url[0]?>">
<img src="<?php echo get_stylesheet_directory_uri(); ?>/timthumb.php?src=<?php echo $image_url[0]?>&h=370&w=350&zc=1" alt=""/>
</a>
</div>
<?php
echo $line_break;
}
//End foreach loop
?>
<?php
}
//End if have portfolio items
?>
</div>
<!-- End main content -->
<br class="clear"/><br/>
</div>
<?php
if(!isset($hide_header) OR !$hide_header)
{
?>
</div>
<!-- End content -->
<?php get_footer(); ?>
<?php
}
?>
Thank you in advance for you help!

Categories