Render conditional $region in node.tpl.php - Drupal 7 - drupal

I was trying to print a conditional region, but my code does not seem to work that well. Anyone can pitch in a fix, please?
<?php if (!empty($region['billboard'])): ?>
<aside class="col-xs-0 col-sm-12" role="banner">
<?php
$region = block_get_blocks_by_region('billboard');
print render($region);
?>
</aside>
<?php endif;?>
Just in case someone is looking for this snippet, the code shown below is for inserting a region into a node.tpl.php in Drupal 7.
<?php
$region = block_get_blocks_by_region('billboard');
print render($region);
?>
Check PraveenKumar reply below for a conditional alternative of the same code.

Try this.
<?php $region = block_get_blocks_by_region('billboard'); ?>
<?php if (!empty($region)): ?>
<aside class="col-xs-0 col-sm-12" role="banner">
<?php
print render($region);
?>
</aside>
<?php endif;?>
Hope this helps you.

Related

Comment 'get_sidebar();' have no effect: remove sidebar from theme

I'm trying to remove the sidebars from a WordPress site;
should be a pretty straight-forward action to do, but I have a problem:
I've commented every get_sidebar() I've found in my theme PHP page (including index.php, page.php etc) but seem that have no effect on the site.
Here is an example from my index.php:
<?php get_header(); ?>
<div id="page-wrapper" class="container">
<div class="row">
<div id="page-content" class="col-sm-7 col-md-8">
<?php if ( have_posts() ) : ?>
<?php get_template_part( 'templates/page-title' ); ?>
<?php $blog_layout = vw_get_option( 'blog_layout' ); ?>
<?php if ( 'classic' == $blog_layout ) : ?>
<div class="row archive-posts post-box-list">
<?php while ( have_posts() ) : the_post(); ?>
<div class="col-sm-12 post-box-wrapper">
<?php get_template_part( 'templates/post-box/classic', get_post_format() ); ?>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="row archive-posts vw-isotope post-box-list">
<?php while (have_posts()) : the_post(); ?>
<div class="col-sm-6 post-box-wrapper">
<?php get_template_part( 'templates/post-box/large-thumbnail', get_post_format() ); ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php get_template_part( 'templates/pagination' ); ?>
<?php comments_template(); ?>
<?php else : ?>
<h2><?php _e( 'Sorry, no posts were found', 'envirra' ) ?></h2>
<?php endif; ?>
</div>
<!--aside id="page-sidebar" class="sidebar-wrapper col-sm-5 col-md-4">
<?php // get_sidebar(); ?>
</aside-->
</div>
</div>
<?php get_footer(); ?>
in the example above you can see both html / PHP comment, but I've also tried with only HTML comment / only PHP comment.
may notice that i'm using a Child theme, but I've found the get_sidebar() only in the father theme.
I'm probably missing something stupid, should i remove the entire line? i'm not an WordPress expert so i'd like to do the less intrusive modification.
Thanks!
Usually there is an option within the most of WP themes to turn off the sidebar(s), also theme authors care to give you a simple checkbox for that in the single Page, Post, or even Category template, but sometimes there is no easy way out.
I suggest you check this guide back from 2017 and a bit more fresh one from 2019 here, most likely you will find your way without additional plugin in the first reference.

Wordpress - How do I show only the 4 newest posts on my page?

I am trying to only load the 4 latest posts on my page and I have found a line of code that should work. The problem is that I don't know where to put it.
<?php query_posts('posts_per_page=20'); ?>
This is what I found when I was searching for a solution (although I don't know if this will also work for showing only the 4 newest posts. As you can see I already have 'catname=projecten' on that line and I have no idea how to combine the two.
<?php
query_posts('catname=projecten');
while (have_posts()) : the_post();?>
<div class="col-md-3">
<div class="newsfeed center">
<h1><?php echo get_the_title( $ID ); ?> </h1>
<p>
<?php the_content(); ?>
</p>
</div>
</div>
<?php endwhile;
?>
I hope someone can help me out!
I'd like to answer it, because there are few things need to be corrected. This should do it, and notes below.
<?php
query_posts('category_name=projecten&posts_per_page=4');
while (have_posts()) : the_post(); ?>
<div class="col-md-3">
<div class="newsfeed center">
<h2><?php echo get_the_title(); ?> </h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
A few notes:
It's category_name, not catname.
You should not use <h1> for each title, for a better accessibility use <h2> or <h3> etc.
get_the_title( $ID ); the $ID seems not necessary to be there.
Don't wrap the_content(); with <p> it's rich content already, use <div> if you need.
Don't forget to reset the query afterwards.
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query

Wordpress Conditional page types such as is_home,is_front_page, is_single not working

I have been trying to use several conditional statements using is_home/is_front_page or is_single and they are not working in my theme. Below is an example on such code:
<?php if(is_front_page() || is_home() ) : ?>
<?php if(function_exists('show_flexslider_rotator'))
echo show_flexslider_rotator('homepage_fexslider' ); ?>
This loads my Flexslider, but when I go to page 2, the slider remains on the page.
I have used these statements successfully in other themes.
Do I need to add support for this feature in my functions.php?
How can I troubleshoot this?
My home.php code is as follows:
<?php get_header(); ?>
<?php if(is_front_page() || is_home() ) : ?>
<?php if(function_exists('show_flexslider_rotator')) echo show_flexslider_rotator( 'homepage' ); ?>
<?php endif; ?>
<div class="row below-banner">
<div id="site-content" class="span8">
<div class="row">
<div class="span8">
<div class="widget-header">
<h2>What's new</h2>
</div>
</div>
</div>
<div class="row">
<div class="posts span8">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') :1; query_posts('cat=-35&paged=' . $paged);?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part( 'entry' ); ?>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php else : ?>
<h2>No Posts Found</h2>
<?php endif; wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
We would need more info/code in order to give a definitive answer, but my best guess is that you need to add a wp_reset_query() somewhere.
A quick test: add wp_reset_query() immediately before the conditional statements and see if that resolves the issue. If so, then you need to find the offending query and make sure it is reset at the appropriate time.
The issue with wordpress conditionals like is_single is that they don't work outside a functions in functions.php
So the solution is to place the conditional within the function and call that function using add_action probably.
The code here is_single not working in functions.php explained how it won't work outside the function and how should it be instead

custom wordpress theme echoes gallery short code

So i'm quite a beginner, trying to make a custom theme. In a page i want to have a gallery. Uploaded images, made a gallery all fine.
When I view the page it outputs only the shortcode:
[gallery orderby="post_date"]
my page.php file basically has:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo $content->post_content ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I really don't understand how to get this to show correctly, any pointers would be greatly appreciated. Cheers, Matt
get_page returns the raw page data. There are a few ways to do what you want:
BAD WAY:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo do_shortcode($content->post_content); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
do_shortcode() renders all registered shortcode that's found within a given string. In this case, your page's content will have all shortcode rendered before being written to the document. I say this is the "bad" way, only because it doesn't follow the usual Wordpress format. Which leads us to the:
BETTER WAY:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<h1><?php the_title(); ?></h1><br>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is what's called "The Loop". It is pretty much the standard for all Wordpress Themes in retrieving all Post or Page Data, as well as running queries against the database.
I would suggest getting to know it, as well as running Wordpress queries to modify the loop using WP Query. This is getting into a more complex area of Wordpress, but it will help you in the longrun with figuring out how to gather up all of the posts and pages you want to retrieve in your theme that aren't provided by Wordpress' globals.
Good luck.

wordpress function wp_link_pages not working

The function wp_link_pages() is not working at all in my wordpress template. I have tried all possibilities to make it work. I didn't get any satisfying help for the same online. Please do help me. Guide.
Thanks in advance.
You should use <!--nextpage--> for work wp_link_pages function.
This tutorial will be help to understand how to work <!--nextpage-->
https://www.youtube.com/watch?v=oGFjVx9jDd4
Example:
https://codex.wordpress.org/Function_Reference/wp_link_pages#Default_Usage
Use the plugin WP-PageNavi - sample code below:
<?php get_header(); ?>
<!-- Start Loop -->
<?php query_posts('category_name='SomeCategoryName'&orderby=title&order=ASC&paged='. get_query_var('paged')); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php //Loop Code Here ?>
<?php endwhile; else: ?>
<p class="error">No results found.</p>
<?php endif; ?>
<!-- Start Pagination - WP-PageNavi -->
<div id="pagination">
<?php wp_pagenavi(); ?>
</div>
<!-- End Pagination -->
<?php wp_reset_query(); ?>
<!-- End Loop -->
<?php get_footer(); ?>
I think header section
the_post(); missing

Resources