Within wordpress and using ACF Pro, I'm merging multiple Songkick XML feeds with PHP, attaching an artist name to them (annoyingly each feed doesn't include the artist name), and ordering them by the event date.
I've managed to put this all together (with help from different questions on here) using separate steps, but the page is loading very slowly, so I wondered if there was a way of streamlining or merging some of the steps?
With the code below I am:
Fetching multiple XML feeds (sing an ACF fields from other pages on the site)
Attaching an artist name to each feed
Merging the feeds (whilst removing some of the data to try and speed
up the processing time)
Outputting the information into a table while ordering by date
My code below:
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => 'artists'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : $artistFeedCount = 0; while ( $parent->have_posts() ) : $parent->the_post(); if( get_page_template_slug() == 'template-artist.php' && 'publish' === get_post_status() ) {
$singleArtistName = get_the_title();
$singleArtistSongkickRSS = 'https://api.songkick.com/api/3.0/artists/' . get_field('artist_songkick_id') . '/calendar.xml?apikey=XXXXXXXXXXXXXXXX';
$SongkickEvents[]=$singleArtistName;
$SongkickEvents[$singleArtistSongkickRSS] = $SongkickEvents[$artistFeedCount];
unset($SongkickEvents[$artistFeedCount]);
$artistFeedCount++;
?>
<?php }; endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
<?php
$eventsDom = new DOMDocument();
$eventsDom->appendChild($eventsDom->createElement('events'));
foreach ($SongkickEvents as $artist_dates => $artist_name ) {
$eventsAddDom = new DOMDocument();
$eventsAddDom->load($artist_dates);
$events = $eventsAddDom->getElementsByTagName('event');
if ($eventsAddDom->documentElement) {
foreach ($events as $event) {
$eventsDom->documentElement->appendChild(
$eventsDom->importNode($event, TRUE)
);
$artistName = $eventsDom->createElement('mainartist', $artist_name);
foreach($eventsDom->getElementsByTagName('event') as $singleEvent) {
$singleEvent->appendChild($artistName);
}
foreach($eventsDom->getElementsByTagName('performance') as $singlePerformance) {
$singlePerformance->parentNode->removeChild($singlePerformance);
}
}
}
}
$newXML = $eventsDom->saveXml();
$LiveDates = simplexml_load_string($newXML);
$eventsArr=array();
foreach($LiveDates->event as $eventsArrSingle)
{
$eventsArr[]=$eventsArrSingle;
}
usort($eventsArr,function($dstart,$dend){
return strtotime($dstart->start['date'])-strtotime($dend->start['date']);
});
foreach($eventsArr as $eventsArrSingle) { ?>
<div class="event-row <?php $eventStatus = $eventsArrSingle['status']; if($eventStatus == 'cancelled' || $eventStatus == 'postponed'): echo 'cancelled'; endif; ?>">
<div class="event-block event-date">
<span><?php $eventDate=$eventsArrSingle->start['date']; echo date("d", strtotime($eventDate)); ?></span>
<?php echo date("M", strtotime($eventDate)); ?>
</div>
<div class="event-block event-info">
<span><?php echo $eventsArrSingle->mainartist; ?></span>
<?php if($eventsArrSingle->venue['displayName'] != 'Unknown venue'): echo $eventsArrSingle->venue['displayName'] . ', '; endif; ?><?php echo $eventsArrSingle->venue->metroArea['displayName']; ?>
</div>
<div class="event-block event-button">
<span><?php if($eventStatus == 'cancelled'): echo 'Cancelled'; elseif($eventStatus == 'postponed'): echo 'Postponed'; else: echo 'Tickets'; endif; ?></span> <i class="fas fa-arrow-right"></i>
</div>
</div>
<?php };?>
Any help would be greatly appreciated, I'm sure there's a way of merging everything in fewer steps!
For anyone coming across the same issue, this is the solution I used. Using the Transient API to store the feed for 3 hours at a time.
<?php
function eventsFunction() {
// Do we have this information in our transients already?
$eventTransient = get_transient( 'eventsTransientData' );
// Yep! Just return it and we're done.
if( ! empty( $eventTransient ) ) {
echo $eventTransient;
} else {
$single_artist_ids = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => 'artists'
);
$parent = new WP_Query( $single_artist_ids );
if ( $parent->have_posts() ) : $artistFeedCount = 0; while ( $parent->have_posts() ) : $parent->the_post(); if( get_page_template_slug() == 'template-artist.php' && 'publish' === get_post_status() ) {
$singleArtistName = get_the_title();
$singleArtistSongkickRSS = 'https://api.songkick.com/api/3.0/artists/' . get_field('artist_songkick_id') . '/calendar.xml?apikey= XXXXXXXXXXXXXXXX';
$SongkickEvents[]=$singleArtistName;
$SongkickEvents[$singleArtistSongkickRSS] = $SongkickEvents[$artistFeedCount];
unset($SongkickEvents[$artistFeedCount]);
$artistFeedCount++;
?>
<?php }; endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
<?php
$eventsDom = new DOMDocument();
$eventsDom->appendChild($eventsDom->createElement('events'));
foreach ($SongkickEvents as $artist_dates => $artist_name ) {
$eventsAddDom = new DOMDocument();
$eventsAddDom->load($artist_dates);
$events = $eventsAddDom->getElementsByTagName('event');
if ($eventsAddDom->documentElement) {
foreach ($events as $event) {
$eventsDom->documentElement->appendChild(
$eventsDom->importNode($event, TRUE)
);
$artistName = $eventsDom->createElement('mainartist', $artist_name);
foreach($eventsDom->getElementsByTagName('event') as $singleEvent) {
$singleEvent->appendChild($artistName);
}
foreach($eventsDom->getElementsByTagName('performance') as $singlePerformance) {
$singlePerformance->parentNode->removeChild($singlePerformance);
}
}
}
}
$eventsXML = $eventsDom->saveXml();
$LiveDates = simplexml_load_string($eventsXML);
$eventsArr=array();
foreach($LiveDates->event as $eventsArrSingle)
{
$eventsArr[]=$eventsArrSingle;
}
usort($eventsArr,function($dstart,$dend){
return strtotime($dstart->start['date'])-strtotime($dend->start['date']);
});
$eventsStored = '';
foreach($eventsArr as $eventsArrSingle) {
$eventsStored .= '<div class="event-row ';
$eventStatus = $eventsArrSingle['status']; if($eventStatus == 'cancelled' || $eventStatus == 'postponed'): $eventsStored .= 'cancelled'; endif;
$eventsStored .= '">
<div class="event-block event-date">
<span>';
$eventDate=$eventsArrSingle->start['date']; $eventsStored .= date("d", strtotime($eventDate));
$eventsStored .= '</span>';
$eventsStored .= date("M", strtotime($eventDate));
$eventsStored .= '</div>
<div class="event-block event-info">
<span>';
$eventsStored .= $eventsArrSingle->mainartist;
$eventsStored .= '</span>';
if($eventsArrSingle->venue['displayName'] != 'Unknown venue'): $eventsStored .= $eventsArrSingle->venue['displayName'] . ', '; endif;
$eventsStored .= $eventsArrSingle->venue->metroArea['displayName'];
$eventsStored .= '</div>
<div class="event-block event-button">
<a href="' . $eventsArrSingle['uri'] . '" target="_blank"><span>';
if($eventStatus == 'cancelled'): $eventsStored .= 'Cancelled'; elseif($eventStatus == 'postponed'): $eventsStored .= 'Postponed'; else: $eventsStored .= 'Tickets'; endif;
$eventsStored .= '</span> <i class="fas fa-arrow-right"></i></a>
</div>
</div>';
};
set_transient( 'eventsTransientData', $eventsStored, 3*HOUR_IN_SECONDS );
echo $eventsStored;
}
}
eventsFunction();
?>
Related
I want to add shortcode that has information about my custom post after the_content but when i add the code it shown before the_content
Here is my code
<?php
function foobar_func() { ?>
<h3>Title : </h3>
<ul>
<li>foo : </li>
</ul>
<?php }
add_shortcode( 'project_information', 'foobar_func' );
function wpdev_before_after( $content ) {
$beforecontent = '';
if ( is_single() ) {
$aftercontent = '[project_information]';
} else {
$aftercontent = '';
}
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
?>
My foobar_func should be like this and i have more custom taxonomy to list and if I use variable to show them my code will be very messy
function foobar_func() { ?>
<h3>Title : </h3>
<ul>
<li>foo : <?php
$terms = wp_get_post_terms( $post->ID, 'taxonomy', $args );
foreach( $terms as $term ) {
if ( end($terms) == $term ){
echo '' . $term->name . '';
} else {
echo '' . $term->name . ' , ';
}
}
?></li>
</ul>
<?php }
Try assigning the html to a variable and return it in your foobar_func()
function foobar_func() {
$html = "
<h3>Title : </h3>
<ul>
<li>foo :
";
$terms = wp_get_post_terms( $post->ID, 'taxonomy', $args );
foreach( $terms as $term ) {
if ( end($terms) == $term ){
$html .= '' . $term->name . '';
} else {
$html .= '' . $term->name . ' , ';
}
}
$html .= "
</li>
</ul>";
return $html;
}
i've created a colophon for my website in which i post all the logos of the different sponsors i have. I add all the sponsors via custom post type. i also added a specific custom taxonomy to distinguish between the different typologies of sponsorships.
I use this code in the footer.php to display them:
<?php $terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { ?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $term->slug,
);
$pesca_post = new WP_Query ($arg);
$quanti_post = $pesca_post->post_count;
if(have_posts()){
while ($pesca_post->have_posts()) : $pesca_post->the_post();
$featured = get_the_post_thumbnail_url(get_the_ID(),'large');
if ($quanti_post == 5){
$classe_bootstrap = 15;
}elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
echo '<div class="col-md-' . $classe_bootstrap . '">';
if (isset($featured)){
$img = $featured;
}else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($post->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
<?php echo '</div>';
endwhile;
}?>
</div>
<?php }
}?>
my problem is that this code is completely working just on some pages, on other it shows the contents avoiding the ones belonging to the first term, no matter which it will be.
I have noticed that it works in pagaes where i use other queries.
What am i doing wrong?
i changed it in this way and now it's working!
$terms = get_terms('sponsor_tipology');
$count = count( $terms );
if ( $count > 0 ) {
foreach ( $terms as $term ) { //per ogni termine presente
$nome = $term->slug;?>
<div class="col-xs-12 <?php echo $term->slug ;?>">
<h3><?php echo $term->name;?></h3>
<?php $arg = array (
'post_type' => 'colophone',
'post_per_page' => -1,
'sponsor_edition' => 'current',
'sponsor_tipology' => $nome,
);
$elementi = get_posts($arg);
$quanti_post = count( $elementi );
if ($quanti_post == 5){
$classe_bootstrap = 15;
}
elseif ($quanti_post > 5){
$classe_bootstrap = "2 text-center";
}
elseif($quanti_post < 5){
$classe_bootstrap = 12/$quanti_post;
}
foreach($elementi as $elemento){
$featured = get_the_post_thumbnail_url($elemento->ID,'large');
if (isset($featured)){
$img = $featured;
}
else{
$img = get_template_directory_uri() . '/img/placeholder.png';
} ?>
<div class="col-md-<?php echo $classe_bootstrap; ?>">
<a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($elemento->ID);?>" >
<div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
</a>
</div>
<?php }?>
</div>
<?php }
}?>
hello I am new to programing. I am creating a simple RSS feed plugin for wordpress to upload some products to WooCommerce and need to upload the category to wordpress . In plugin the category is visible but they are not showing like the url link. I need the category to show like checkbox. Can anybody have any idea ?
File number 1
* Init feed with information from DB
private function load()
{
if ($this->id) {
$post = get_post( $this->id );
if (!$post) {
$this->id = null;
return;
}
$this->title = $post->post_title;
$this->id = $post->ID;
$meta = get_post_meta($post->ID);
foreach ($meta as $key=>$item) {
$newKey = substr($key, 1);
$this->properties[$newKey] = $item[0];
if ($newKey == 'post_category') {
$this->properties[$newKey] = unserialize($item[0]);
}
}
}
}
..................
$fields = array( 'post_category');
..................
// Create post
$post = array('post_category' => $this->post_category);
And the file number 2 have this
<div class="postbox">
<div class="handlediv" title="<?php esc_html_e('Click to toggle', 'rss-autopilot'); ?>"><br></div>
<h3 class="hndle ui-sortable-handle"><span><?php esc_html_e('Categories', 'rss-autopilot'); ?></span></h3>
<div class="inside">
<ul class="rssap-categories-list">
<?php wp_category_checklist( 0, 0, $feed->post_category, false, null, true ); ?>
</ul>
<div class="clear"></div>
</div>
</div>
Here is your code.
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul class="product-cats">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<input name="product_category[]" type="checkbox" value="'. $term->term_id.'"';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
I want the page http://zanifesto.com/product/boise-infographic/ go back to http://zanifesto.com/gallery, not the Infographics category page at the "Back to" link at the top.
When I replace the .home_url etc etc you see below with the gallery url mentioned above, it errors out.
I am looking to understand why the php code doesn't act like I am replacing one url for another.
<div class="product_navigation desktops">
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){ echo '<div class="nav-back">‹ '. __('Back to ', 'theretailer').$term_list.'</div>'; };
?>
<?php if (function_exists('be_previous_post_link')) { ?>
<div class="nav-next-single"><?php be_next_post_link( '%link', '', true,'', 'product_cat' ); ?></div>
<div class="nav-previous-single"><?php be_previous_post_link( '%link', '', true,'', 'product_cat' ); ?></div>
<div class="nav-prev-next-txt"><?php _e('Prev / Next', 'theretailer'); ?></div>
<?php } ?>
<div class="clr"></div>
</div>
<?php } ?>
this code hack could do what you want...
<?php
$term_list = '';
$j=0;
foreach ($terms as $term) {
if($term->parent==0){
$j++;
if( $j <= 1 ){
$url=home_url() . '/' . $category_slug . '/'. $term->slug;
if($url=='http://zanifesto.com/product-category/infographics'){
$url='http://zanifesto.com/gallery';
}
$term_list .= '' . $term->name . '';
}
}
}
if(strlen($term_list) > 0){ echo '<div class="nav-back">‹ '. __('Back to ', 'theretailer').$term_list.'</div>'; };
?>
I'm trying to tweak a Wordpress Plugin - Category Thumbnail List to display "Coming Soon" when the shortcode calls for posts in a category but there are none.
I've tried contacting the developer but had no luck.
I've modified the original code using the following but it doesn't display. Any ideas?
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
Here's the full code:
<?php
/*
Plugin Name: Category Thumbnail List
Plugin URI: http://jonk.pirateboy.net/blog/category/bloggeriet/wordpress/plugins/
Description: Creates a list of thumbnail images, using the_post_thumbnail() in WordPress 2.9 and up.
Version: 1.11
Author: Jonk
Author URI: http://jonk.pirateboy.net
*/
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
if ($categoryThumbnailList_Order == '') {
$categoryThumbnailList_Order = 'date';
}
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
if ($categoryThumbnailList_OrderType == '') {
$categoryThumbnailList_OrderType = 'DESC';
}
$categoryThumbnailList_Path = get_option('siteurl')."/wp-content/plugins/categoy-thumbnail-list/";
define("categoryThumbnailList_REGEXP", "/\[categorythumbnaillist ([[:print:]]+)\]/");
define("categoryThumbnailList_TARGET", "###CATTHMBLST###");
function categoryThumbnailList_callback($listCatId) {
global $post;
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
$tmp_post = $post;
$myposts = get_posts('numberposts=-1&&category='.$listCatId[1].'&&orderby='.$categoryThumbnailList_OrderType.'&&order='.$categoryThumbnailList_Order);
$output = '<div class="categoryThumbnailList">';
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail() ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;
$output .= '</div>';
$output .= '<div class="categoryThumbnailList_clearer"></div>';
$post = $tmp_post;
wp_reset_postdata();
return ($output);
$output = '';
}
function categoryThumbnailList($content) {
return (preg_replace_callback(categoryThumbnailList_REGEXP, 'categoryThumbnailList_callback', $content));
}
function categoryThumbnailList_css() {
global $categoryThumbnailList_Path;
echo "
<style type=\"text/css\">
#import url(\"".$categoryThumbnailList_Path."categoy-thumbnail-list.css\");
</style>
";
}
add_action('wp_head', 'categoryThumbnailList_css');
add_filter('the_content', 'categoryThumbnailList',1);
?>
<?php
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_options_page('Category Thumbnail List Options', 'Category Thumbnail List', 'manage_options', 'category-thumbnail-list', 'my_plugin_options');
}
function my_plugin_options() {
global $categoryThumbnailList_Order;
global $categoryThumbnailList_OrderType;
if( $_POST['save_category-thumbnail-list_settings'] ) {
// update order type
if( !$_POST['category-thumbnail-list_ordertype'] )
{
$_POST['category-thumbnail-list_ordertype'] = 'date';
}
update_option('category-thumbnail-list_ordertype', $_POST['category-thumbnail-list_ordertype'] );
// update order
if( !$_POST['category-thumbnail-list_order'] )
{
$_POST['category-thumbnail-list_order'] = 'DESC';
}
update_option('category-thumbnail-list_order', $_POST['category-thumbnail-list_order'] );
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
echo "<div id=\"message\" class=\"updated fade\"><p>Your settings are now updated</p></div>\n";
}
?>
<div class="wrap">
<h2>Category Thumbnail List Settings</h2>
<form method="post">
<table class="form-table">
<tr valign="top">
<th scope="row">Order by</th>
<td>
<select name="category-thumbnail-list_ordertype" id="category-thumbnail-list_ordertype">
<option <?php if ($categoryThumbnailList_OrderType == 'date') { echo 'selected="selected"'; } ?> value="date">Date</option>
<option <?php if ($categoryThumbnailList_OrderType == 'title') { echo 'selected="selected"'; } ?> value="title">Title</option>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">Display order</th>
<td>
<select name="category-thumbnail-list_order" id="category-thumbnail-list_order">
<option <?php if ($categoryThumbnailList_Order == 'DESC') { echo 'selected="selected"'; } ?> value="DESC">Descending (z-a/9-1/2010-2001)</option>
<option <?php if ($categoryThumbnailList_Order == 'ASC') { echo 'selected="selected"'; } ?> value="ASC">Ascending (a-z/1-9/2001-2010)</option>
</select>
</td>
</tr>
</table>
<div class="submit">
<!--<input type="submit" name="reset_category-thumbnail-list_settings" value="<?php _e('Reset') ?>" />-->
<input type="submit" name="save_category-thumbnail-list_settings" value="<?php _e('Save Settings') ?>" class="button-primary" />
</div>
<div>
Update the thumbnail sizes here
</div>
<div>
You may need to update your css when changing the thumbnail size
</div>
</form>
</div>
<?php
}
?>
Many Thanks,
Mike
Haven't tested your code, but a quick glance shows at least one problem... you're using this outside of the loop. When not used in the loop, has_post_thumbnail() doesn't know what post you're checking. With just that modification, your code should be this:
foreach($myposts as $post) :
setup_postdata($post);
if ( has_post_thumbnail( $post->ID ) ) {
$link = get_permalink($post->ID);
$thmb = get_the_post_thumbnail($post->ID,'thumbnail');
$title = get_the_title();
$output .= '<div class="categoryThumbnailList_item">';
$output .= '' .$thmb . '<br/>';
$output .= '' .$title . '';
$output .= '</div>';
} else {
$output .= '<p>Coming Soon</p>';
}
endforeach;