Drupal 7: View theming - Get specific field by it's number? - drupal

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.

Related

Conditional custom post type while post ID is - WordPress

How to add content to specific custom post type page?
I am using this code
is_singular()
I wanted to add content to custom post type, but for specific page only.
I just figured it out using get_the_id.
The code is something like this
is_singular('custom-post-slug') && get_the_id() == 123 ): ?>
"custom-post-slug" is the slug of the custom post type
"123" is the page ID
#user2415803 it a bad practice to hardcode ID like this please consider using custom fields or some other checks to get the desired ID.
Can you please provide more information? If you are trying to add content for specific custom post type just use this :
if ( get_post_type() === 'your_post_type_name' ) {
// add the content
}
Please note that you can extend the if condition to be for specific page and so on. Please let me know if you have further questions.
In these cases you can i always add single-{custom-post-type}.php in the theme and define specials html structure for that like following code:
<?php get_header(); ?>
<div class="single-page-customtype">
Hello Single
</div>
<?php get_footer(); ?>

What's the best/most common approach to multiple sections front page in WordPress

I'm new to WordPress and I'm thinking about developing some premium themes. I see that a real trend these days is these themes with multiple sections separated in horizontal blocks in the first page. Stuff like this:
<section class="about-us row">
<h1> About us</h1>
<p>Some lorem here </p>
</section>
<section class="features row">
<h1> Features</h1>
<div class="col-1-3">
<h2>Responsive and shit</h2>
<p>Some lorem here </p>
</div>
<div class="col-1-3">
<h2>Free support</h2>
......
</section>
<section class="testimonials">
........
</section>
I'd like to know what's the best or most common approach devs are taking to provide this feature for their end-users.
I see that some of the best selling themes are using page builders managing it as shortcodes, but I'm not planning to use any page builder, at least not in my first theme, I noted that it's quite easy to get a messy code when using them, so I want to start simple.
So guys, can you help me? would the answer be using just shortcodes?
Thank you
Step 1:
I would suggest breaking the layout into sections using the get_template_part() function in your front-page template. The benefit of this is you can simply call for whatever part of the layout you need in any page template like so: get_template_part('testimonials');. You can even use them in the header and footer if you need to.
In your case i'm assuming there are 3 template parts: about us, features, and testimonials. You will need to create 3 PHP files that contain all of the code for each of those 3 parts. The PHP files will need to be located in your template's root folder. The PHP file can obviously utilize PHP however you need it to, but the main idea is that your HTML code for that section or "template part" will be placed in it's own PHP file. If you need to pull posts from wordpress, or perform database queries to generate the content for that section, you can do so individually for each template part in it's own self-contained PHP file. For the purposes of this example, let's just assume that you've called the PHP files for your template parts about_us_part.php, features_part.php, and testimonials_part.php.
After you create your 3 PHP files, making sure they are placed in your template root, you simply place the following lines of code wherever you want that particular section or "template part" to appear in your Wordpress page template. Like so:
<?php get_template_part( 'about_us_part' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part' ); // Testimonials (testimonials_part.php) ?>
Basically, get_template_part( '{slug}' ); searches for a filename in your template root matching {slug}.php. So you can name it whatever you want, but if there is no matching PHP file found, obviously nothing will show up. There is one other option for get_template_part() that allows you to specify a name for the template section. However it is optional and not required, you can use it like so:
<?php get_template_part( 'about_us_part', 'about-us' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part', 'features' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part', 'testimonials' ); // Testimonials (testimonials_part.php) ?>
You can read more about get_template_part() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/get_template_part
Step 2:
Now say you wanted to allow the user to display these template parts using shortcodes, you'd need to give them that ability in your functions.php file. For instance, say we wanted to create 3 shortcodes for each of the 3 template parts above. You can do it pretty easily using the Wordpress Shortcode API. You'd add the following code to your functions.php file:
[about_us]
function themeprefix_about_us_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'about_us_part' ); //Get about_us_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'about_us', 'themeprefix_about_us_shortcode' );
Once that function is in your functions.php file, along with the matching add_shortcode() function users can call out the About Us section by using the shortcode [about_us]. The two parts of the add_shortcode() function are the shortcode name, and the function that generates the content for the shortcode. Like so: add_shortcode( '{shortcode name}', '{shortcode function}' );
You'd need to create 2 more for your other 2 shortcodes:
[features]
function themeprefix_features_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'features_part' ); //Get features_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'features', 'themeprefix_features_shortcode' );
[testimonials]
function themeprefix_testimonials_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'testimonials_part' ); //Get testimonials_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'testimonials', 'themeprefix_testimonials_shortcode' );
Note: I placed "themeprefix" on the front of each function. I'd reccomend replacing that with your theme name, or whatever prefix you might be using on the front of your theme's function names. However the function name can be whatever you want it to be, just be sure to update your add_shortcode() to the new function name.
You can read more about add_shortcode() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/add_shortcode
Also, I reccomend reading the Shortcode API page in the codex to learn how to add parameters to your shortcodes:
http://codex.wordpress.org/Shortcode_API

Wordpress Pods: Index in Template

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.

Drupal 6: pre-defined variable for amount [count] of custom type items

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.

How do I append to the end of the comments array in WordPress?

I'm trying to include something at the end of the comments array for a WordPress plugin.
I currently have add_filter('comments_array', 'my_function'), where my_function($comments='') is something along the lines of:
my_function($comments='') {
echo 'something';
return $comments;
}
I obviously can't return the comments first, and echoing them doesn't work because $comments is a multi-dimensional array. Is there some way I can print the WordPress comments and then append something to them?
Thanks.
You want to add something that looks like a comment or add arbitrary html under each comment block?
Can you add to the page template(s) what you want to appear under the comments block? I.e.,
<?php comments_template(); ?>
html or php here
<?php endwhile; else: ?>

Resources