رفتن به مطلب

استفاده از کوئری در ابزارک


پست های پیشنهاد شده

سلام دوستان آیا برای استفاده از دستورات کوئری میشه آنها رو در ابزارک متن استفاده کرد؟

نکته: افزونه پشتیبانی php رو در ابزارک فعال کردم.

یا اینکه باید یک سایدار استاتیک تعریف کنم؟

بهترینش کدوم کاره؟

لینک به ارسال

بهترینش ساخت یک ابزارک دلخواهه


class MyNewWidget extends WP_Widget {
function MyNewWidget() {
// مشخصات پنلی که درز صفحه ابزارک ها می بینید
parent::__construct( false, 'My New Widget Title' );
}
function widget( $args, $instance ) {
// کد خروجی مورد نظر
}
function update( $new_instance, $old_instance ) {
// گزینه های متغیر ابزارک برای ذخیره سازی
}
function form( $instance ) {
//خروجی فرم
}
}
function myplugin_register_widgets() {
register_widget( 'MyNewWidget' );
}
add_action( 'widgets_init', 'myplugin_register_widgets' );

نمونه عملی


add_action( 'widgets_init', 'my_widget' );

function my_widget() {
register_widget( 'MY_Widget' );
}
class MY_Widget extends WP_Widget {
function MY_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays the authors name ', 'example') );

$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );

$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}

function widget( $args, $instance ) {
extract( $args );
//Our variables from the widget settings.
$title = apply_filters('widget_title', $instance['title'] );
$name = $instance['name'];
$show_info = isset( $instance['show_info'] ) ? $instance['show_info'] : false;
echo $before_widget;
// Display the widget title
if ( $title )
echo $before_title . $title . $after_title;
//Display the name
if ( $name )
printf( '<p>' . __('Hey their Sailor! My name is %1$s.', 'example') . '</p>', $name );

if ( $show_info )
printf( $name );

echo $after_widget;
}
//Update the widget

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
//Strip tags from title and name to remove HTML
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['name'] = strip_tags( $new_instance['name'] );
$instance['show_info'] = $new_instance['show_info'];
return $instance;
}

function form( $instance ) {
//Set up some default widget settings.
$defaults = array( 'title' => __('Example', 'example'), 'name' => __('Bilal Shaheen', 'example'), 'show_info' => true );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
//Widget Title: Text Input.
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
//Text Input.
<p>
<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Your Name:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo $instance['name']; ?>" style="width:100%;" />
</p>

//Checkbox.
<p>
<input class="checkbox" type="checkbox" <?php checked( $instance['show_info'], true ); ?> id="<?php echo $this->get_field_id( 'show_info' ); ?>" name="<?php echo $this->get_field_name( 'show_info' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_info' ); ?>"><?php _e('Display info publicly?', 'example'); ?></label>
</p>
<?php
}
}

لینک به ارسال

من از کد زیر استفاده کردم ابزارک ساخته شد.


// Creating the widget
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
echo __( 'Hello, World!', 'wpb_widget_domain' );
echo $args['after_widget'];
}

// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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 esc_attr( $title ); ?>" />
</p>
<?php
}

// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );

اما دستورات کوئری را باید کجای ای کد قرار بدم؟

لینک به ارسال

به کلمه <?php گیر میده!

کدی که گذاشتم:

به خط اول گیر میده!


<?php
$custom_query = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'order' =>'descending',
'orderby' =>'ID',
'cat' =>'3',
'posts_per_page' =>'5',

'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
?>
<?php if($custom_query->have_posts()) :
while($custom_query->have_posts()) : $custom_query->the_post();?>
<?php the_title(); ?>
<?php endwhile;endif;?>
<?php wp_reset_query(); ?>

post-3445-0-63894200-1382987286_thumb.pn

لینک به ارسال

چون قبل از این کدها تگ پی اچ پی باز شده و هنوز بسته نشده گیر میده.

تمامی تگهای باز و بسته شدن php رو از این قسمت از کدهاتون که توی عکس مشخصه بردارید.

اینها منظورمه :

<?php 

و

?>

لینک به ارسال

میشه بگید کدوما رو بردارم.؟

هر کدوم رو برمیدارم یا کار نمیکنه یا باز خطا میده!


<?php

// Creating the widget
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
<?php
$custom_query = new WP_Query(array(
'post_status' =>'published',
'post_type' =>'post',
'order' =>'descending',
'orderby' =>'ID',
'cat' =>'61',
'posts_per_page' =>'5',
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
?>
<ul>
<?php if($custom_query->have_posts()) :
while($custom_query->have_posts()) : $custom_query->the_post();?>
<li><strong>
<?php the_title(); ?>
</strong> <a class="links" href="<?php the_permalink(); ?>"> پیوند یکتا</a>
<div class="post" id="post<?php echo the_ID();?>">
<?php the_content(); ?>
</div>
</li>
<?php endwhile;endif;?>
</ul>
<?php wp_reset_query(); ?>
echo $args['after_widget'];
}

// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">
<?php _e( 'Title:' ); ?>
</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 esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
?>

ویرایش شده توسط نـایس تم
لینک به ارسال

تست کنید

<?php

// Creating the widget

class wpb_widget extends WP_Widget {

function __construct() {

parent::__construct(

// Base ID of your widget

'wpb_widget',

// Widget name will appear in UI

__('WPBeginner Widget', 'wpb_widget_domain'),

// Widget description

array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )

);

}

// Creating widget front-end

// This is where the action happens

public function widget( $args, $instance ) {

$title = apply_filters( 'widget_title', $instance['title'] );

// before and after widget arguments are defined by themes

echo $args['before_widget'];

if ( ! empty( $title ) )

echo $args['before_title'] . $title . $args['after_title'];

// This is where you run the code and display the output

$custom_query = new WP_Query(array(

'post_status' =>'published',

'post_type' =>'post',

'order' =>'descending',

'orderby' =>'ID',

'cat' =>'61',

'posts_per_page' =>'5',

'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1

));

?>

<ul>

<?php if($custom_query->have_posts()) :

while($custom_query->have_posts()) : $custom_query->the_post();?>

<li><strong>

<?php the_title(); ?>

</strong> <a class="links" href="<?php the_permalink(); ?>"> پیوند یکتا</a>

<div class="post" id="post<?php echo the_ID();?>">

<?php the_content(); ?>

</div>

</li>

<?php endwhile;endif;?>

</ul>

<?php wp_reset_query();

echo $args['after_widget'];

}

// Widget Backend

public function form( $instance ) {

if ( isset( $instance[ 'title' ] ) ) {

$title = $instance[ 'title' ];

}

else {

$title = __( 'New title', 'wpb_widget_domain' );

}

// Widget admin form

?>

<p>

<label for="<?php echo $this->get_field_id( 'title' ); ?>">

<?php _e( 'Title:' ); ?>

</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 esc_attr( $title ); ?>" />

</p>

<?php

}

// Updating widget replacing old instances with new

public function update( $new_instance, $old_instance ) {

$instance = array();

$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

return $instance;

}

} // Class wpb_widget ends here

// Register and load the widget

function wpb_load_widget() {

register_widget( 'wpb_widget' );

}

add_action( 'widgets_init', 'wpb_load_widget' );

?>

لینک به ارسال

ممنون از همتون به خصوص آقا مرتضی درست شد مشکل از من بود. تو کد زیر آیدی 61 موجود نبود.

'cat' =>'61',

موندم چه جوری تشکر کنم به این خوبی و سرعت زیاد وقت میذارید و جواب میدید.

مرسی.

لینک به ارسال

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع ...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...