Surya kanth
asked 11 years ago

How can i have Ask a question button in my twenty twelve theme with categories list also.
And How can i control the sidebar based on pages. That is I should show the button and categories and other widgets only on clicking questions page.
Thanks.

5 Answers
Guru
answered 11 years ago

I would be interested in hearing from the admins about this one too!

Kido D
answered 11 years ago

Hi there, Please follow the steps here:
(Note: This is based on the twenty twelve theme, but the steps should be the same with other themes)
1) Create a new template for DW Q&A (override the template files in the plugin)

  • Go to your theme folder, and create a new folder, named it dwqa-templates.
  • Inside your new folder, create 2 files: content-start-wrapper.php and content-end-wrapper.php.

2) Edit the content wrapper files
Based on the content wrapper of the twenty twelve theme, let’s add the code below to the content-start-wrapper.php file:

 <?php
if ( ! defined( 'ABSPATH' ) ) exit;
global $dwqa_options;
?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php if( ( $dwqa_options['pages']['archive-question'] && is_page( $dwqa_options['pages']['archive-question'] ) ) || is_archive() ) { ?>
<header class="entry-header">
<h1 class="entry-title"><?php _e('Questions & Answers', 'dwqa') ?></h1>
</header>
<?php } ?>

.. and the content-end-wrapper.php file:

 <?php 
if ( ! defined( 'ABSPATH' ) ) exit;
global $dwqa_options;
?>
<?php if( ( $dwqa_options['pages']['archive-question'] && is_page( $dwqa_options['pages']['archive-question'] ) ) || is_archive() ) { ?>
</div><!--/ content -->
</div><!--/ primary -->
<div id="secondary" class="widget-area">
<div class="dwqa-container">
<?php dwqa_get_ask_question_link( true, false, 'dwqa-btn dwqa-btn-block dwqa-btn-success' ); ?>
<?php dynamic_sidebar( 'question-sidebar' ); ?>
</div>
</div>
<?php } ?>
<?php if(is_single()) { ?>
</div><!--/ content -->
</div><!--/ primary -->
<div id="secondary" class="widget-area">
<div class="dwqa-container">
<?php dynamic_sidebar( 'single-question-sidebar' ); ?>
</div>
</div>
<?php } ?>
<?php if( $dwqa_options['pages']['submit-question'] && is_page( $dwqa_options['pages']['submit-question'] ) ){ ?>
</div></div>
<?php } ?>

3) Add new sidebar positions for Q&A pages, and register the caregories list widget
Open up the functions.php file in the theme folder, and add the following code to bottom of the file:

 // Question pages sidebar
add_action( 'widgets_init', 'dwqa_register_sidebar' );
function dwqa_register_sidebar() {
register_sidebar( array(
'name' => __( 'Questions Sidebar', 'dwqa' ),
'id' => 'question-sidebar',
'description' => 'Appears in the questions page of the site.',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_sidebar( array(
'name' => __( 'Single Question Sidebar' ),
'id' => 'single-question-sidebar',
'description' => 'Appears in the single question page of the site.',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
register_widget('dwqa_categories_tags_widget');
}

4) Create the new widget for showing the categories list
Still in the functions.php file, add the following code to the bottom of the file:

 // question categories/tags widget
class dwqa_categories_tags_widget extends WP_Widget {
function dwqa_categories_tags_widget() {
parent::WP_Widget( false, __( 'DWQA Categories / Tags', 'nex' ), array( 'description' => __( 'Display the questions categories or tags', 'nex' ) ) );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$tax = $instance['taxonomy'];
global $post;
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
$terms = get_terms( $tax, array( 'hide_empty' => false ) );
if ( is_wp_error( $terms ) ) {
return;
} else {
echo "<ul class=\"dwqa-taxonomy-widget ".$tax."\">\n";
$query_var = get_query_var($tax);
$query_term = get_term_by('slug', $query_var, $tax);
foreach ( $terms as $term ) {
if ( $query_term->term_id == $term->term_id ) {
echo '<li class="current">';
} else {
echo '<li>';
}
echo '<a href="' . get_term_link( $term ) . '" title="' . esc_attr( $term->name ) . '" rel="bookmark">' . $term->name . '</a> ('.$term->count.')</li>'."\n";
}
echo "</ul>\n";
}
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['taxonomy'] = strip_tags( $new_instance['taxonomy'] );
return $instance;
}
function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$taxonomy = isset( $instance['taxonomy'] ) ? esc_attr( $instance['taxonomy'] ) : 'dwqa-question_category';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'nex' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy:', 'nex' ); ?></label>
<select name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>">
<option value="dwqa-question_category" <?php selected( 'dwqa-question_category', $taxonomy ); ?>><?php _e( 'Categories', 'nex' ); ?></option>
<option value="dwqa-question_tag" <?php selected( 'dwqa-question_tag', $taxonomy ); ?>><?php _e( 'Tags', 'nex' ); ?></option>
</select>
</p>
<?php
}
}

5) Add the new widgets to the question sidebar positions
Go to Dashboard / Appearance / Widgets, and add the widgets to the new sidebar positions that you created above.
See the screenshot:

 
 
 
 
P/S.: You may need a little custom CSS code to make the widgets display as you want.
Hope this helps!

Surya kanth
replied 11 years ago

Thanks a lot will try this and let you know. Thanks again.

Surya kanth
replied 11 years ago

I Tried it the sidebars got added in the admin (widgets). But when I come to the site the sidebar disappears completely!!!!!

Guru
replied 11 years ago

You have to make sure that the content wrapper is actually set right to display the widget and the links.

It’s working for me.

Surya kanth
replied 11 years ago

can you kindly tell me how to do it.

Guru
replied 11 years ago

Surya, I can’t provide a ready made answer, as the answer was given by Kido already. You have to use the code above to adapt it to your particular theme and wordpress installation to make it work. I hope you have already integrated DWQA template into your theme.

Kido D
answered 11 years ago

Hi Surya,
As Guru has said, you need to integrate the plugin into your theme and flexibly use the code.
If could not make it, you can send me your site URL, the WordPress admin account and the FTP info (via private answer), so I can check it for you.
Regards,

Guru
replied 11 years ago

Hi @kido-d

Could please advise how to limit the number of items returned in the category & tag functions for the sidebar. I want to limit the categories/tags with the highest number of answers to 8. Please help!

Kido D
answered 11 years ago

Hi Surya,
I have fixed this issue for you, please check again.

Kido D
answered 11 years ago

@guruji,
To limit the categories/tags to 8, please open up the functions.php file in your theme folder, find the code below (inside the dwqa_categories_tags_widget class):

 foreach ( $terms as $term ) {

.. and change it into this:

 $i = 0;
foreach ( $terms as $term ) {
$i++; if($i > 8) break;

Hope this helps!

Guru
replied 11 years ago

Great Help Kido 🙂

And if we don’t want empty categories/tags to appear in the widget what to do about it. I mean some times there maybe tags/categories which are empty, it wouldn’t be a good user experience to see empty tags/categories being listed.

Best regards,

Kido D
answered 11 years ago

Hi Guru,
To resolve that issue, please use this code:

 $i = 0;
foreach ( $terms as $term ) {
if(!$term->count) {
continue;
}
$i++; if($i > 8) break;

Hope this helps!

Guru
replied 11 years ago

Superb! Thankx alot Kido.

Drew Little
replied 11 years ago

Hello, I need help implementing this in my theme…I followed the steps but the “Ask a Question” buttons and the sidebar isn’t showing. (see link for screenshot: http://postimg.org/image/hbn35110p/) I can send you admin and ftp login for you the check it out.

Many thanks for any help!

Kido D
answered 10 years ago

Hi Drew,
Please send me your site URL, the WordPress admin account and the FTP info (via private answer), so I can check it for you. Thanks!

Vik Sohal
replied 10 years ago

Hello,

I used all of the steps, but the sidebar is appearing under the questions/ask questions field and not to the right side. Can I ask you to please take a quick look?

Dominic Staff
replied 10 years ago

if you still face there issue. You can send me your site for further checking.

Mingjie Li
answered 8 years ago

@Dominic

could I ask how to make the question list page right Sidebar to show the

Question Category, just like this page right Sidebar : https://www.designwall.com/question/

And We have created Widgets the these steps https://www.designwall.com/question/ask-a-question-button/#answer-22384

Widgets can be created , and can be added Sidebar , but the question list page can not show Question Category .

https://uploads.intercomcdn.com/i/o/6534736/c1ed5411228d2df3a47ff324/333%2520.png

How to make it appear .

Thanks

JK

Dominic Staff
replied 8 years ago

At the moment, we have some change in layout and style. and this solution just worked with the older version. I don’t think it fully compatible with the latest version. If you want to resolve this issue, you can send me username & password of your site for further checking. After checking, I will help you.

Powered by DW Question & Answer Pro