In one of my D7 project, I used several tpl files for view templating. Please see below some of the tpl file codes. However, my client has been reviewed the codes and commented:
Hard coding layout in Views. Instead of creating blocks with different display layouts (same display layouts are the same in other places), there is a code with the layout on the Views result that it depends on the key of the result.
I cannot understand the points he saying. If you wish, I can share some additional coding / admin panel screen shots
Here are some tpl files:
1.
<div id="programmes" class="page-section">
<div class="container">
<h1 class="section-heading"><?php echo t('OUR');?> <?php echo count($rows); ?> <?php echo t('key Programmes');?></h1>
<h3><?php echo t('The XXX operate');?> <?php echo count($rows); ?> <?php echo t('key programmes to ensure the greatest global impact:');?></h3>
<div class="programblock" >
<?php
if($rows) {
$count = 1;
foreach($rows as $id => $row)
{
$title = $view->render_field('title', $id);
$description = $view->render_field('field_programme_description', $id);
$image = $view->render_field('field_programme_image', $id);
$icon = $view->render_field('field_programme_icon', $id);
$color = $view->render_field('field_programme_background_color', $id);
?>
<a href="#" class="programcard w-inline-block" style="background-color:<?php echo !empty($color) ? $color : '#FFFFFF' ?>"><img src="<?php echo $image; ?>" class="progimage">
<div class="programtext">
<div class="w-clearfix">
<div class="progtabicon smallpti"><?php echo $icon; ?><br></div>
</div>
<div>
<h2 class="progtitle"><?php echo $title; ?></h2>
<div><?php echo $description; ?></div>
<div class="readmorecard"><?php echo t('Read More...'); ?></div>
</div>
</div>
</a>
<?php
$count++;
}
}
?>
</div>
</div>
</div>
2.
<div id="s5" class="relevant-news">
<div class="container w-clearfix">
<h3 class="section-heading"><?php echo t('Related content'); ?></h3>
<?php
if ($rows) {
foreach ($rows as $id => $row) {
$image = $view->render_field('field_image', $id);
$body = $view->render_field('body', $id);
$title = $view->render_field('title', $id);
$date = $view->render_field('field_news_publist_date', $id);
$formatted_date = date('d F Y', strtotime($date));
$programme = $view->render_field('field_news_programme', $id);
$type = $view->render_field('field_tags', $id);
?>
<div class="_4-block nomar4">
<a href="#" class="nl2 w-inline-block">
<div class="tag" data-ix="tagtext"><span class="cblu"></span><span class="uhcgreen"> </span><span class="jyel"></span><br></div><img src="<?php echo $image; ?>" alt="image" class="cardimage">
<div class="card-text">
<div class="cardtitle"><?php echo $title; ?></div>
<div class="tagtext alt"><?php echo $formatted_date; ?></div>
<div><?php echo $body; ?></div>
<div class="tagtext alt"><?php echo $programme; ?></div>
</div>
<div class="pagetype news">
<div><span class="fa"></span> <?php echo $type; ?></div>
</div>
</a>
</div>
<?php
}
}
?>
</div>
<div class="container"><span class="fa"></span> <?php echo t('View all News & Media'); ?></div>
</div>
3.
<?php global $base_url;?>
<?php
if(!empty($rows)){
$articles = array();
$video = array();
$gallery = array();
$carousel = array();
foreach($rows as $article){
$nid = $article['nid'];
$node = node_load(trim($nid));
if($node->type == 'article'){
$articles[] = $nid;
}elseif($node->type == 'news_video'){
$video[] = $nid;
}elseif($node->type == 'news_gallery'){
$gallery[] = $nid;
}
$slider = 0;
if($node->type != 'article'){
$carousel[] = $nid;
}
}
$items = array();
$count = 0;
if(!empty($carousel)){
$slider = 0;
foreach($carousel as $nid){
$items[$slider][] = $nid;
$count++;
if(($count % 5) == 0) {
$slider++;
}
}
}
?>
<!--Show stiky images-->
<div class="container w-clearfix">
<?php
$i = 0;
for($i = 0; $i < count($articles); $i++){
$nid = (int) $articles[$i];
$node = node_load(trim($nid));
$title = $node->title;
$image = file_create_url($node->field_image[LANGUAGE_NONE][0]['uri']);
$news_type_id = $node->field_tags[LANGUAGE_NONE][0]['tid'];
$news_type = taxonomy_term_load($news_type_id);
$programmes = $node->field_news_programme[LANGUAGE_NONE];
$type_name = $news_type->name;
$type_icon = path_icon($news_type->field_news_icon[LANGUAGE_NONE][0]['icon']);
$description = !empty($node->field_short_description[LANGUAGE_NONE]) ? $node->field_short_description[LANGUAGE_NONE][0]['value'] : "";
$date = $node->field_news_publist_date[LANGUAGE_NONE][0]['value'];
$program_name = array();
$program_short_name = array();
$node_url = drupal_get_path_alias('node/' . $nid);
?>
<div class="_2block nomar special">
<a href="<?php print $node_url;?>" class="nl2 bgimage w-inline-block" style="background-image:linear-gradient(180deg, rgba(21, 36, 49, 0), #152431), url('<?php echo $image;?>');background-repeat:no-repeat;background-size:cover;">
<div class="pagetype">
<div><span class="fa fa-<?php print $type_icon;?>"></span> <?php echo $type_name;?></div>
</div>
<div class="card-text"></div>
<?php if(!empty($programmes)){ ?>
<div class="tag">
<?php foreach($programmes as $key2 => $pro){
$programme = taxonomy_term_load($pro['tid']);
$bundle = $programme->field_icon_programme[LANGUAGE_NONE][0]['bundle'];
$icon = path_icon($programme->field_icon_programme[LANGUAGE_NONE][0]);
$program_name[] = $programme->name;
$program_short_name[] = $programme->field_short_name[LANGUAGE_NONE][0]['value'];
?>
<i class="<?php echo $icon;?>"></i>
<?php } ?>
<br>
</div>
<?php } ?>
<div class="card-text forcedown">
<div class="cardtitle bigct"><?php echo $title;?></div>
<div class="tagtext alt"><?php echo date("d M Y", strtotime($date));?></div>
<div><?php echo $description;?></div>
<?php if(!empty($program_short_name)){?>
<div class="tagtext"><?php echo implode($program_short_name, ', ');?></div>
<?php }?>
</div>
</a>
</div>
<?php
if($i == 1) {
break;
}
}
?>
</div>
First thing , view provide several "Display" for one view created. It let you create some various display of this view to handle and manage data/display.
- You can create a display like a "Page" so your view will have own url.
- You can create another display like a "Block" , so it provide a block you can add to region
Small tutorial for view creation
Second thing is to understand templating hook override available , so you can change html and displaying for a view:
If my view is named "test" and my display is named "block" , i can override with file
views-view--test--block.tpl.php
How theming function work
Third thing , template must not contain functions calls , it can create some performance issues and it's not a good practice. Only $variables must be echo or printed.
You can take a look into views module , under "themes" folder to understand.
Finally , if you need to modify some data before rendering you have several hooks usable into custom modules or template.php which allow you to modify it
List of hooks available
Hope it helps you :)
Related
I am looking for a approach to add multiple images option under the Feature image section in wordPress custom post type. I will integrate/implement these images on single.php file where I want to show the slider images. I want to do it using Codex / coding approach, Please NO PLUGIN. Any Idea/help will be appreciated.
EDIT
I have found this code and it has to be pasted in function.php or plugin.php. I have create a new file plug-slider.php and add the below code there & its not working, but the issue is that I have to call each image separately instead like create an array and call the array, also if there is no second/third image set for other post then the slider block is showing empty.. I don't know why, may be I am missing something... can someone please review.
Code Reference: Stack overflow question
//init the meta box
add_action( 'after_setup_theme', 'custom_postimage_setup' );
function custom_postimage_setup(){
add_action( 'add_meta_boxes', 'custom_postimage_meta_box' );
add_action( 'save_post', 'custom_postimage_meta_box_save' );
}
function custom_postimage_meta_box(){
//on which post types should the box appear?
$post_types = array('portfolios','page');
foreach($post_types as $pt){
add_meta_box('custom_postimage_meta_box',__( 'More Featured Images', 'yourdomain'),'custom_postimage_meta_box_func',$pt,'side','low');
}
}
function custom_postimage_meta_box_func($post){
//an array with all the images (ba meta key). The same array has to be in custom_postimage_meta_box_save($post_id) as well.
$meta_keys = array('second_featured_image','third_featured_image');
foreach($meta_keys as $meta_key){
$image_meta_val=get_post_meta( $post->ID, $meta_key, true);
?>
<div class="custom_postimage_wrapper" id="<?php echo $meta_key; ?>_wrapper" style="margin-bottom:20px;">
<img src="<?php echo ($image_meta_val!=''?wp_get_attachment_image_src( $image_meta_val)[0]:''); ?>" style="width:100%;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" alt="">
<a class="addimage button" onclick="custom_postimage_add_image('<?php echo $meta_key; ?>');"><?php _e('add image','yourdomain'); ?></a><br>
<a class="removeimage" style="color:#a00;cursor:pointer;display: <?php echo ($image_meta_val!=''?'block':'none'); ?>" onclick="custom_postimage_remove_image('<?php echo $meta_key; ?>');"><?php _e('remove image','yourdomain'); ?></a>
<input type="hidden" name="<?php echo $meta_key; ?>" id="<?php echo $meta_key; ?>" value="<?php echo $image_meta_val; ?>" />
</div>
<?php } ?>
<script>
function custom_postimage_add_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
custom_postimage_uploader = wp.media.frames.file_frame = wp.media({
title: '<?php _e('select image','yourdomain'); ?>',
button: {
text: '<?php _e('select image','yourdomain'); ?>'
},
multiple: false
});
custom_postimage_uploader.on('select', function() {
var attachment = custom_postimage_uploader.state().get('selection').first().toJSON();
var img_url = attachment['url'];
var img_id = attachment['id'];
$wrapper.find('input#'+key).val(img_id);
$wrapper.find('img').attr('src',img_url);
$wrapper.find('img').show();
$wrapper.find('a.removeimage').show();
});
custom_postimage_uploader.on('open', function(){
var selection = custom_postimage_uploader.state().get('selection');
var selected = $wrapper.find('input#'+key).val();
if(selected){
selection.add(wp.media.attachment(selected));
}
});
custom_postimage_uploader.open();
return false;
}
function custom_postimage_remove_image(key){
var $wrapper = jQuery('#'+key+'_wrapper');
$wrapper.find('input#'+key).val('');
$wrapper.find('img').hide();
$wrapper.find('a.removeimage').hide();
return false;
}
</script>
<?php
wp_nonce_field( 'custom_postimage_meta_box', 'custom_postimage_meta_box_nonce' );
}
function custom_postimage_meta_box_save($post_id){
if ( ! current_user_can( 'edit_posts', $post_id ) ){ return 'not permitted'; }
if (isset( $_POST['custom_postimage_meta_box_nonce'] ) && wp_verify_nonce($_POST['custom_postimage_meta_box_nonce'],'custom_postimage_meta_box' )){
//same array as in custom_postimage_meta_box_func($post)
$meta_keys = array('second_featured_image','third_featured_image');
foreach($meta_keys as $meta_key){
if(isset($_POST[$meta_key]) && intval($_POST[$meta_key])!=''){
update_post_meta( $post_id, $meta_key, intval($_POST[$meta_key]));
}else{
update_post_meta( $post_id, $meta_key, '');
}
}
}
}
My Callback code in single.php
<div class="project-carousel owl-carousel js-project-carousel">
<?php
while(have_posts()) {
the_post(); ?>
<!-- <div class="project-detail-item">
<?php //the_post_thumbnail('portfolio-slider') ?>
</div> -->
<?php
$slider_img=wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
if($slider_img !='')
{?>
<div class="project-detail-item">
<?php
echo wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
?>
</div>
<?php }
else
{
the_post_thumbnail('portfolio-slider');
}
?>
</div>
again the if/else block works for the empty check but only for single instance, like I have to check for each image... or I messed up?
Please do the below change and then try...
in your plugin file change this line.
$meta_keys = array('second_featured_image','third_featured_image');
To
$meta_keys = array('second_featured_image','third_featured_image','fourth_featured_image','fifth_featured_image');
change your call back single.php file code to something like below. Loop through each file and check for empty.
I have modified the code to accept up to 5/6 images so you can use it in the slider.
<div class="project-carousel owl-carousel js-project-carousel">
<?php
while(have_posts()) {
the_post();
$slider_img=the_post_thumbnail('portfolio-slider');
$slider_img1=wp_get_attachment_image(get_post_meta(get_the_ID(),'second_featured_image', true),'full');
$slider_img2=wp_get_attachment_image(get_post_meta(get_the_ID(),'third_featured_image', true),'full');
$slider_img3=wp_get_attachment_image(get_post_meta(get_the_ID(),'fourth_featured_image', true),'full');
$slider_img4=wp_get_attachment_image(get_post_meta(get_the_ID(),'fifth_featured_image', true),'full');
$slider_img5=wp_get_attachment_image(get_post_meta(get_the_ID(),'six_featured_image', true),'full');
$slider_img6=wp_get_attachment_image(get_post_meta(get_the_ID(),'svn_featured_image', true),'full');
if($slider_img !='')
{?>
<div class="project-detail-item">
<?php echo $slider_img;?>
</div>
<?php }?>
<?php if($slider_img1 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img1; ?>
</div>
<?php }?>
<?php if($slider_img2 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img2; ?>
</div>
<?php }?>
<!--image-->
<?php if($slider_img3 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img3; ?>
</div>
<?php }?>
<!--image end-->
<!--image-->
<?php if($slider_img4 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img4; ?>
</div>
<?php }?>
<!--image end-->
<!--image-->
<?php if($slider_img5 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img5; ?>
</div>
<?php }?>
<!--image end-->
<!--image-->
<?php if($slider_img6 !=''){ ?>
<div class="project-detail-item">
<?php echo $slider_img6; ?>
</div>
<?php }?>
<!--image end-->
</div>
As posted here https://www.concrete5.org/index.php?cID=751287 I want to get a thumbnail from a page using the 'old' way.
Before I could use the code below which included an image helper.
<div class="image-link">
<a <?php if ($target != '') { ?> target="<?php echo $target ?>" <?php } ?> href="<?php echo $url ?>">
<?php
$ts = $page->getBlocks('Thumbnail Image');
if (is_object($ts[0])) {
$tsb = $ts[0]->getInstance();
$thumb = $tsb->getFileObject();
if ($thumb) {
$ih->outputThumbnail($thumb, 170, 80, $title);
}
}
?>
</a>
</div>
From this section of the subpage:
<div id="thumbnail">
<?php
if ($c->isEditMode()) {
print '<br><br>';
$a = new Area('Thumbnail Image');
$a->display($c);
}
?>
</div>
However now this has all changed and the new system uses page attributes for thumbnails. As the site is already setup the 'old' way I want to be able to retrieve the thumbnail the same way again.
Any help would be much appreciated.
I have "thumbnail" page attribute set via the composer, and this is how I retrieve it in the page template:
<?php
$thumbnail = $c->getAttribute('thumbnail');
if($thumbnail) {
$img = Core::make('html/image', array($thumbnail));
$tag = $img->getTag();
print $tag;
}
?>
I dug out my experimentation hat and fixed it.
<div class="image-link">
<a <?php if ($target != '') { ?> target="<?php echo $target ?>" <?php } ?> href="<?php echo $url ?>">
<?php
foreach ($blocks as $block) {
if ($block->getBlockTypeHandle() == "image" && $block->getAreaHandle() == "Thumbnail Image") {
if (is_object($block)) {
$tsb = $block->getInstance();
$thumb = $tsb->getFileObject();
if ($thumb) {
$ih->outputThumbnail($thumb, 170, 80);
}
}
}
}
?>
</a>
</div>
i want to display portfolio category above the content where all portfolio is displayed. i know some of the modification is required in loop portfolio.php but how put that so that it display above the content post. i want to display in ul li format how it possible?? i m using broadscope theme
my code for loop portfolio.php is given below
<?php
global $avia_config;
if(isset($avia_config['new_query'])) {
query_posts($avia_config['new_query']);
}
// check if we got a page to display:
if (have_posts()) :
$loop_counter = 1;
$extraClass = 'first';
//iterate over the posts
while (have_posts()) : the_post();
//get the categories for each post and create a string that serves as classes so the javascript can sort by those classes
$sort_classes = "";
$item_categories = get_the_terms( $id, 'portfolio_entries' );
if(is_object($item_categories) || is_array($item_categories))
{
foreach ($item_categories as $cat)
{
$sort_classes .= $cat->slug.'_sort ';
}
}
?>
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>
<?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>
<?php the_title(); ?>
<span class='date_sort hidden'><?php the_time('Y m d H i s'); ?></span>
<div class="entry-content">
<!--<?php the_excerpt(); ?> -->
<!--<a class="more-link" href="<?php echo get_permalink(); ?>"><?php _e('Read more','avia_framework'); ?></a>
-->
</div>
<!-- end post-entry-->
</div>
<?php
$loop_counter++;
$extraClass = "";
if($loop_counter > 3)
{
$loop_counter = 1;
$extraClass = 'first';
}
endwhile;
else:
?>
<div class="entry">
<h1 class='post-title'><?php _e('Nothing Found', 'avia_framework'); ?></h1>
<p><?php _e('Sorry, no posts matched your criteria', 'avia_framework'); ?></p>
</div>
<?php
endif;
?>
i want to display portfolio category before
<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php
_e('Permanent Link:','avia_framework')?> <?php echo avia_inc_display_featured_within_entry('portfolio', false); ?>">
inside
<div class='post-entry one_third all_sort <?php echo
$sort_classes.$extraClass; ?>'>
so please help me if anyone know this. hope friends you help me regarding this question
Try to insert the following code
<?php
$post_id = get_the_ID()
$item_terms = get_the_terms( $post_id, 'portfolio_entries' );
if($item_terms !== false && count($item_terms) > 0)
{
echo '<ul>';
foreach ($item_terms as $term)
{
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}
?>
just after
<div class='post-entry one_third all_sort <?php echo $sort_classes.$extraClass; ?>'>
i added a block with some information through sitebuilding->block->addblock
and selected its region as header
But this block doesn't displays in header....any idea?
I am using drupal6
The code of page.tpl.php is as follows
<div id="header">
<div id="header_logo">
<?php if (isset($secondary_links)) : ?>
<div id="secondary-menu">
<?php print theme('links', $secondary_links, array('class' => 'links secondary-links')) ?>
</div>
<?php endif; ?>
<?php
// Prepare header
$site_fields = array();
if ($site_name) {
$site_fields[] = check_plain($site_name);
}
if ($site_slogan) {
$site_fields[] = check_plain($site_slogan);
}
$site_title = implode(' ', $site_fields);
if ($site_fields) {
$site_fields[0] = '<span>'. $site_fields[0] .'</span>';
}
$site_html = implode(' ', $site_fields);
if ($logo || $site_title) {
print '<h1><a href="'. check_url($front_page) .'" title="'. $site_title .'">';
if ($logo) {
print '<img src="'. check_url($logo) .'" alt="'. $site_title .'" id="logo" />';
}
print $site_html .'</a></h1>';
}
?>
</div>
<div id="primary-menu">
<?php if (isset($primary_links)) : ?>
<?php print $primary_links_tree; ?>
<?php endif; ?>
</div>
</div>
<!-- end header -->
I'm guessing header is defined in your theme .info file since you see it in the block page.
regions[header] = Header
But what I don't see is you printing the header region in the page template. If the region is named 'header', then you need to add this somewhere in your page template.
print $header;
I need to add links to WordPress Posts on a static HTML page. I got some information that has been helpful but need a few more elements to make it complete.
Here is the code I have so far:
<?php
$number = 5;
$wordpress_header = "blog/wp-blog-header.php";
// Include wordpress header
if (file_exists($wordpress_header))
{
include ($wordpress_header);
$myposts = get_posts('numberposts=$number&offset=0&category=0');
echo "<ul class='Bloglinks'>";
foreach(array_slice($myposts, 0, $number) as $post)
{
echo '<li><a href="';
the_permalink();
echo '">';
the_date();
echo " ";
the_title();
echo '</a></li>';
}
echo "</ul>";
}
else
{
echo "Unable to connect to Wordpress header file.";
die();
}
?>
This only shows the titles of the most recent posts. I need to be able to display the following:
<h5>The truth about Lazy Eye</h5>
<p class="blog-info">07.16.10 | <a class="comment-ref">3 Comments</a></p>
<h5>More Adult Learning Problems Linked to Eyes</h5>
<p class="blog-info">06.12.10 | <a class="comment-ref">1 Comments</a></p>
<h5>New Vision Examination Instruments Arrived!</h5>
<p class="blog-info">05.10.10 | <a class="comment-ref">13 Comments</a></p>
You should use the function query_posts() with the functions have_posts() and the_post(). Here is an example for the WordPress API:
//The Query
query_posts('posts_per_page=5');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
That will loop through all posts you have queried. So you can just insert your query from the get_posts() function into the query_posts() function.
EDIT: I think if you want to stick with the get_posts() function, you have to call the setup_postdata() function to get the new post (source code for the API):
<ul>
<?php
global $post;
$myposts = get_posts('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
But I would recommend to take the query_posts() function instead.
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
$args = array(
// 'cat' => 3, // Only source posts from a specific category
'posts_per_page' => 6 // Specify how many posts you'd like to display
);
$latest_posts = new WP_Query( $args );
if ( $latest_posts->have_posts() ) {
while ( $latest_posts->have_posts() ) {
$latest_posts->the_post(); ?>
<article class="vertical-item content-padding ls">
<div class="item-media">
<img src="<?php the_post_thumbnail() ?>" alt="<?php the_title(); ?>">
</div>
<div class="item-content">
<h4 class="entry-title">
<?php the_title(); ?>
</h4>
<div>
<div class="media-body media-middle greylinks">
<br><a class="small-text" href="#"><?php the_time('l jS F, Y') ?></a>
</div>
</div>
</div>
<div class="item-footer">
<a class="lato lightgrey weight-black" href="<?php the_permalink(); ?>">Read this Article</a>
</div>
</article>
<? }
} else {
echo '<p>There are no posts available</p>';
}
wp_reset_postdata();
?>