How to add news Ticker on home page only - php

I installed a news ticker plugin,
Then I added necessary function/code in sidebar.php
<!-- BEGIN: #secondary -->
<div id="secondary" class="g1-sidebar widget-area" role="complementary">
<h3>Latest News</h3>
<div id="news">
<?php if ( function_exists('insert_newsticker') ) { insert_newsticker(); } ?>
</div>
.......
.........
</div>
Now news ticker coming on all page's sidebar.
But i want it to add in only home page sideabr.
Is there any way to do this, so if side bar is primary/home
then only display news ticker...

use is_front_page() for the front page check.
if( is_front_page() && function_exists('insert_newsticker') ){
insert_newsticker();
}
More Information is_front_page()
Updated according to Comments:
<div id="secondary" class="g1-sidebar widget-area" role="complementary">
<?php if( is_front_page() ): ?>
// Your h3 tag here
<?php endif; ?>
<h3>Latest News</h3>
<div id="news">

use 'is_home' function more detail: http://codex.wordpress.org/Function_Reference/is_home

The $_SERVER["REQUEST_URI] will contain the uri of the current address so if you compare that to the homepage ("/") it will allow you to see whether to insert it. You could also fairly easily add support for other pages on your site if you decided to add the plugin there at a later date.

Related

How to hide Wordpress widget ID programmatically

Based on a particular value(appID) I want to hide one of the widget ID in my wordpress blog.
my widget Id : custom_html-2 ( I got this value from enabling accessibility mode)
( from this url I got widgetID : /wp-admin/widgets.php?widgets-access=on&_wpnonce=0b123456fb2&editwidget=custom_html-2&sidebar=right-sidebar&key=0 )
Here I want to hide custom_html-2 when the value of appID is in.abcd.app .
I just echoed appID , I am getting the value in.abcd.app , but the widget is not getting hidden.
I opened the Sidebar (sidebar.php) in the word press and did modification as bellow. Didnt workout - I am new to wordpress and PHP
<!-- Sidebar -->
<div class="col-lg-4 col-md-4 col-sm-4">
<aside class="sidebar">
<?php if ( is_active_sidebar( 'right-sidebar' ) ) : ?>
<div class="sidebar-widget">
<?php dynamic_sidebar( 'right-sidebar' ); ?>
<?php
<?php $widgets = get_option('widget'.sidebar-widget); ?>
$appID = $_SERVER['HTTP_X_REQUESTED_WITH']
if ($appID = 'in.abcd.app')
{
<?php unset($widgets['custom_html-2']); ?>
?>
</div>
<?php endif; ?>
</aside>
</div>
<!-- /end sidebar -->
You'll want to add something like the following to your functions.php
add_filter( 'sidebars_widgets', 'unset_sidebar_widget' );
function unset_sidebar_widget( $sidebars_widgets ) {
$appID = $_SERVER['HTTP_X_REQUESTED_WITH'];
if ($appID = 'in.abcd.app')
{
unset($sidebars_widgets['custom_html-2']);
}
return $sidebars_widgets;
}
I suppose dynamic_sidebar( 'right-sidebar' ); renders the sidebar and the widgets, so if you unset a widget after the widget was rendered, it's not going to change anything.
Try to put the dynamic_sidebar( 'right-sidebar' ); after the unset part.

how to Wordpress slider code in header show only in homepage

Hi i am new bee in wordpress customization, here is the slider code, i want to insert this code in to header and show only on home page please healp me
<?php
$slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
if ($slider_option == '1'):
do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
endif;
?>
It would be much more simpler to just create ( if you dont have one ) a "HOME" page template, which is a fairly simple PHP page with a specific HEADING, so that you can choose that template when CREATING/EDITING pages.
Then in that code you add the part where you want your slider to show, and you can easily bypass these "is_home" or "is_frontpage" clauses.
Example :
<?php
/*
Template Name: NAME-OF-TEMPLATE
Author: NAME OF AUTHOR
Web Site: author url
Contact: author email
*/
get_header(); ?>
<!-- Get nav bar -->
<?php get_template_part( 'navigation', 'default' ); ?>
<!-- Start of page content -->
<div id="primary" class="site-content">
<div id="content" role="main">
<article id="post-0" class="post">
<header class="entry-header">
<!-- Page Title/head if needed -->
<!-- <h1 class="entry-title"><?php echo get_the_title(); ?></h1> -->
<!-- Your Code snippet -->
<?php
$slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
if ($slider_option == '1'):
do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
endif;
?>
</header>
<!-- Main content -->
<div class="entry-content">
<!-- Rest of your content and page structure -->
</div><!-- .entry-content -->
</article><!-- #post-0 -->
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Please refer to this simple tutorial to help you familiarize your self : https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/
And WP referenses:
https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-page-templates-for-specific-post-types
// Use following code, is_front_page() returns true when viewing the Site Front Page.
<?php if(is_front_page()) {
$slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
if ($slider_option == '1'):
do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
endif;
} ?>
You can try is_home() or is_front_page() which will determine if it is home page
if ( is_home() || is_front_page() ) {
$slider_option = get_theme_mod('wp_store_homepage_setting_slider_option',0);
if ($slider_option == '1'):
do_action('wp_store_slider_section'); // Slider section- this function is in wp-store-function.php
endif;
} else {
// Display what you want if not home
}
Look here for reference: is_home
Look here for reference: is_front_page

Remove sidebar and replay from wordpress post page

I am looking for help to remove "junk" widgets from my wordpress website. Website theme is Zerif Lite and on every page so far there was no sidebar,however when I make a new post there is alot of junk (sidebar,who posted it,when,how entry was tagged and replay option).
I tried removing it from appearance->widgets and it didnt work and theme customization for widgets is locked for lite version.
So for start I 1st want to remove sidebar that include search,archives and meta.Only thing that worked so far was blanking out sidebar.php that include:
<div id="secondary" class="widget-area" role="complementary">
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<aside id="search" class="widget widget_search">
<?php get_search_form(); ?>
</aside>
<aside id="archives" class="widget">
<h2 class="widget-title"><?php _e( 'Archives', 'zerif-lite' ); ?></h2>
<ul>
<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
</ul>
</aside>
<aside id="meta" class="widget">
<h2 class="widget-title"><?php _e( 'Meta', 'zerif-lite' ); ?></h2>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<?php wp_meta(); ?>
</ul>
</aside>
<?php endif; ?>
</div><!-- #secondary -->
However when I did that sidebar was removed,but space it took was blank,meaning post itself was only using 2/3 of page.
To get you to see for your self here are links to site (its in serbian,ignore that):
website and:
new post, this you can only see with this link,its where problem is
So if anyone can help me remove all that and make page clean I would be very greatfull. Thanks
The problem is css
Here is the solution (only if you dont use sidebar anywhere in your site)
Change the width of your content container, maybe right now is set to 70%(remaining 30% is for sidebar) to 100%.
If you plane to use sidebar, then you need set a conditional class in your functions.php:
function custom_body_classes( $classes ) {
// Adds a class of nosidebar to sites without active sidebar.
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
$classes[] = 'nosidebar';
}
return $classes;
}
add_filter( 'body_class', 'custom_body_classes' );
Then only customize the css with the new class. Ex:
.content .nosidebar {
width:100%
}
Cheers

different widgets for different pages and posts

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 } ?>

Underscores Wordpress Theme - Adding second sidebar

Stared a Wordpress site using Underscores theme (_s)
I have got one sidebar working but want to make a second one to be on the same page. (containing different widgets)
I have added the new sidebar to the functions.php and it appears in the Wordpress login screen and i can drop widgets into it. However, I can't get it to show on the actual webpage. (the first sidebar is working fine)
Anyone know how to do this or know a tutorial...
Thanks
You'll need to edit the "sidebar.php" file in your theme folder. It should normally be:
<div id="secondary" class="widget-area" role="complementary">
<?php do_action( 'before_sidebar' ); ?>
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<?php endif; // end sidebar-1 widget area ?>
</div><!-- #secondary -->
You will need to add another if statement that looks out for the other sidebar. Be sure to reference it by whatever you used in the functions.php file.
If you want it to appear below the current sidebar, just be sure to add it before closing the 'div' so it will still be in the same column. I used 'sidebar-lower':
<div id="secondary" class="widget-area" role="complementary">
<?php do_action( 'before_sidebar' ); ?>
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<?php endif; // end sidebar-1 widget area ?>
<?php if ( ! dynamic_sidebar( 'sidebar-lower' ) ) : ?>
<?php endif; // end sidebar-lower widget area ?>
</div><!-- #secondary -->

Categories