How to hide Wordpress widget ID programmatically - php

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.

Related

How to display posts with a certain tag

I'm really struggling at what I am sure is a simple problem. I cannot seem to get the posts which fall under a certain tag to display on that tags page ie: (/tag/blog/) with blog being the Tag.
So far following the Official Tag Templates page on Wordpress I still cannot seem to get it working.
I do not need a heirarchy so tag.php works fine. Using single_tag_title() it does correctly display the tag at the top of the page.
The rest of the Official Tag Templates does not really give much more detail on wether or not I can use the default Loop or a custom one. I have tried with the custom one as shown below but that does not work. (I've kept it down to the minimum not currently worried about styling.)
<?php get_header(); ?>
<p>Tag: <?php single_tag_title(); ?></p>
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_title();
endwhile; // end while
endif; // end if
?>
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
So this code currently does display the title of the Tag but not the title of the posts which I am trying to get out.
To wrap the question up. "How do I display the posts which fall under a certain tag."
You're not telling the loop to look for that particular tag anywhere...all you're doing is displaying the current selected tag at the top of the page. Add a PHP if statement into your loop to grab the posts with that tag:
<?php get_header(); ?> // Get the header
<p>Tag: <?php single_tag_title(); ?></p> // Display the tag name
<div class="container">
<div id="content" class="clearfix row">
<div id="main" class="col col-lg-8 clearfix blog-page-padding" role="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> // Start your loop
<?php if(tag_slug( ' THE SLUG ' ) ) : ?> // Look for the tag
// Post HTML stuff
<?php endif; ?>
<?php endwhile; endif ?> // finish the loop
</div> <!-- end #main -->
<?php get_sidebar('blog'); // Blog ?>
</div> <!-- end #content -->
</div>
<?php get_footer(); ?>
This page from the Codex has examples of how to grab categories and tags.
You just need the the_post() function, you have to call it inside the while, and it should be the first function you call, as it loads the template tags.
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
the_title();
endwhile; // end while
endif; // end if
?>
if the tags is just used in custom post types you should add something like this to you functions.php:
add_action( 'pre_get_posts', 'custom_function' );
function custom_function($query){
if(! is_admin()){
if(is_tag()){
$query->set('post_type', array('your_post_type', 'your_other_post_type', 'post'));
}
}
}
I found that I had to use a WP Query loop to bring them in due to being custom post types.
<?php if ( is_tag() ) {$term_id = get_query_var('tag_id'); $taxonomy = 'post_tag'; $args ='include=' . $term_id; $terms = get_terms( $taxonomy, $args );} ?>
<!-- This gets the tags slug -->
<?php $query = new WP_Query(array( "post_type" => array('blog', 'portfolio'), "tag" => $terms[0]->slug ) ); while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

How to add news Ticker on home page only

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.

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

IF, ELSE and ELSEIF Statements

I'm trying to output a sidebar in Wordpress depending on whether it has widgets (is active) or not and to display them on my Shop page accordingly.
I'm pretty unfamiliar with PHP, and so far have written up the following scripts to try and make this work, but it doesn't seem to be happening.
shop.php:
<?php
/**
* The template for displaying the Shop page.
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php shop_content(); ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar('shop'); ?>
<?php get_footer(); ?>
sidebar-shop.php:
<?php
/**
* The sidebar containing the Shop page widget areas.
*
* If no active widgets are in the sidebars (Left, Right and theShop),
* the sidebars should be hidden completely.
*/
?>
<?php
// If the left, shop and right sidebars are inactive
if ( ! is_active_sidebar( 'right-sidebar' ) && ! is_active_sidebar( 'shop-sidebar' ) ) {
return;
}
// If there are active widgets in the Shop Sidebar
if ( is_active_sidebar( 'shop-sidebar' ) ) { ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'shop-sidebar' ); ?>
</div><!-- #secondary -->
<?php
}
// If there are active widgets in the Right Sidebar
elseif ( is_active_sidebar( 'right-sidebar' ) ) { ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'right-sidebar' ); ?>
</div><!-- #secondary -->
<?php
}
?>
sidebar.php:
<?php
/**
* The sidebar containing the main widget area.
*
* If no active widgets in sidebar, let's hide it completely.
*
*/
?>
<?php if ( is_active_sidebar( 'right-sidebar' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'right-sidebar' ); ?>
</div><!-- #secondary -->
<?php endif; ?>
How would I modify the script above to output the following:
if the Right Sidebar (right-sidebar) has widgets, display the Right Sidebar
if the Shop Sidebar (shop-sidebar) has widgets, display the Shop Sidebar
if the Right Sidebar and Shop Sidebar both have widgets, display the Shop Sidebar
if neither the Right Sidebar or the Shop Sidebar have any widgets, don't display anything
Thank you.
// if both are active shoe shop
if (is_active_sidebar('shop') && is_active_sidebar('sidebar-1')) {
get_sidebar('shop');
}
//if shop is active, show shop
elseif (is_active_sidebar('shop')) {
get_sidebar('shop');
}
// if sidebar 1 is active show it.
elseif (is_active_sidebar('sidebar-1')) {
get_sidebar('sidebar-1');
}
Code should be simple, because you have defined good precedence of Shop sidebar over right sidebar. You don't need to worry if you don't need to display the sidebars.
<?php
// If shop sidebar is active, it takes precedence
if ( is_active_sidebar( 'shop' )){
get_sidebar( 'shop' );
}
elseif (is_active_sidebar( 'sidebar-1' )){
get_sidebar( 'sidebar-1' );
//This is "shop side bar is inactive and right sidebar is active"
}
// That's all; just two condition blocks!
?>
Thanks.
It is being modified by the functions.php file.

Tough Wordpress Conditional if_page NOT display widget

I've tried many variations and can't seem to get the syntax right. In one of my Wordpress theme pages I have the following Widget code. I want to append the existing functionality to where it removes this widget if the page name is 'event-view' or page id is '640'...how do I do this?
<?php
// A third sidebar for widgets
if ( is_active_sidebar( 'third-widget-area' ) ) : ?>
<div id="third" class="widget-area" role="complementary">
<ul class="xoxo">
<?php dynamic_sidebar( 'third-widget-area' ); ?>
</ul>
</div><!-- #secondary .widget-area -->
Try this:
<?php
// A third sidebar for widgets
if ( is_active_sidebar('third-widget-area') && !is_page(640) ) : ?>
<div id="third" class="widget-area" role="complementary">
<ul class="xoxo">
<?php dynamic_sidebar( 'third-widget-area' ); ?>
</ul>
</div>
You can also check page by it's name is_page('event-view')
try changing
if ( is_active_sidebar( 'third-widget-area' ) )
to
if ( is_active_sidebar( 'third-widget-area' ) && !is_page(640) )
this way you can check if the current page is any other than the one with the id 640; see http://codex.wordpress.org/Function_Reference/is_page
Try out this.
You can give pass page_id in is_page() function.
if(is_page('640') || is_page('49'))
And for more details see below.
http://wordpress.org/support/topic/display-widget-on-certain-pages

Categories