I've been trying to get this seemingly easy peace of code to work.
I'm loading rss from a wordpress site and it all works fine except for the thumbnails. Since in the XML they are set as an attribute instead of a nodeValue i can't seem to get import them. (i've really tried a lot)
$rss = new DOMDocument();
$rss->load('http://goalprogramme.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
// in XML it looks like <media:thumbnail url="http://goalprogramme.files.wordpress.com/2014/01/dsc_0227.jpg?w=150"/>
//echo $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url');
//push items
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'thumbnail' => $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url') // this line doesn't work !!!
);
array_push($feed, $item);
}
Any help would be greatly appreciated.
Thanks so much in advance!
Hours later i've created another piece of code that does work. If anyone needs it it, here it is:
$feed_array = array();
$feed = simplexml_load_file('http://goalprogramme.wordpress.com/feed/');
foreach ($feed->channel->item as $item) {
$title = (string) $item->title;
$description = (string) $item->description;
$link = (string) $item->link;
$date = (string) $item->date;
if ($media = $item->children('media', TRUE)) {
if ($media->thumbnail) {
$attributes = $media->thumbnail->attributes();
$thumbnail = (string)$attributes['url'];
}
}
$item = array (
'title' => $title ,
'desc' => $description,
'link' => $link,
'date' => $date,
'thumbnail' => $thumbnail
);
array_push($feed_array, $item);
}
Related
The following lists all of the terms, can I get help revising it so that it shows all terms except the the active/current page? Thank you.
$terms = get_terms( 'topics', array(
'orderby' => 'name',
'order' => 'ASC',
));
if ( ! empty( $terms ) ){
foreach ( $terms as $term ) {
$term_thumb = get_field('image', $term);
echo '<li><img src="' .$term_thumb['url']. '"><span class="model">'.$term->name .'</span></li>';
}
}
You can do something like this:
// create an empty array holder
$current_tax_ids = array();
// get the post terms
$current_tax = get_the_terms( $post->ID, 'topics' );
// creating loop to insert ids
foreach( $current_tax as $tax ) {
$current_tax_ids[] = $tax->term_id;
}
$args = array(
'taxonomy' => 'topics',
'hide_empty' => 0,
'exclude' => $current_tax_ids // exclude the terms
);
// get all the terms
$all_terms = get_terms($args);
// now do whatever you want
so if you follow my comments it should be clear, but basically you want to get the current post terms and store the id in an array, then simply exclude the ids when you do get_terms .
All,
I'm running a search with Wordpress and it's returning ACF data. The problem, is that for fields that are hidden with conditional logic, they are still returning in the search results.
Does anyone happen to know how to exclude fields that are hidden? Or, does anyone know those items are declared as hidden in the database? I can't seem to find anything that ties a field to another in the wp_postmeta.
EDIT: added some code by request
<?php
error_reporting(0);
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
}
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$search = new WP_Query(
$search_query,
array(
'post_type' => 'page',
'post_status'=>'publish',
'post__in' => array(),
//'posts_per_page' => -1,
'paged' => $paged
)
);
$resultArray = array();
if (have_posts()){
while(have_posts()): the_post();
//places the WP post meta data into an array
$array = get_post_meta($post->ID);
//Flattens the array into one-dimensonal
$result = call_user_func_array('array_merge', $array);
//changes the search term to all lower case
$searchword = strtolower($search_query['s']);
//Searches the $result array for the $searchword and saves all occurances into $matches array
$matches = array_filter($result, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", strip_tags($var)); });
//Grab the first value of the $matches array since we won't always know the key
$first_value = reset($matches);
if(!empty($first_value)){
array_push($resultArray,
array(
'id'=>$post->ID,
'match' =>strip_tags($first_value)
)
);
}
endwhile;
}
$total_results = count($resultArray);
?>
I would like to get all the taxonomy of one post (in a loop), hierarchically. Example I have those taxonomies, and the ID of the tax in bracket.
Tax1(1)
-Tax2(3)
--Tax3(2)
I would like to gather them, in an array maybe, in this order. Right now I manage to get an array of those 3, but the order is wrong. I can't order it by id, since the ID are not ordered at first. I can't also order it by name and slug. (Names of my current taxonomies are not Tax1, Tax2...)
The code I have at the moment is
$args = array('orderby' => 'term_order', 'order' => 'ASC', 'fields' => 'all');
$productcategories = wp_get_object_terms($post->ID, 'guide_type', $args);
Use "Wordpress" Walker class to create a hierarchy of the taxonomy
<?php
class Walker_Quickstart extends Walker {
// Tell Walker where to inherit it's parent and id values
var $db_fields = array(
'parent' => 'parent',
'id' => 'term_id'
);
/**
* At the start of each element, output a <p> tag structure.
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= sprintf( "\n<p>%s %s (%s)</p>\n",
str_repeat('‐', $depth),
$item->name,
$item->term_id
);
}
}?>
This class will create a hierarchy of elements. Use this class with your returned elements like this :
$args = array('orderby' => 'term_order', 'order' => 'ASC', 'fields' => 'all');
$productcategories = wp_get_object_terms($post->ID, 'guide_type', $args);
$walk = new Walker_Quickstart();
echo $walk->walk($productcategories, 0);
Was about to get something with this function that I made, But Vikash Kumar gave me a better answer, thanks !
function get_term_top_most_parent($post_id, $taxonomy){
$return = array();
$registeredcat = 0;
$newparent = '';
$catcount = 0;
$firstlevels = wp_get_object_terms( $post_id, $taxonomy); //post id, taxo, args
foreach ($firstlevels as $key => $value){
if($value->parent == 0 ){
//$firstlevel = $value->term_id; //23
$newparent = $value->term_id;
array_push($return, $value);
$registeredcat += 1;
}
$catcount += 1;
}
return $return;
}
I need to show list of all nodes using EntityFieldQuery() with pagination. And i have applied all condition to get my result and it is working fine. But some how pagination not working. I search on google and applied all solutions but its not working. I am getting the result but pagination not displaying. Please Help.
Here is my Code.
$output = array();
$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle', 'article')
->pager(1);
$results = $query->execute();
if (isset($results['nodes'])) {
$content = node_view_multiple(node_load_multiple(array_keys($results['nodes'])));
$output = array(
'content'=> $content,
'pager'=> array(
'#markup' => theme('pager'),
'#weight' => 10
)
);
}
print render($output);
Thanks,
Sunil
I think the isset() should be done on 'node' instead of 'nodes'. Same goes for your $content var. Instead of print render($output); use it as the return value. Like return $output;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle','article')
->pager(1);
$results = $query->execute();
$output = array();
if (isset($results['node'])) {
$content = node_view_multiple(node_load_multiple(array_keys($results['node'])));
$output = array(
'content'=> $content,
'pager'=> array(
'#markup' => theme('pager'),
'#weight' => 10
)
);
}
return $output;
I wrote a plugin, which reads csv files and create new products. The plugin works when I create only one product but when I add while in Insert() the plugin doesn't work. I want to create all products first off. Maybe it's something related to the add_action... Please help.
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
function CreateProduct($line) {
$data = explode('";"', $line);
define(POST_NAME, $data[2]);
define(cena_netto_pw, $data[5]);
$post = get_page_by_title( POST_NAME, 'OBJECT', 'product' );
$product_ID = $post->ID;
$post_data = get_post($product_ID);
function hbt_create_post() {
$my_post = array(
'post_title' => POST_NAME,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' =>'product'
);
$product_ID = wp_insert_post( $my_post );
}
if(!isset($post))
hbt_create_post();
return $error_obj;
}
function Import() {
$file = PLUGIN_DIR.'test.csv';
$open = fopen($file, 'r');
while (!feof($open)) {
$line = fgets($open);
CreateProduct($line);
}
fclose($open);
}
add_action('admin_init', 'Import' );
?>
While loop code
while (!feof($open)) { $line = fgets($open); CreateProduct($line); }
This code doesn't work. It works when there is only
$line = fgets($open); CreateProduct($line);
try
fgetcsv($open)
instead
fgets($open)