How to truncate wp_rss - php

does anyone know the best way to truncate an rss feed using wp_rss? I tried googling and couldn't seem to find anything. Any help would be appreciated!
Here is my code:
<?php
include_once(ABSPATH . WPINC . '/rss.php');
$feed = 'http://miletich2.blogspot.co.uk/feed/';
$rss = fetch_feed($feed);
?>
<div class="container">
<div class="right">
<div class="rss-container">
<div class="rss-heading">
<?php
$title = "Clashing With Life";
$description = 'This is a blog written by an autistic person for other autistic people about <br>some of the biggest issues in life, whether deplorable or marvelous.';
echo '<h3 class="text-center">' . $title . '</h3>';
echo '<p class="">' . $description . '</p>';
?>
</div>
<div class="text-center">
<?php
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
if ($rss_items):
echo "<ul>\n";
foreach ( $rss_items as $item ) :
//instead of a bunch of string concatenation or echoes, I prefer the terseness of printf
//(http://php.net/manual/en/function.printf.php)
printf('<div class=""><li>%s<p>%s</p><p>%s</p></li></div>',$item->get_permalink(),$item->get_title(),$item->get_date(get_option('date_format')),$item->get_description() );
'<hr>';
endforeach;
echo "</ul>\n";
endif;
endif;
?>
</div>
</div>
</div>
</div>

You can use wp_trim_words() and wp_strip_all_tags()
Example:
if ( !is_wp_error($rss) ) :
$maxitems = $rss->get_item_quantity(3);
$rss_items = $rss->get_items(0, $maxitems);
if ($rss_items):
foreach ($rss_items as $item) :
$item_title = esc_attr( $item->get_title() );
$item_permalink = esc_url( $item->get_permalink() );
$item_post_date = $item->get_date( get_option('date_format') );
$item_excerpt = wp_trim_words( wp_strip_all_tags( $item->get_description(), true ), 55 );
echo sprintf('<div class="">%s<p>%s</p><p>%s</p></div>', $item_permalink, $item_title, $item_post_date, $item_excerpt);
endforeach;
endif;
endif;
Btw, include_once(ABSPATH . WPINC . '/rss.php'); is unnecessary. You should remove it.

Related

Trying to apply a specific css class : php variable / foreach and echo

I am trying to modify a portfolio template in a Wordpress theme. I'd like to apply a specific css class to a specific category of the portfolio, using its ID.
I've tried many things thanks to the different answers already brought here about conditional css (How to apply a CSS class depending on a value contained in PHP variable?), php variables, or about the use of the " foreach", but I wasn't able to find a solution :
May be I didn't put my condition at the right place, or may be is it because of the use of the foreach, or may is there a misplaced tag??
The result remains the same, a full blank page.
I hope you'll have few time to have a look on this and give me a little help.
Here is the original code extract :
<?php $terms_array = array(); $id = $cat = null; ?>
<?php foreach ( $p_terms as $term ) { ?>
<?php $terms_array[$term->term_id] = array( 'name' => $term->name ); ?>
<?php } ?>
<?php if ( count( $terms_array ) > 0 ) { ?>
<div class="row">
<div class="col-md-12">
<ul id="ef-portfolio-filter" class="list-inline text-center">
<?php if ( count( $terms_array ) > 1 ) { ?>
<li class="cbp-filter-item-active"><a href="#" data-filter="*"><?php _e( 'All', EF_TDM ) ?>
<span class="cbp-filter-counter"></span>
</a>
</li>
<?php } ?>
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php echo '<li' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>'; ?>
<?php } ?>
</ul><!-- #ef-portfolio-filter -->
</div><!-- .col-md-12 -->
</div><!-- .row -->
<?php } ?>
<?php } ?>
And here is one of my tries. I just paste an extract as I didn't change anything above it and just introduce my condition after the foreach :
<?php $cur = count( $terms_array ) == 1 ? ' class="cbp-filter-item-active"' : ''; ?>
<?php foreach ( $terms_array as $id => $cat ) { ?>
<?php
if ($id = myspecific id)
{echo '<li class="cbp-filter-item-custom"' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter-custom"></span></a></li>';
}else{
echo '<li ' . $cur . '><a data-filter=".cbp-filter-' . $id . '" href="' . esc_url( get_term_link( $id, EF_CPT_TAX_CAT ) ) . '">' . $cat['name'] . '<span class="cbp-filter-counter"></span></a></li>' } ?>
</ul><!-- #ef-portfolio-filter -->

How to concatenate if statement into a string?

Here is my code on one of my templates...
<?php while ( have_posts() ) :
the_post();
the_content();
query_posts( 'posts_per_page=5' );
endwhile; ?>
<ul id="on-going">
<?php $terms = get_terms(
'series', array(
'orderby' =>'name'));
foreach( $terms as $t ){
$t_id = $t->term_id; $series = get_option("taxonomy_$t_id");
if( $series['status_term_meta'] != 1 ) continue; ?>
<li>
<a title="See all <?php echo $t->name; ?> Episodes" href="<?php echo home_url(); ?>
/series/<?php echo $t->
slug; ?>">
<?php echo( strlen($t->
name) > 25)?substr( $t->name, 0, 25 ) . '...':$t->name; ?> </a>
<?php if(is_subbed($t->slug)) {
echo'<span class="subbedcolor small rightside"> SUBBED </span> ';
}
if(is_dubbed($t->slug)){
echo'<span class="dubbedcolor small rightside"> DUBBED </span> ';
}
?>
I want to take the part
<?php if(is_subbed($t->slug)) {
echo'<span class="subbedcolor small rightside"> SUBBED </span> ';
}
if(is_dubbed($t->slug)){
echo'<span class="dubbedcolor small rightside"> DUBBED </span> ';
}
and include it into my other template so it will show if an episode is subbed, or dubbed:
Here is the string for the other code:
$page = $page . "<li><a style='display:block;' title='" . $name . "' href='" . home_url(). "/series/" .$term->slug . "'>" . $name ."</a></li>";
How would I include the subbed or dubbed part after the closing li tag? I tried to concatenate the if statement, but I can't seem to get it to work.
Thanks so much if anyone can figure it out :D
You said that concatenation didn't work. What did you try and what didn't work?
Try this:
if(is_subbed($t->slug)) {
$page = $page.'<span class="subbedcolor small rightside"> SUBBED </span> ';
}
if(is_dubbed($t->slug)){
$page = $page.'<span class="dubbedcolor small rightside"> DUBBED </span> ';
}

Why I cannot just change the a href to a hardcoded url link?

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>'; };
?>

Trouble creating checkbox array with 2 items being checked by default in PHP

Im working with a Wordpress theme.
The code I am having trouble with is relevant to a filter divided into "action" and "categories".
I would like to have 2 checkboxes checked by default (1 action & 1 category) and the rest blank.
I'm at a complete loss, any help would be sincerely appreciated.
here is the code:
<?php
$icons = array();
$taxonomy = 'property_action_category';
$tax_terms = get_terms($taxonomy);
$taxonomy_cat = 'property_category';
$categories = get_terms($taxonomy_cat);
// add only actions
foreach ($tax_terms as $tax_term) {
$icon_name = 'wp_estate_icon'.$tax_term->slug;
$icons[$tax_term->slug] = esc_html( get_option($icon_name) );
}
// add only categories
foreach ($categories as $categ) {
$icon_name = 'wp_estate_icon'.$categ->slug;
$icons[$categ->slug] = esc_html( get_option($icon_name) );
}
?>
<div class="gmap_wrapper <?php
if (!is_front_page()) {
print 'gmap_not_home" style="height:295px;"';
}else{
print'"';
}
?>>
<div class="gmap-next <?php if (is_front_page()) print 'is-front'; ?>" id="gmap-next" > </div>
<div class="gmap-prev <?php if (is_front_page()) print 'is-front'; ?>" id="gmap-prev" > </div>
<?php
$geo_status = esc_html ( get_option('wp_estate_geolocation','') );
$custom_image = esc_html( esc_html(get_post_meta($post->ID, 'page_custom_image', true)) );
$rev_slider = esc_html( esc_html(get_post_meta($post->ID, 'rev_slider', true)) );
if($geo_status=='yes' && $custom_image=='' && $rev_slider==''){
print' <div id="mobile-geolocation-button"></div>';
}
?>
<div id="googleMap" <?php
$home_small_map_status= esc_html ( get_option('wp_estate_home_small_map','') );
if (!is_front_page() || ( is_front_page() && $home_small_map_status=='yes' ) ) {
print 'style="height:295px;"';
}
?>
</div>
<div class="tooltip"> <?php _e('click to enable zoom','wpestate');?></div>
<div id="gmap-loading"><?php _e('Loading Maps','wpestate');?>
<span class="gmap-animation">
<img src="<?php print get_template_directory_uri();
?>
</span>
</div>
<?php
////////////////////////// enable /disable map filters
$show_filter_map_status = esc_html ( get_option('wp_estate_show_filter_map','') );
$home_small_map_status = esc_html ( get_option('wp_estate_home_small_map','') );
if($show_filter_map_status!='no'){
?>
<div class="gmap-menu-wrapper" >
<div class="gmap-menu" id="gmap-menu" <?php if (!is_front_page() || (is_front_page() && $home_small_map_status=='yes') ) print 'style="display:none;"'; ?> >
<div id="closefilters"></div>
<div class="action_filter" >
<?php
foreach ($tax_terms as $tax_term) {
print '<div class="checker"><input type="checkbox" checked="checked" name="filter_action[]" id="'.$tax_term->slug.'" class="'.$tax_term- >slug.'" value="'.$tax_term->name.'"/><label for="'.$tax_term->slug.'"><span></span>';
if( $icons[$tax_term->slug]!='' ){
print '<img src="'.$icons[$tax_term->slug].'" alt="'.$tax_term->name.'">' . $tax_term->name . '</label></div>';
}else{
print '<img src="'.get_template_directory_uri().'/css/css- images/'.$tax_term->slug.'icon.png" alt="'.$tax_term->name.'">' . $tax_term->name . '</label></div>';
}
}
?>
</div>
<div class="type-filters">
<?php
foreach ($categories as $categ) {
print '<div class="checker"><input type="checkbox" checked="checked" name="filter_type[]" id="'.$categ->slug.'" class="' . $categ->slug . '" value="' . $categ->name . '"/><label for="' . $categ->slug. '"><span></span>';
if( $icons[$categ->slug]!='' ){
print' <img src="'.$icons[$categ->slug].'" alt="'.$categ->slug.'">' . $categ->name . '</label></div>';
}else{
print' <img src="'.get_template_directory_uri().'/css/css-images/'.$categ->slug.'icon.png" alt="'.$categ->name.'">' . $categ->name . '</label></div>';
}
}
?>
</div>
</div>
</div>
<?php
}
?>
</div> `enter code here`
Thanks
Kyle

Dynamic Bootstrap Tabs using PHP/MySQL

So I've been tackling this problem for a while now and I can't seem to get it to work. I have a category table and a link in my database. I'm trying to display the "category" title as the tab and the "link" as my tab content.
Let me share my code and I'll explain the problem:
<ul class="nav nav-tabs" id="lb-tabs">
<?php $sqlCat = $db->query('SELECT `tab_title` FROM `category`'); ?>
<?php
foreach ($sqlCat as $row):
echo '<li><a href="#' . $row['tab_title'] . '" data-toggle="tab">' .
$row['tab_title'] . ' </a></li>';
endforeach;
?>
</ul>
<div class="tab-content">
<?php foreach ($sqlCat as $row2):
$tab = $row2['tab_title'];?>
<div class="tab-pane active" id="<?php $row['tab_title']; ?>">
<div class="links">
<ul class="col">
<?php
$items = $db->prepare('SELECT u_links.title, u_links.link, u_links.tid, category.id, category.tab_title
FROM u_links, category
WHERE category.id = u_links.tid
ORDER BY category.id ');
$items->execute();
while ($r = $items->fetch(PDO::FETCH_ASSOC)) {
echo '<li>' . $r['title'] . '</li>';
}
?>
</ul>
</div>
</div><!-- /tab-pane -->
<?php endforeach; ?>
</div>
This current code is not displaying the content in the "tab-content" div. I've tried different ways like this for example:
$tab = '';
$content = '';
$link = '';
$tab_title = null;
while($row = $items->fetch(PDO::FETCH_ASSOC)) {
$link = '<li>' . $row['title'] . '</li>';
if ($tab_title != $row['tab_title']) {
$tab_title = $row['tab_title'];
$tab .= '<li><a href="#' . $row['tab_title'] . '" data-toggle="tab">' .
$row['tab_title'] . ' </a></li>';
$content .= '<div class="tab-pane active" id="' . $row['tab_title'] . '"><div
class="links"><ul class="col">' . $link . '</ul></div></div><!-- /tab-pane //
support -->';
}
}
With this code I either get as too many tabs(as many items within the category) which I only want one tab for many items(links). Or I'll only get one link per section and doesn't output that row from the database.
If anyone can help me out with this, it will be much appreciated! :) Thank you.
Ok, so I think the issue is how you set your .tab-pane id's. Right now there is but no "echo" so nothing is being output there.
I put together a working demo, I did change some part of your code, but very minor stuff which I tried to comment:
<!-- START OF YOUR CODE -->
<ul class="nav nav-tabs" id="lb-tabs">
<?php
// I just made an array with some data, since I don't have your data source
$sqlCat = array(
array('tab_title'=>'Home'),
array('tab_title'=>'Profile'),
array('tab_title'=>'Messages'),
array('tab_title'=>'Settings')
);
//set the current tab to be the first one in the list.... or whatever one you specify
$current_tab = $sqlCat[0]['tab_title'];
?>
<?php
foreach ($sqlCat as $row):
//set the class to "active" for the active tab.
$tab_class = ($row['tab_title']==$current_tab) ? 'active' : '' ;
echo '<li class="'.$tab_class.'"><a href="#' . urlencode($row['tab_title']) . '" data-toggle="tab">' .
$row['tab_title'] . ' </a></li>';
endforeach;
?>
</ul><!-- /nav-tabs -->
<div class="tab-content">
<?php foreach ($sqlCat as $row2):
$tab = $row2['tab_title'];
//set the class to "active" for the active content.
$content_class = ($tab==$current_tab) ? 'active' : '' ;
?>
<div class="tab-pane <?php echo $content_class;?>" id="<?php echo $tab; //-- this right here is from yoru code, but there was no "echo" ?>">
<div class="links">
<ul class="col">
<?php
// Again, I just made an array with some data, since I don't have your data source
$items = array(
array('title'=>'Home','tab_link'=>'http://home.com'),
array('title'=>'Profile','tab_link'=>'http://profile.com'),
array('title'=>'Messages','tab_link'=>'http://messages.com'),
array('title'=>'Settings','tab_link'=>'http://settings.com'),
array('title'=>'Profile','tab_link'=>'http://profile2.com'),
array('title'=>'Profile','tab_link'=>'http://profile3.com'),
);
// you have a while loop here, my array doesn't have a "fetch" method, so I use a foreach loop here
foreach($items as $item) {
//output the links with the title that matches this content's tab.
if($item['title'] == $tab){
echo '<li>' . $item['title'] . ' - '. $item['tab_link'] .'</li>';
}
}
?>
</ul>
</div>
</div><!-- /tab-pane -->
<?php endforeach; ?>
</div><!-- /tab-content -->
<!-- END OF YOUR CODE -->
This help me a lot. I have two static tabs for content creation which drive the dynamic tabs. It is definitely still a little rough but at least the concept is working.
<ul class="nav nav-tabs">
<li class="active">Clusters</li>
<li>Activities</li>
<?php
$sql = "<insert query here>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $rowtab = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$tab_class = ($rowtab['tab_title']==$current_tab) ? 'active' : '' ;
$nospaces = str_replace(' ', '', $rowtab['tab_title']);
echo '<li class="'.$tab_class.'"><a href="#' . urlencode($nospaces) . '" data-toggle="tab">' .
$rowtab['tab_title'] . '</a></li>';
}
?>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
Tab1 Content
</div>
<div class="tab-pane" id="tab2">
Tab2 Content
</div>
<?php
$sql = "<insert query here>";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $rowtab = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$tab = $rowtab['tab_title'];
$nospaces = str_replace(' ', '', $rowtab['tab_title']);
$content_class = ($tab==$current_tab) ? 'active' : '' ;
echo '<div class="tab-pane'. $content_class.'" id="'.$nospaces.'">';
echo 'You are looking at the '.$tab.' tab. Dynamic content will go here.';
echo '</div><!-- /.tab-pane -->';
}
?>
</div>
foreach ($files as $file):
$filename = preg_replace("/\.html$/i", "", $file);
$title = preg_replace("/\-/i", " ", $filename);
$documentation = 'usage/'.$type.'/'.$file;
$tab1 = 'usage/'.$type.'/'.$file;
echo '<div class="sg-markup sg-section">';
echo '<div class="sg-display">';
echo '<h2 class="sg-h2"><a id="sg-'.$filename.'" class="sg-anchor">'.$title.'</a></h2>';
//TAb Set up
echo '<div class="row"><div class="col-md-12">';
echo '<ul class="nav nav-tabs" role="tablist">';
echo '<li role="presentation" class="active">Visual</li>';
echo '<li role="presentation">Rules</li>';
echo '</ul>';
echo '</div>';
echo '</div>';
//Visual Tab Content
echo '<div class="row"><div class="col-md-12">';
echo '<div class="tab-content">';
echo '<div role="tabpanel" class="tab-pane active" id="Visual">';
echo '<h3 class="sg-h3">Visual</h3>';
include('markup/'.$type.'/'.$file);
//View Source
echo '<div class="sg-markup-controls"><a class="btn btn-primary sg-btn sg-btn--source" href="#">View Source</a> <a class="sg-btn--top" href="#top">Back to Top</a> </div>';
echo '<div class="sg-source sg-animated">';
echo '<a class="btn btn-default sg-btn sg-btn--select" href="#">Copy Source</a>';
echo '<pre class="prettyprint linenums"><code>';
echo htmlspecialchars(file_get_contents('markup/'.$type.'/'.$file));
echo '</code></pre>';
echo '</div><!--/.sg-source-->';
echo '</div>';
//Documentation Tab Content
if (file_exists($documentation)) {
echo '<div role="tabpanel" class="tab-pane" id="Rules">';
echo '<h3 class="sg-h3">Rules</h3>';
include($documentation);
//View Source
echo '<a class="sg-btn--top" href="#top">Back to Top</a>';
echo '</div>';
}
//Documentation Tab1 Content
echo '<div role="tabpanel" class="tab-pane" id="Tab1">';
echo '<h3 class="sg-h3">Tab1</h3>';
include($tab1);
echo '<a class="sg-btn--top" href="#top">Back to Top</a>';
echo '</div>';
//Documentation Tab2 Content
echo '<div role="tabpanel" class="tab-pane" id="Tab2">';
echo '<h3 class="sg-h3">Tab2</h3>';
// include($tab2);
echo '<a class="sg-btn--top" href="#top">Back to Top</a>';
echo '</div>';
echo '</div>'; //End Tab Content
echo '</div>'; //End Column
echo '</div>'; //End Row
//echo '<div class="row"><div class="col-md-12">';
//echo '<h3 class="sg-h3">Example</h3>';
//include('markup/'.$type.'/'.$file);
//echo '</div>';
// if (file_exists($documentation)) {
// echo '<div class="col-md-12"><div class="well sg-doc">';
// echo '<h3 class="sg-h3">Rules</h3>';
// include($documentation);
// echo '</div></div></div>';
// }
echo '</div><!--/.sg-display-->';
//echo '</div><!--/.colmd10-->';
echo '</div><!--/.sg-section-->';
endforeach;
}

Categories