Rss feeds: get certain amount of tweets - rss

Is it possible to give a parameter when getting the RSS feeds to determine how many feeds it should get?
I don't want to load all the RSS feeds, but only the first 20. Is this possible?
Thanks!

You can set the limit. By executing a Loop in limit. So it will parse the xml and your program will read items in loop. Once the loop crossed the limit. Just break the loop.
$i=0;
while ($reader->read()) {
if($i>=10)
break;
else{
switch ($reader->nodeType) {
case (XMLREADER::ELEMENT):
if ($reader->localName == "item") {
$node = $reader->expand();
$dom = new DomDocument();
$n = $dom->importNode($node,true);
$dom->appendChild($n);
$sxe = simplexml_import_dom($n);
$url = (String)$sxe->url;
$title=(String)$sxe->title;
}
}
}
In the above code $i is the limiter. Where we can limit number feed to display in the page.

Related

How to get total rows in Drupal 8 view with pagination?

In Drupal 8, I use a view with pagination. There are 10 items per page.
In hook_views_pre_render(ViewExecutable $view) {..} I want to get the number of items to use in another function. However, $result = 10 and $total_rows = 10 due to the pager setting. The actual item number is between 500 and 2000.
As a workaround I tried a temporary view.
$view2 = Views::getView('my_view');
$view2->setDisplay('page_1');
$view2->setItemsPerPage(15000);
$view2->execute();
This works. view2->total_rows returns the desired value. But I don't want to use a fixed value in setItemsPerPage. 0 or null (for unlimited items per page) returns no result for total_rows.
What can I do to get the correct number of views-items?
In your hook_views_pre_render function, instead of calling view->SetDisplay and view->setItemsPerPage, you can execute the view (even with pager settings) and get the total rows/results:
$view = Views::getView('my_view');
$view->execute('page_1');
$rows = $view->total_rows;
$rows will provide the total count that you are looking for.
And get_total_rows needs to be set to true for total_rows to operate.
$view = Views::getView('my_view');
$view->get_total_rows = TRUE;
$view->execute('page_1');
$rows = $view->total_rows;

How to get total number of rows in a drupal view alter hook?

I've to avoid duplicate search result in a view, so what I am trying to alter the view using pre render hook. and removing the duplicates it's working fine but the problem is in the count of result. it shows the count from the query executed and this include the duplicated item too. also, I enabled the pagination with limit of 5 in a page. then the count seems to be strange it's taking the count of the elements showing in each page
function search_helper_views_pre_render(\Drupal\views\ViewExecutable $view) {
if ($view->id() == "all_news" || $view->id() == "all_publications" || $view->id() == "all_events" || $view->id() == "global_search") {
$unique_nids = $d_nids = $new_results = array();
// Loop through results and filter out duplicate results.
foreach($view->result as $key => $result) {
if(!in_array($result->nid, $unique_nids)) {
$unique_nids[] = $result->nid;
}
else {
unset($view->result[$key]);
}
}
$view->total_rows = count($view->result);
//$view->pager->total_items = count($view->result);
$view->pager->updatePageInfo();
}
}
the expected output of the $view->total_rows must be the total count of result instead of count of elements shown in the page.
You totaly done it in wrong way. as you see ( and it's clear from its name ), it's hook__views_pre_render it runs before the rendering. So its really hard to manipulate the views results and counter, pagination there.
As I see in your query you just remove duplicate Nids , so you can easily do it by Distinct drupal views feature.
Under Advanced, query settings, click on settings.
You will get this popup, now checkmark Distinct
Could do
$difference = count($view->result) - count($new_result);
$view->total_rows = $view->total_rows - $difference;
BTW Distinct setting doesn't always work, see https://www.drupal.org/project/drupal/issues/2993688

Woocommerce: How-to show ‘Order Total’ amount in Words

On my Woocommerce website, I need to display Total Order Amount in words, which will be shown on Checkout Page, Cheque Payment & on Invoice.
Example: 1590.00 (One thousand five hundred & ninety only)
How can we achieve this?
TIA
You can try number formatter class as mentioned in these threads a and b
Use the filter "woocommerce_cart_totals_order_total_html".
function custom_woocommerce_order_amount_total( $order_total ) {
$number_total = WC()->cart->get_total();
// number formatting goes here;
// using number formatter class
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
$total_in_words = $f->format($number_total); // Total in words
$order_total = $total_in_words;
return $order_total;
};
add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_woocommerce_order_amount_total' );
You can also try the other hooks like woocommerce_get_formatted_order_total

How to ignore a custom field value if its empty

I have five custom fields for loading images but all of them are not required. I mean the user can upload a random number of images from 1 to 5. I am stuck in a simple lack of concept here. How should I check if any of them is empty and discard it. More specifically I want to discard the non-existing fields and store only the uploaded ones in an array. Here is my code
$custom_fields = get_post_custom($id);
$my_custom_field1 = $custom_fields['image1'];
$my_custom_field2 = $custom_fields['image2'];
$my_custom_field3 = $custom_fields['image3'];
$my_custom_field4 = $custom_fields['image4'];
$my_custom_field5 = $custom_fields['image5'];
if(!(false===($my_custom_field1))) { $img[]=$my_custom_field1;}
if(!(false===($my_custom_field2))) { $img[]=$my_custom_field2;}
if(!(false===($my_custom_field3))) { $img[]=$my_custom_field3;}
if(!(false===($my_custom_field4))) { $img[]=$my_custom_field4;}
if(!(false===($my_custom_field5))) { $img[]=$my_custom_field5;}
$images = Array("image1","image2","image3","image4","image5");
foreach($images as $image){
if(isset($custom_fields[$image])){
$img[] = $custom_fields[$image];
}
}
Didn't tested that, but should work.
if(isset($my_custom_field5)){
// do something with $my_custom_field5
}
I would suggest using
if ( !empty($my_custom_field)){
\\Do Something
}
because isset only checks for variable beings set and not null where as empty checks if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

WordPress loop by function

I'm creating magazine style theme (not e-commerce) and I want to display 3 latest posts from ex. X, Y and Z category, where this 1st post will be with thumb and other 2 only titles. I found some similar solution themes, but when I look into the code, they created 2 loop for each category (2x3=6) and with this 6 loops code looks very messy. So I decided to create function (ex. latest_post_from_category($cat);) to display this post.
Here comes the question is my decision right, if yes do have any advices to make this function more flexible?
Thanks for your time.
a function can become more flexible with params and switches. example follows
function getPosts($type,$return = false,$amount = 4)
{
switch($type)
{
case 'comments':
//Get latest comments here
break;
case 'posts':
case 'posts-desc':
case 'posts-asc':
if($type == 'posts-asc'){ $order = 'ASC';}else{$order = 'DESC';/*default*/}
//Get posts
break;
/*(etc...etc)*/
}
}
$comments = getPosts('comments',true,5); //5 comments
$posts= getPosts('posts-desc',true,6); //5 Latest
Things like that can really make a design come together.
The Thumbs
In regards to this you only really need the post id and wordpress provide the functions so with my example above you can loop and do an if statement
$i = 0;
foreach(getPosts('post-asc',true,3) as $row)
{
$i++;
if($i == 1)
{
//Show thumb for $row
if(!wct_display_thumb("width:200px;height:150px", $row->ID))
{
//Show title
}
}else
{
//Show title for $row!
}
}

Resources