Calling a field inside included feed - feed

I'm trying to do different feeds into one area that I switch with jquery tabs. I have a feed template and only need to switch the rss feed url inside of it each time. I tried to set a value to the url on my index page and then echo it inside the feed but I'm obviously not doing this right. Whats wrong with my approach?
<----Index Page--->
<div class="news-feed"><?php $url = "http://mysite.com/category/news/feed/"; ?><?php locate_template( array( 'feeds/news-feed.php' ), true ) ?></div>
<-------FEED-------->
<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed('$url');<---WHERE I NEED MY CUSTOM URL
if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity(5);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
endif;
?>
<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo esc_url( $item->get_permalink() ); ?>'
title='<?php echo esc_html( $item->get_title() ); ?>'>
<?php echo '<img src="' .get_first_image_url($item->get_content()). '"/>'; ?>
<?php echo esc_html( $item->get_title() ); ?></br>
<span style="color:#e2e2e2"><?php echo shorten( $item->get_description (),140 ); ?></span></a>
</li>
<?php endforeach; ?>
</ul>

You need to change
fetch_feed('$url');
to
fetch_feed($url);
Single quoted strings in PHP do not have variables inside them expanded.

Related

Showing custom fields of custom post type in a date ordered list on a page

I'm struggling with this. I add multiple custom field values: in this case event dates with their title and location to all my projects (custom post type) and want to display them in a table together on a page called Calendar. I tried to query the posts but then the custom fields will show as groups together, and not as a date ordered list.
The event titles match the post title in this case.
My question is: what would be the best solution? Creating all the event dates as custom fields within the posts and then show them all together on a single page called calendar, or create them all in a calendar page and then filter those on title to show only the relevant ones in a single post that match the page title?
The event name is taken from a post object, which seemed the best solution for the scenario where all events are created on the same page, but other wise I might use just the post title.
Hope there is someone to help!
My code for the calendar (the part with upcoming dates):
<div class="Rtable upcoming Rtable--collapse">
<?php $outs = array(); if( have_rows('events') ): ?>
<?php while ( have_rows('events') ) : the_row(); ob_start();
// Get sub field values.
$date = get_sub_field('date');
$location = get_sub_field('location');
$website = get_sub_field('website');
$title = get_sub_field('title');
$premiere = get_sub_field('premiere');
$event = get_sub_field('date', false, false);
$today = strtotime(date('Ymd'));
$upcoming = strtotime($event);
$datefin = new DateTime($event); ?>
<?php if ($upcoming >=$today): ?>
<div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">
<?php if( get_sub_field('date') ): ?><?php the_sub_field('date'); ?><?php endif; ?>
</div>
<div class="Rtable-cell table-project-title"><?php if( get_sub_field('premiere') == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif; ?>
<?php if( $title ): ?><?php foreach( $title as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?><?php the_title(); ?> <?php endforeach; ?> <?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?> <?php endif; ?>
</div>
<div class="Rtable-cell"><?php if( get_sub_field('location') ): ?><?php the_sub_field('location'); ?><?php endif; ?></div>
<div class="Rtable-cell Rtable-cell--foot"><?php if( get_sub_field('website') ): ?><a class="button" href="<?php echo esc_url( $website ); ?>" target="_blank">Tickets</a> <?php endif; ?></div>
<?php endif; ?>
<?php $outs[] = ob_get_clean(); endwhile; ?>
<?php
else :
endif;
$outs = array_reverse($outs);
echo implode($outs);
?>
</div><!-- UPCOMING END -->
I found the solution, to query by the custom field date. Result here:
<?php
/**
* Template Name: Calendar
*/
get_header();
?>
<?php get_header();
?>
<div class="content-header yellow">
<h1><?php the_title();
?></h1>
</div>
<!-- Kalender -->
<div class="yellow content">
<div class="Rtable upcoming Rtable--collapse">
<?php
// Create an empty array for storage
$post_data = array();
// Set arguments for your query
$args = array(
'post_type' => 'projects',
'posts_per_page' => -1
);
// Do our query with any arguments from above
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Setup our repeater
if ( have_rows( 'events' ) ):
while ( have_rows( 'events' ) ) : the_row();
// Optional - Only get posts that match a true false within our repeater
$event = get_sub_field( 'date', false, false );
$today = strtotime( date( 'Ymd' ) );
$upcoming = strtotime( $event );
$datefin = new DateTime( $event );
if ( $upcoming >= $today ) {
// Build our array with the data we need using key => value
$post_data[] = array(
'url' => get_the_permalink(),
'title' => get_the_title(),
'date' => get_sub_field( 'date' ),
'location' => get_sub_field( 'location' ),
'website' => get_sub_field( 'website' ),
);
}
endwhile;
endif;
}
} ?>
<?php // Custom function to use in our sort to list the items by date
function subval_sort( $a, $b ) {
if ( $a['date'] == $b['date'] )
return 0;
return $a['date'] < $b['date'] ? -1 : 1;
}
// Sort our multidimensional array by sub array value
usort( $post_data, 'subval_sort' );
// We can now work our data normally through easy to access methods
foreach ( $post_data as $post ) {
?>
<div class="Rtable-cell Rtable-cell--head <?php if( get_sub_field('premiere') == 'yes' ): ?>premiere <?php endif; ?>">
<?php $format_in = 'm/d/Y';
// the format your value is saved in ( set in the field options )
$format_out = 'd/m/Y';
// the format you want to end up with
$date = DateTime::createFromFormat( $format_in, $post['date'] );
echo $date->format( $format_out ) . ' ' ;
?>
</div>
<div class="Rtable-cell table-project-title"><?php if ( get_sub_field( 'premiere' ) == 'yes' ): ?><span style="color: var(--magenta);">premiere:</span> <?php endif;
?>
<a href="<?php echo $post['url']; ?> ">
<?php
echo $post['title'] . ' ' ;
?>
</a>
</div>
<div class="Rtable-cell">
<?php echo $post['location'] . ' ' ;
?>
</div>
<div class="Rtable-cell Rtable-cell--foot">
<a class="button" href="<?php
echo $post['website']; ?> ">
tickets
</a>
</div>
<?php }
?>
</div>
</div>
<?php get_footer();
?>

wp shortcode breaks get_post_thumbnail

I'm having some trouble with short code's breaking a featured image I have set on my home page in Wordpress (4.0). For some reason, when I paste in my shortcode in the wp-admin > page > edit > content for the home page, suddenly my featured image stops working. It's working until I paste in the shortcode and update the page, but then the image disappears and the_post_thumbnail() returns false. I've also tried get_the_post_thumbnail() without success.
Here's my code excerpt from "front-page.php":
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php if (has_post_thumbnail()) {the_post_thumbnail();} ?>
</div>
And here's the shortcode function from "functions.php":
// [random_testimonial]
function short_code_get_random_testimonial(){
$args = array( 'post_type' => 'testimonial', 'posts_per_page' => 1, 'orderby' => 'rand' );
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$random_testimonial = get_the_content();
$author = get_the_title();
$rt = '<p class="quote">' . $random_testimonial;
$rt .= '</p><p class="right">– ' . $author . '</p>';
endwhile;
return $rt;
}
// Register the shortcodes.
add_shortcode( 'random_testimonial', 'short_code_get_random_testimonial' );
// Allow text widgets to contain shortcodes.
add_filter('widget_text', 'do_shortcode');
add_filter('the_content', 'do_shortcode');
Any ideas would be greatly appreciated.
I think I may have solved this one. It seems to have something to do with the fact that I was calling a custom post type with the shortcode, so by the time the_post_thumbnail() was called, the query had changed to refer to the custom post type included in the shortcode ("testimonial") rather than the parent post (in this case the home page). I revised my code as follows to catch the image before the shortcode is called, and all is working again:
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<?php
// go ahead and get the post image in case [random_testimonial]
// shortcode is used in page content which will break the query.
if (has_post_thumbnail()) {$image = get_the_post_thumbnail();} ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php
// Echo post image from above.
if(isset($image)){echo $image;}
?>
</div>

Adding links with active classes

I use the method described bellow to add links with active classes for my CMS pages. The problem is that when I click on any of the links it becomes active, but the class remains even after clicking some of the other links. So the rest of the links don't obtain an active class but just the first opened. Any idea where the problem lies?
<li class="level0 nav-2 parent <?php if (strpos(Mage::helper('core/url')->getCurrentUrl(),'custom') != false ) :?> active<?php endif;?>">
<?php echo $this->__('TEXT OF MY LINK 1') ?>
</li>
<li class="level0 nav-3 parent <?php if (strpos(Mage::helper('core/url')->getCurrentUrl(),'wholesale') != false ) :?> active<?php endif;?>">
<?php echo $this->__('TEXT OF MY LINK 2') ?>
</li>
<li class="level0 nav-4 parent <?php if (strpos(Mage::helper('core/url')->getCurrentUrl(),'faq') != false ) :?> active<?php endif;?>">
<?php echo $this->__('TEXT OF MY LINK 3') ?>
</li>
try changing:
if (strpos(Mage::helper('core/url')->getCurrentUrl(),'custom') != false )
to
if (strpos(Mage::helper('core/url')->getCurrentUrl(),'custom') !== false )
or you can get current CMS page identifier and check accordingly, like
$current_page = Mage::getSingleton('cms/page')->getIdentifier();
//and check & add class
<li class="level0 nav-2 parent <?php if($current_page == 'custom' ):?> active <?php endif;?>">
....
In stead of checking !=false you can directly use below code.
<?php if (strpos(Mage::helper('core/url')->getCurrentUrl(),'custom')) :?> active<?php endif;?>
After updating this, clear all cache from Magento Admin from System->Cache Management
OR
Updated code:
<?php if (Mage::getSingleton('cms/page')->getIdentifier() == 'custom') :?> active<?php endif;?>
Try this, Hope will help!

Place an image for photo form button in Zend form

I have created a form by Zend form that uploads pictures in jpg and png format. however, the form generates a default button and text that reads "no file chosen". How can I place my own designed button image and remove the default text for this form?
$photo = new Zend_Form_Element_File('photo');
$photo->addValidator('Count', false, 1)
->addValidator('Size', false, 200*1024)
->addValidator('Extension', false, 'jpg,png');
It looks like you need to use Zend_Form_Element_Buttom if you want to use an image.
Just make sure you pass in type=submit and you can use the style="" attribute to set your image.
You can pass in these using:
setAttribs(array('type'=>'submit', 'style'=> array('image' => 'image.png')));
I think this syntax is correct, but I'm sure I have the incorrect stlye attributes.
MDN Button Reference
$photo->setDecorators(array(
array('ViewScript', array('viewScript' => 'my_upload_button.phtml', 'abc' => $some_parameter)
);
then inside
my_upload_button.phtml do your own style, here is mine
<?php
$image = $this->element->getAttrib('class');//prendi il nome del file
$this->element->setAttrib('class', '');//cancella la class
?>
<td class="nome_campo">
<label for="<?php echo $this->element->getName(); ?>"><?php echo $this->element->getLabel(); ?></label></td>
<td class="valore_campo" colspan="1">
<?php echo $this->content; ?>
<?php if ($this->element->hasErrors()): ?>
<span class="error">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</span>
<?php endif; ?>
<p class="hint"><?php echo $this->element->getDescription() ?></p>
<b>
<?php

Rendering only the <form> tag

Is there any way that i can render ONLY the start <form> tag of a Zend_Form object?
print $this->registerForm->renderForm();
renders <form></form>, and i only need <form>
Edit:
After Asleys possible solution i wrote this for My_Form class
public function renderFormOpen() {
return str_replace('</form>', '', $this->renderForm());
}
public function renderFormClose() {
return '</form>';
}
Still looking for at ZF way of doing thins, even though i don't think there is any - after going through the code in the ZF library.
You could write an custom form-decorator that uses a custom view-helper that only renders the open form tag. But I think this would be overkill.
Just "hardcode" the form-tags and fill the attributes with the data provided by the form-variable in your view.
<!--in your view-template -->
<form action="<?php echo $this->form->getAction() ?>"
enctype="<?php echo $this->form->getEnctype() ?>"
method="<?php echo $this->form->getMethod() ?>"
id="<?php echo $this->form->getId() ?>"
class="<?php echo $this->form->getAttrib('class') ?>" >
<!--in case your products are represented as elements -->
<?php foreach ($this->form->getElements() as $element): ?>
<?php echo $element ?>
<?php endforeach; ?>
<!--in case your products are represented as displayGroups -->
<?php foreach ($this->form->getDisplayGroups() as $displayGroup): ?>
<?php echo $displayGroup ?>
<?php endforeach; ?>
<!--in case your products are represented as subforms -->
<?php foreach ($this->form->getSubforms() as $subform): ?>
<?php echo $subform ?>
<?php endforeach; ?>
<!--in case your products are rendered by a view helper -->
<?php foreach ($this->products as $product): ?>
<?php echo $this->renderProduct($product) ?>
<?php endforeach; ?>
</form>
Just for fun the overkill way
// Get your products form
$form = new Form_Products();
// Add custom prefix path
$form->addPrefixPath('Foobar_Form_Decorator', 'Foobar/Form/Decorator', 'decorator');
// Set OnlyOpenTagForm-ViewHelper for FormDecorator
$form->getDecorator('Form')->setHelper('OnlyOpenTagForm');
// copy Zend/View/Helper/Form to Foobar/Form/Decorato/OnlyOpenTagForm.php
// In OnlyOpenTagForm.php
// replace Zend_View_Helper_Form with Foobar_View_Helper_OnlyOpenTagForm
// replace method "form" with onlyOpenTagForm"
// replace
if (false !== $content) {
$xhtml .= $content
. '</form>';
}
// with:
if (false !== $content) {
$xhtml .= $content;
}
Done! - The Java-Guys will love it ;)
You can render just the open form tag by passing false to the form decorator like so:
<?php echo $this->form->renderForm(false) ?>
Which will output something like:
<form id="post" enctype="multipart/form-data" method="post" action="/post">
Additonally you can pass a string to the form decorator to be enclosed by the form tags like so:
<?php echo $this->form->renderForm('Some Text') ?>
Which outputs something like:
<form id="post" enctype="multipart/form-data" method="post" action="/simchas/post">Some Text</form>
Hope this helps...
You could do something like this:
echo $this->form->getDecorator('Form')->setElement($this->form)->render(false);