I have a view for a blog page that has the basic fields associated such as title, body, post-date, image, and author. What I need is a way to get access to the fields of the view to show on parts of the site with out creating a page or block. I just need the view to organize my information and will be using jQuery to parse the data.
2 Answers
Sounds like you could use views_get_view_result().
views_get_view_result($name, $display_id = NULL)
Get the result of a view.
Also take a look at this answer for a more complete solution.
-
that just gives me an array. Cool thanks but does that give me the raw data as well as in this case I would need a specific photo and date... Commented Dec 12, 2012 at 14:50
-
Just as with any programmatic method, you're almost always going to end up with either an array or an object from which you must extract the values you want for display.– bethCommented Dec 12, 2012 at 15:44
It sounds like you may want to use an Entity Field Query
To do something like gather the nodes tagged with taxonomy term of tid = 2 in field_your_taxonomy_field
, do something like the following:
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('status', 1)
->fieldCondition('field_your_taxonomy_field', 'tid', 2, '=')
->execute();
This returns a list of IDs (in this case nid
s) of entities that meet the conditions specified. To get info beyond the ID, you would load the entity and extract the value, with something like:
foreach($results['node'] as $result) {
$nid = ($result->nid);
$node = node_load($nid);
print($node->title);
}
To be clear, this cannot load information from a view that has already been created, but it can be used instead of a view. This can be good if you don't need the view for purposes other than the fields you're trying to display right now or you need to display fields other than the way your view does.
-
This is heavier than doing a db_select and might be a bad choice if you're getting only a small number of fields from a large number of nodes, due to the node_load(). It's most useful if you're getting one or more fields from only a few nodes at a time.– bethCommented Dec 11, 2012 at 17:52
-
-
thanks I see another use for this... but essentially, for this task, I don't need the whole node. Hopefully, I'm not reading this wrong but I need parts of the content (the fields) such as the photo and the title or a part of the body trimmed with quotations. Commented Dec 12, 2012 at 15:04
-
You don't have to display the whole node. The performance issue has more to do with how many nodes you want to get info for at once than how much of the node you're displaying. If you're getting fields from multiple nodes, you might want to use
node_load_multiple()
instead ofnode_load()
ordb_select
instead of an EFQ. Choosing which meets your needs best is another question.– bethCommented Dec 12, 2012 at 15:38