Is it possible to get a pod items index in the template? For instance, if I have a pods variable that has ten entries in it, could I make a template that renders each entry with its place in the list or, even better yet, if it's first/last as such:
Rendering item {#index} out of {#total} called {#title}
Note: I mean the index in the scope of the group of items being rendered. Not the posts ID or something else. If this doesn't exist it would be a great feature to have!
It's possible with PHP:
Total in this list: <?php echo $obj->total(); ?>
Or you can get the total (across all pages, if using pagination or limiting):
Total Found: <?php echo $obj->total_found(); ?>
Or you can get the current position in the loop (new in Pods 2.3):
Current Position: <?php echo $obj->position(); ?>
Or you can even do an nth check (CSS nerds know what I'm talking about in regards to how nth-child works):
<?php
if ( $obj->nth( 'even' ) )
echo 'even row';
if ( $obj->nth( 'odd' ) )
echo 'odd row';
if ( $obj->nth( '1n+3' ) )
echo 'you get the picture';
if ( $obj->nth( '3n+0' ) )
echo 'you get the picture';
?>
For info about nth, it just takes the same input as nth-child does in CSS: http://www.w3schools.com/cssref/sel_nth-child.asp
If you'd like these available as magic tags, please submit a feature request at http://pods.io/submit/
I have the same requirement. My scenario is that I need to detect first element so that I can display it in a slideshow. Inside template, I declare a global variable:
<?php global $counter;
if ($counter == 0) {
$class='active';
$counter++;
} else {
$class='';
}?>
Then I echo variable $class to where i want.
By this way, I cannot know totally how many row but It sold my problem.
PHP is deprecated in Pods templates. Now you should use Pods Frontier plugin or put PHP code in WP templates.
Related
So I've just installed the Baum package in Laravel and put together a small tree of categories.
I've been able to display the tree in nested JSON format with the getDependentsAndSelf() method, but I have no idea how to go about actually displaying this in a usuable format with laravel.
Ideally I'd just like to spew them out in an indented list format, but I feel like that would probably require some recursion and I have no idea how to do that in Laravel.
Here's the output I have right now:
{"14":{"id":14,"parent_id":null,"name":"Root","lft":1,"rgt":6,"depth":0,"children":[{"id":15,"parent_id":14,"name":"Child 1","lft":2,"rgt":5,"depth":1,"children":[{"id":16,"parent_id":15,"name":"Child 2","lft":3,"rgt":4,"depth":2,"children":[]}]}]}}
Essentially it's just a tree of the format
- Root
- Child 1
- Child 2
So what's the best way to go about this in laravel? In php I could have just made a function that recursed upon itself, but I'm not sure how to do that in my laravel view.
You can use method described here: https://gist.github.com/etrepat/6920301
<?php
$roots = Category::roots()->get();
echo "<ul>";
foreach($roots as $root) renderNode($root);
echo "</ul>";
// *Very simple* recursive rendering function
function renderNode($node) {
echo "<li>";
echo "<b>{$node->name}</b>";
if ( $node->children()->count() > 0 ) {
echo "<ul>";
foreach($node->children as $child) renderNode($child);
echo "</ul>";
}
echo "</li>";
}
I've been running in circles for like ages trying to edit this widget. Had some trouble formating all that code in here so i will just post a link from pastebin - http://pastebin.com/tdYFAgQD.
What i want and can't achieve, is to display all the posts in a category, in this format (a block with the featured image, then the title then the text), one after another. I want to get rid of that list thingy (that displays x number of posts but in the same window) and have all the post displayed like as i said, one after another in separate blocks.
Here's a picture that may make the things clearer:
http://i.imgur.com/pfHbi.jpg
You should read up on http://codex.wordpress.org/The_Loop_in_Action
To get the 4 blocks to show up like that, you'll have to think more about the HTML / CSS that will be generated. Consider simply getting the 4 divs you want to show up by modifying the loop, then go from there.
Beginning at line 105 in your pastebin is the bit producing those "links" so simply remove these lines of code:
<?php if ( $warrior_posts_list_left->post_count > 1) echo '<ul>'; ?>
<?php } else { ?>
<li>
<span class="icon-file"></span> <?php echowarrior_post_title('35'); ?>
</li>
<?php } ?>
<?php $i = $i + 1; endwhile; ?>
<?php if ( $warrior_posts_list_left->post_count > 1) echo '</ul>'; ?>
Further, you may want to ask your question here https://wordpress.stackexchange.com/ or on the support forum for the particular wordpress plugin you're modifying http://wordpress.org/extend/plugins/
I'm trying to get a specific field number in my "views-view-fields--news.tpl.php". Right now it's like this :
<?php foreach ($fields as $id => $field): ?>
<?php print $field->content; ?>
<?php endforeach; ?>
I've tried to do something like this :
<?php print $fields[0]->content; ?>
But it doesn't seem to works and Google doesn't care about my problem.
Can you help me figure this?
You have to use the field name instead.
Try to use
<?php var_dump(array_keys($fields)); ?>
to find the one you need
This is my individual field tpl file name in theme folder
views-view-field--field-fba-value.tpl.php
where "field-fba-value" is my field name. so don't need to use $field variable here and iterate it through loops. I just use $output variable that contain the current value of the field.
Draw back of this approach is if you want to put styling of each field then you need to create those many tpl file. But if you have one or two field to make styling on it then go with this approach.
I am trying to get the index-page of a Wordpress-blog show some very specific posts. As far as I understand i need to use a standard loop in order to make sticky posts work, so custom queries is out of the question. (Correct me if this is wrong.)
All posts are put in a main category (Eg. "Make-Up") In addition, posts that should show on the front page gets an additional category "Frontpage".
The current loop outputs all posts, regardless of category. And styles certain categories differently. An example would be the video-category which is only shown by getting an embed code from a custom field in the post.
<?php elseif (in_category('20')) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
I need to remove all posts not in the category "Frontpage" while still being able to control how posts are being shown.
Earlier i used a filter to control the main loop:
function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '20 27');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
However this would cause my geomashup-plugin to break as it probably uses the same loop?
My current proposal for a solution would be to do something like this, plus functioning code:
<?php elseif (the post is in BOTH category 20 and 27)) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
<?php else : ?>
<div style: display: none;></div>
However i am unsure about how make a condition demanding the post to be in two categories, and i realise this is a terribly dirty fix.
Any tips or pointers as to how i could solve this would be greatly appreciated :)
Front page can be seen here: http://parfymelle.brandbase.no
For anyone wondering i solved it by including the geotagged posts-category (the shop locations) in the filter for the main loop, and then using a php if in the index.php to hide posts from that category. Dirty, but works, i guess.
I'm a drupal newbie...
I researched but couldnot find :/ is there any predefined variable that gives my CCK field value count?
for example; I have field_logo_sponsor and I need to display all logo items. Now I have 5 item
<?php print $node->field_logo_sponsor[0]['view'] ?>
<?php print $node->field_logo_sponsor[1]['view'] ?>
<?php print $node->field_logo_sponsor[2]['view'] ?>
<?php print $node->field_logo_sponsor[3]['view'] ?>
<?php print $node->field_logo_sponsor[4]['view'] ?>
it is stupid to use it that way :/ if there is any count variable for that, I will just create a loop for that and display them in a for or while loop
Appreciate helps! thanks a lot!
How about:
<?php
foreach($node->field_logo_sponsor as $logo_sponsor) {
print $logo_sponsor['view'];
}
?>
Also count($node->field_logo_sponsor) should return you the number of items.
Sidenote: never use
foreach($node->field_logo_sponsor as $logo_sponsor) {
print $logo_sponsor['value'];
}
Even if that calue contains what you want, and the view does not contain the HTML you want. value is unescaped, meaning, it can (and therefore will, at some point) contain stuff like XSS.