رفتن به مطلب

مشکل با زمینه های دلخواه


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

سلام و درود

دوستان گرامی، چرا زمینه دلخواه با اینکه توی حلقه نوشته شده کار نمیکنه؟

<?php echo get_post_meta($post->ID, 'price', true); ?>

همیشه کار کرده ولی الان نه.

 

widgets.php

ویرایش شده توسط unid_user
لینک به ارسال
<?php
add_action( 'widgets_init', create_function('', 'return register_widget("hp_recent_posts");') );
class hp_recent_posts extends WP_Widget {
    public function __construct() {
    parent::WP_Widget( false, 'دسته بندی های صفحه اصلی',array('description' => __('نمایش آخرین دسته بندی های هر دسته در صفحه اصلی') ));
	}
    public function widget( $args, $instance ) {
        extract( $args );
        $headline = $instance['headline'];
        $category = $instance['category'];
        $numberposts = $instance['numberposts'];
        echo $before_widget;
        
		$the_query = new WP_Query( array( 'category_name' => $category, 'posts_per_page' => $numberposts ) );
		if ( $the_query->have_posts() ) {
			echo '<section class="mt-5">';
			echo $before_title;
					echo "<h3 class='text-center text-dark sec-title'>$headline</h3>";
       echo $after_title;
			echo '<div class="container"><div class="row">';
			
			$i =0 ;
			while ( $the_query->have_posts() ) {
				$the_query->the_post(); $i++;
				if ($i == 1 ) {
					echo '<div class="col-lg-6 d-flex align-items-stretch">'; ?>
					<div class="card mb-2">
						<?php the_post_thumbnail('post_thumbnail', ['class' => 'card-img-top']); ?>
						<div class="card-body">
							<h5 class="card-title"><?php the_title(); ?></h5>
							<a href="<?php the_permalink(); ?>" class="btn btn-success float-start">کد تخفیف</a>
							<span class="offer-icon">
								<?php echo get_post_meta($post->ID, 'offer_percent', true); ?>
							</span>
							<span class="text-muted"> | </span>
							<span class="text-success"><?php echo get_post_meta($post->ID, 'price', true); ?></span>
							<span class="text-muted"> | </span>
							<span> <?php echo getPostViews(get_the_ID()); ?> </span>
							<span class="view-icon"></span>
						</div>
					</div>
					<?php echo '</div>';
				}
				if ($i == 2 ) {
					echo '<div class="col-lg-6">'; ?>
						<div class="row">
						<div class="col-lg-6">
							<div class="card mb-2 ux-card-small">
								<?php the_post_thumbnail('post_thumbnail', ['class' => 'card-img-top']); ?>
								<div class="card-body">
									<h5 class="card-title"><?php the_title(); ?></h5>
									<a href="<?php the_permalink(); ?>" class="btn btn-success float-start">کد تخفیف</a>
									<span class="offer-icon">
										<?php echo get_post_meta($post->ID, 'offer_percent', true); ?>
									</span>
									<span class="text-muted"> | </span>
									<span class="text-success"><?php echo get_post_meta($post->ID, 'price', true); ?></span>
									<span class="text-muted"> | </span>
									<span> <?php echo getPostViews(get_the_ID()); ?> </span>
									<span class="view-icon"></span>
								</div>
							</div>
						</div>
					<?php
				}
				if ($i == 3 ) {
					?>
					<div class="col-lg-6">
							<div class="card mb-2 ux-card-small">
								<?php the_post_thumbnail('post_thumbnail', ['class' => 'card-img-top']); ?>
								<div class="card-body">
									<h5 class="card-title"><?php the_title(); ?></h5>
									<a href="<?php the_permalink(); ?>" class="btn btn-success float-start">کد تخفیف</a>
									<span class="offer-icon">
										<?php echo get_post_meta($post->ID, 'offer_percent', true); ?>
									</span>
									<span class="text-muted"> | </span>
									<span class="text-success"><?php echo get_post_meta($post->ID, 'price', true); ?></span>
									<span class="text-muted"> | </span>
									<span> <?php echo getPostViews(get_the_ID()); ?> </span>
									<span class="view-icon"></span>
								</div>
							</div>
						</div>
						</div>
					<?php
				}
				if ($i == 4 ) {
					?>
					<div class="row">
					<div class="col-lg-6">
						<div class="card mb-2 ux-card-small">
							<?php the_post_thumbnail('post_thumbnail', ['class' => 'card-img-top']); ?>
							<div class="card-body">
								<h5 class="card-title"><?php the_title(); ?></h5>
								<a href="<?php the_permalink(); ?>" class="btn btn-success float-start">کد تخفیف</a>
								<span class="offer-icon">
									<?php echo get_post_meta($post->ID, 'offer_percent', true); ?>
								</span>
								<span class="text-muted"> | </span>
								<span class="text-success"><?php echo get_post_meta($post->ID, 'price', true); ?></span>
								<span class="text-muted"> | </span>
								<span> <?php echo getPostViews(get_the_ID()); ?> </span>
								<span class="view-icon"></span>
							</div>
						</div>
					</div>
					<?php
				}
				if ($i == 5 ) {
					?>
						<div class="col-lg-6">
							<div class="card mb-2 ux-card-small">
								<?php the_post_thumbnail('post_thumbnail', ['class' => 'card-img-top']); ?>
								<div class="card-body">
									<h5 class="card-title"><?php the_title(); ?></h5>
									<a href="<?php the_permalink(); ?>" class="btn btn-success float-start">کد تخفیف</a>
									<span class="offer-icon">
										<?php echo get_post_meta($post->ID, 'offer_percent', true); ?>
									</span>
									<span class="text-muted"> | </span>
									<span class="text-success"><?php echo get_post_meta($post->ID, 'price', true); ?></span>
									<span class="text-muted"> | </span>
									<span> <?php echo getPostViews(get_the_ID()); ?> </span>
									<span class="view-icon"></span>
								</div>
							</div>
						</div>
					</div>
					<?php
				}
			}
					echo '</div>';
				echo "</div>";
			echo "</div>";
			echo "</section>";
		wp_reset_postdata();
		}
        echo $after_widget;
    }
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['headline'] = ( $new_instance['headline'] );
        $instance['category'] = ( $new_instance['category'] );
        $instance['numberposts'] = ( $new_instance['numberposts'] );
        return $instance;
    }
    public function form( $instance ) {
        $headline = $instance[ 'headline' ];
        $category = $instance[ 'category' ];
        $numberposts = $instance[ 'numberposts' ];
		$categories = get_categories(array('type'=>'post','orderby'=> 'name','order'=> 'ASC'));
		$defaults = array( 'numberposts' => 3 );
		$instance = wp_parse_args( (array) $instance, $defaults );
        echo '<p>';
		echo '<label for="'. $this->get_field_id( 'headline' ) .'">'. _e( 'عنوان : ' ) .'</label>';
		echo '<input class="widefat" id="'. $this->get_field_id( 'headline' ) .'" name="'. $this->get_field_name( 'headline' ) .'" type="text" value="'. esc_attr( $headline ) .'" />';
		echo '</p>';
		echo '<p>';
		echo '<label for="'. $this->get_field_id( 'category' ) .'">'. _e( 'انتخاب دسته بندی : ' ) .'</label>';
		echo '<select name="' . $this->get_field_name('category') . '" id="' . $this->get_field_id('category') . '" style="width: 100%;">';
			foreach($categories as $category):
				echo '  <option value="' . $category->slug .'" '. selected($category->slug, $instance['category'], false) . '>' . $category->name . '</option>';        
			endforeach;
		echo '</select>';
		echo '</p>';
		echo '<p>';
		echo '<label for="'. $this->get_field_id( 'numberposts' ) .'">'. _e( 'تعداد نوشته‌هایی که نمایش داده می‌شود:' ) .'</label>';
		echo '<input class="tiny-text" id="'. $this->get_field_id( 'numberposts' ) .'" name="'. $this->get_field_name( 'numberposts' ) .'" type="number" value="'. $instance['numberposts'] .'" step="1" min="1" size="3" />';
		echo '</p>';
    }
}

 

لینک به ارسال
در 9 دقیقه قبل، asadiy4n گفته است :

سلام

ایدی و مقدار رو داخل متغیر ذخیر کنین و تست کنید

سپاس. درست شد.

لینک به ارسال
  • 8 ماه بعد...

درود

این تکه کد تو ورژن جدید وردپرس پشتیبانی نمیشه؟ خطا میده که تابع create_function تعریف نشده.

<?php
add_action( 'widgets_init', create_function('', 'return register_widget("hp_recent_posts");') );
class hp_recent_posts extends WP_Widget {
    public function __construct() {
    parent::WP_Widget( false, 'دسته بندی های صفحه اصلی',array('description' => __('نمایش آخرین دسته بندی های هر دسته در صفحه اصلی') ));
	}
    public function widget( $args, $instance ) {
        extract( $args );
        $headline = $instance['headline'];
        $category = $instance['category'];
        $numberposts = $instance['numberposts'];
        echo $before_widget;
        $category_id = get_cat_ID($category);
        $category_url = get_category_link($category_id);
        echo '<a href="'.$category_url.'">'.$category.'</a>';

		$the_query = new WP_Query(
		array(
			'category_name' => $category,
			'posts_per_page' => $numberposts
			)
		);
		if ( $the_query->have_posts() ) {
			$i =0 ;
			while ( $the_query->have_posts() ) {
				$the_query->the_post(); $i++;
				//echo $ads_price;
				if ($i == 1 ) {
				}
				if ($i == 2 ) {
				}
				if ($i == 3 ) {
				}
				if ($i == 4 ) {
				}
				if ($i == 5 ) {
				}
			}
		wp_reset_postdata();
		}
        echo $after_widget;
    }
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['headline'] = ( $new_instance['headline'] );
        $instance['category'] = ( $new_instance['category'] );
        $instance['numberposts'] = ( $new_instance['numberposts'] );
        return $instance;
    }
    public function form( $instance ) {
        $headline = $instance[ 'headline' ];
        $category = $instance[ 'category' ];
        $numberposts = $instance[ 'numberposts' ];
		$categories = get_categories(array('type'=>'post','orderby'=> 'name','order'=> 'ASC'));
		$defaults = array( 'numberposts' => 3 );
		$instance = wp_parse_args( (array) $instance, $defaults );
        echo '<p>';
		echo '<label for="'. $this->get_field_id( 'headline' ) .'">'. _e( 'عنوان : ' ) .'</label>';
		echo '<input class="widefat" id="'. $this->get_field_id( 'headline' ) .'" name="'. $this->get_field_name( 'headline' ) .'" type="text" value="'. esc_attr( $headline ) .'" />';
		echo '</p>';
		echo '<p>';
		echo '<label for="'. $this->get_field_id( 'category' ) .'">'. _e( 'انتخاب دسته بندی : ' ) .'</label>';
		echo '<select name="' . $this->get_field_name('category') . '" id="' . $this->get_field_id('category') . '" style="width: 100%;">';
			foreach($categories as $category):
				echo '  <option value="' . $category->name .'" '. selected($category->name, $instance['category'], false) . '>' . $category->name . '</option>';
			endforeach;
		echo '</select>';
		echo '</p>';
		echo '<p>';
		echo '<label for="'. $this->get_field_id( 'numberposts' ) .'">'. _e( 'تعداد نوشته‌هایی که نمایش داده می‌شود:' ) .'</label>';
		echo '<input class="tiny-text" id="'. $this->get_field_id( 'numberposts' ) .'" name="'. $this->get_field_name( 'numberposts' ) .'" type="number" value="'. $instance['numberposts'] .'" step="1" min="1" size="3" />';
		echo '</p>';
    }
}

 

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

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

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

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

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

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

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

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

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

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