my blog page in Wordpress is using a specific widget but I wanted to use different widget for a post in the blog.
I used an example (https://gist.github.com/anonymous/1308851) to write my code in sidebar.php.
<?php
if ( 'content' != $current_layout ) :
?>
<?php
//for rest of posts
if (is_active_sidebar('blog_widget_area') ) : ?>
<div id="secondary" class="blog_widget_area bordered" role="complementary">
<?php dynamic_sidebar( 'blog_widget_area' ); ?>
</div><!-- #secondary .widget-area -->
<?php endif;
//for specific post "Loyalty Program Trends in the Restaurant Industry"
if (is_active_sidebar('loyalty_programs_widget_area') && is_single('4063') ) : ?>
<div id="secondary" class="blog_widget_area bordered" role="complementary">
<?php dynamic_sidebar( 'loyalty_programs_widget_area' ); ?>
</div>
<?php endif;
?>
<?php endif; ?>
However, I couldn't get the second widget to display in that specific post....
Possible solutions:
Before trying below solution make sure some data/widget already loading on sideabar
loyalty_programs_widget_area.
solution 1
// use array inside is_single function
<?php if (is_active_sidebar('loyalty_programs_widget_area') &&
is_single( array('4063','4063-page-name-here') ) ) : ?>
<?php endif; ?>
solution 2
// put separate if check inside sidebar
<?php if (is_active_sidebar('loyalty_programs_widget_area') ) : ?>
if( is_single('4063') ) { } or
if( is_single(array('4063','pagename')) ) { }
<?php endif; ?>
Other possible solutions:
Try:
-deactivating ALL plugins temporarily to narrow down and possibly fix the problem . If the problem goes away, activate them individually to find the culprit?
-switching to the default theme (Twenty Ten) for a moment by renaming your current theme's folder in wp-content/themes. The idea is to force WordPress to fall back to the default theme to rule out any theme-specific issue?
For further details read official documentation regarding " sidebar "
Note:
is_single() offer different other ways to check post id or name(when permalink isenabled)
- is_single(array(17,'beef-stew','Irish Stew'));
- is_single('17'); or is_single(17);
Try this code below:
<?php // only show on 31 post not any other. ?>
<?php if ( is_single('31') ) { ?>
<?php // if blog_widget_area2 is active then show otherwise don't :) ?>
<?php if ( is_active_sidebar( 'blog_widget_area2' ) ) : ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area2"); ?>
</div>
<?php endif; ?>
<?php }else{ // otherwise load this sidebar ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area"); ?>
</div>
<?php } ?>
With the help of codewizz (Thanks codewizz!), we got it to work. The things we need to change is to move the code over from sidebar.php to single.php. However, the way it's coded, it displays both the first widget and the second widget on the post's sidebar.
So he suggested that I use http://wordpress.org/plugins/display-widgets/. And now it works.
Additionally, if one want to start from scratch, you should use that code
<?php // only show on 31 post not any other. ?>
<?php if ( is_single('31') ) { ?>
<?php // if blog_widget_area2 is active then show otherwise don't :) ?>
<?php if ( is_active_sidebar( 'blog_widget_area2' ) ) : ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area2"); ?>
</div>
<?php endif; ?>
<?php }else{ // otherwise load this sidebar ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area"); ?>
</div>
<?php } ?>
Related
I added a new custom field to a fields group to be used only to render the homepage. When trying read and display the contents of this field with the code below, nothing happens. I am sure the way to read custom field is how it should be. I don't get any error message. Could you please help fix this? This is being done on a WordPress site.
<div class="bg-white-base relative pt-row pb-row pl pr">
<div class="wrap-x">
<div class="inside mini">
<?php if ( $headline = get_sub_field( 'slider_-_h1' ) ) : ?>
<h2 class="mb color-purple-base">
<?php echo $headline; ?>
</h2>
<?php endif; ?>
<?php if ( $subheadline = get_sub_field( 'slider_-_h2' ) ) : ?>
<h3 class="mb alt-heading h5">
<?php echo $subheadline; ?>
</h3>
<?php endif; ?>
<?php if ( $text_area = get_sub_field( 'slider-_shortcode' ) ) : ?>
<article class="body-area mt2x">
<?php echo $text_area; ?>
</article>
<?php endif; ?>
</div>
</div>
</div>
get_sub_field() is supposed to be use inside a loop. When you use a group, you may use a loop like :
while( have_rows('hero') ): the_row();
........
endwhile;
Everything is explained here : https://www.advancedcustomfields.com/resources/group/
I am trying to loop out a repeater field in my wordpress front-page template
But for some reason the div is empty and doesn't seem to work.
I am 100% sure my code is correct so there must be an issue.
Anyone has any idea what it could be?
This is how my loop looks like, the field keys are correct! :)
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
This is what my front-page.php looks like. The funny thing I've used this in an other project before and everything worked fine. Now my screen is just blank, I have no idea what's going on.
if have_rows('field_56e7d8bebb545') is really true, debug the_row();
var_dump(the_row());
You can view the_result, if is empty, maybe you must set a second parameter for have_row($field_name, $post_id)
field_56e7d8bebb545 is the field key, not the field name. The field name must be used in the have_rows() function. This can be found on the Advanced Custom Fields screen next to the label -
Try this one .. since you have not printed any such thing that can be displayed in browser .. you only provided the data attribute to div.
Write something inside inner HTML might see you repeater values
<?php
/**
* Template Name: Front Page Template
*/
?>
<?php while(have_posts()) : the_post(); ?>
<?php if( have_rows('achtergrond_afbeeldingen') ): ?>
<div class="slider-circles">
<?php while ( have_rows('achtergrond_afbeeldingen') ) : the_row(); ?>
<p id="slide1" data-bgimage="<?php the_sub_field('image'); ?>" class="transparent-circle slick-active"><?php the_sub_field('image'); ?></p>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
I am building a basic Wordpress blog which is build on Duplex theme.
I Would like to do some minor customization to the site where in the site has an Instagram widget after two posts and then continue the posts.
Just to simplify the order of the page would be :
2 posts.
Instagram widget
Continue from 3rd post.
The Front page displays set to posts like the way I want.
How can I achieve getting the Instagram widget in between of the posts without disturbing the flow?
What you do is run the loop two times to display the first posts, reset it and then start it again after your widget - skipping the two first posts.
You may achieve this like so:
First run the loop, displaying the two first posts.
// The Query
$the_query = new WP_Query( array( 'posts_per_page' => 2 ) );
// The Loop
if ( $the_query->have_posts() ) {
//Display post
}
Reset the query, and display your widget
/* Restore original Post Data */
wp_reset_postdata();
//Your widget here.
Display the next posts, skipping the first two.
// The Second Query
$the_query = new WP_Query( array( 'posts_per_page' => 10, 'offset' => 2 ) );
// The Loop
if ( $the_query->have_posts() ) {
//Display post
}
Thanks a ton #danjah! Below is my code
<?php get_header(); ?>
<div id="content">
<div class="posts clearfix">
<?php if ( is_search() ) { ?>
<h2 class="archive-title"><i class="icon-search"></i> <?php echo $wp_query->found_posts; ?> <?php _e('Results for','hyphatheme'); ?> "<?php the_search_query() ?>" </h2>
<?php } else if ( is_tag() ) { ?>
<h2 class="archive-title"><i class="icon-tag"></i> <?php single_tag_title(); ?></h2>
<?php } else if ( is_day() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date(); ?></h2>
<?php } else if ( is_month() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('F Y'); ?></h2>
<?php } else if ( is_year() ) { ?>
<h2 class="archive-title"><i class="icon-time"></i> <?php echo get_the_date('Y'); ?></h2>
<?php } else if ( is_category() ) { ?>
<h2 class="archive-title"><i class="icon-list-ul"></i> <?php single_cat_title(); ?></h2>
<?php } else if ( is_author() ) { ?>
<h2 class="archive-title"><i class="icon-pencil"></i> <?php echo get_userdata($author)->display_name; ?></h2>
<?php } ?>
<?php
if ( have_posts()) :
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
else:
/*
* If no posts are found in the loop, include the "No Posts Found" template.
*/
get_template_part( 'content', 'none' );
endif;
?>
</div><!--END .posts-->
<?php if ( hypha_page_has_nav() ) : ?>
<!-- post navigation -->
<?php $post_nav_class = ( get_option( 'hypha_customizer_infinite_scroll' ) == 'enable' ) ? ' infinite' : '' ; ?>
<div class="post-nav<?php echo $post_nav_class; ?>">
<div class="post-nav-inside clearfix">
<div class="post-nav-previous">
<?php previous_posts_link(__('<i class="icon-arrow-left"></i> Newer Posts', 'hyphatheme')) ?>
</div>
<div class="post-nav-next">
<?php next_posts_link(__('Older Posts <i class="icon-arrow-right"></i>', 'hyphatheme')) ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if ( is_single() ) { ?>
<!-- comments template -->
<?php if ( 'open' == $post->comment_status ) : ?>
<?php comments_template(); ?>
<?php endif; ?>
<?php } ?>
</div><!--END #content-->
One approach would be the one mentioned would be the one by #danjah wehre you're creating a secondary loop on the page to get the first two posts. Since you're new to wordPress, lets start at the beginning.
All of your templating revolves around the loop. The loop works with the query already defined on the page. wordPress has various default queries depending on which template its loading (a post, an archive, a single post, a page, the index)
The Loop
The Tempalte Heirarchy
So a) wordpress defines the query based on the route you're on, and b) you use the loop to get at it. BUT. You can alater the query, override it, or run additional queries.
Have a loop at WP_Query
Lets loop at the standard loop. In your index.php you probably have something like :
<!-- Start the Loop. -->
<?php if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<!-- use markup or template helpers in here -->
<?php endwhile;
endif; ?>
While you could run a secondary query before the main loop, insert the thing you want, and then alter the main loop to skip those first two, the second way would be to, rather than creating another loop and altering the main one, why not rather keep a count. You could do this with a variable that you increment (its just a standard php while loop after all), But I believe that you can also get the current post index from the global $wp_query object.
$index = 1;
<!-- Start the Loop. -->
<?php if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<!-- use markup or template helpers in here -->
<?php if ($index == 2) : ?>
<!-- do something after the second post -->
<?php endif; ?>
<!-- increment the index -->
$index++;
<?php endwhile;
endif; ?>
I believe you can get the index as well with $wp_query->current_post, but I haven't done this for a while, and haven't tested this.
Hope it helps!
Good day to all,
I'm new in wordpress. I want to ask on how to display my page content in different div. But when I try this code. It won't display anything, my page won't even load. Any help would be much appreciated. Thanks!
<?php if($_SERVER['REQUEST_URI'] == '/wordpress/?page_id=5'): ?>
<div style="width:960px; float:left;min-height:290px;word-wrap: break-word">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php else: ?>
<div style="width:640px; float:left;min-height:290px;word-wrap: break-word">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php endif;?>
If you have access to the theme files, copy and paste the page.php, rename the duplicate file to page-about.php, where about is the slug of the page. Similarly you can have any number of page duplicates.
To find what is the slug of a page, go to the page editing area, Screen Option > View Slug.
I am making a theme for my site which is WordPress based. I wanted a small help as I am not an expert on php. My issue is like this:
I have a side bar div which again within it has 4 sidebars:
<div id="jp-double-bar" class="equal">
<?php if ( is_active_sidebar( 'double-top' ) ) : ?>
<div class="jp-double-top">
<?php dynamic_sidebar( 'double-top' ); ?>
</div>
<?php endif; ?>
<div class="double-middle">
<?php if ( is_active_sidebar( 'middle-2' ) ) : ?>
<div class="jp-middle-2 equalmiddle">
<?php dynamic_sidebar( 'middle-2' ); ?>
</div>
<?php endif; ?>
<?php if ( is_active_sidebar( 'middle-1' ) ) : ?>
<div class="jp-middle-1 equalmiddle">
<?php dynamic_sidebar( 'middle-1' ); ?>
</div>
<?php endif; ?>
</div>
<?php if ( is_active_sidebar( 'double-bottom' ) ) : ?>
<div class="jp-double-bottom">
<?php dynamic_sidebar( 'double-bottom' ); ?>
</div>
<?php endif; ?>
</div>
Here my issue is that if I do not publish any widgets in any of the sidebars, still the outer <div id="jp-double-bar"> shows up.
My question is....... The way we are wrapping for individual sidebar with an <?php if ():?> statement, is there a way to wrap the main outer div with 4 if statements.
Like if there is no widget published in any of the 4 sidebars then the main outer div should not display at all.
Kindly help.
Regards
It seems that you have to be using the wp_get_sidebars api call to retrieve the bars. Check and see if this has a length of zero (empty).
Or just use and statements
if (is_active_sidebar( 'double-bottom' ) && is_active_sidebar('') && ...) {}