php技术博客
让天下没有搞不定的bug~

wordpress根据用户id判断是否为管理员

zone阅读(1618)

wordpress 二次开发过程中,经常会用到这个问题,就是根据用户id判断是否为管理员。具体代码如下:

//判断是否为管理员
function ludou_is_administrator($user_id) {
  $user = get_userdata($user_id);
  if(!empty($user->roles) && in_array('administrator', $user->roles))
    return 1;  // 是管理员
  else
    return 0;  // 非管理员
}

wordpress首页根据自定义字段排行,同时传递搜索参数

zone阅读(1255)

wordpress首页根据自定义字段排行,同时传递搜索参数,这里是通过自定义字段浏览量views来查询,通过全局变量global $wp_query;然后进行查询条件去重即可。

  <?php
  global $wp_query;
  $pageNum = get_option('posts_per_page');
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $query = array(
    'posts_per_page' => $pageNum,
    'meta_key' => 'views',// 自定义栏目名称 
    'orderby' => 'meta_value_num', // 自定 义栏目值 
    'order'   => 'DESC',
    'paged' => $paged,
  );
  query_posts(array_merge($wp_query->query_vars, $query));

	while ( have_posts() ) : the_post();
		get_template_part( 'content');
	endwhile;

	// Pagination
	the_posts_pagination( array(
				'mid_size' => 5,
				'prev_text'          => esc_html__( 'Previous page', 'wordstar' ),
				'next_text'          => esc_html__( 'Next page', 'wordstar' ),
				'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page:', 'wordstar' ) . ' </span>',
				'screen_reader_text' =>  esc_html__( 'Pagination', 'wordstar' ) 
	) );
}else {
		get_template_part( 'content', 'none' );
};
//wp_reset_query();
?>

wordpress读取某个指定时间之前的文章

zone阅读(1121)

wordpress读取某个指定时间之前的文章,我这里是读取当前文章时间之前的文章。用before来限制时间即可。
    $post_num   =   8;
    $exclude_id =   $post->ID;
    //根据时间约束
    $date_query=array(
         array(
         'column' => 'post_date',
         'before' => date('Y-m-d H:i',strtotime($post->post_date)),  //在此文章发布时间之前,包括当天
         //'after'  => date('Y-m-d H:i',time()-3600*24*30)   //在此时间之后
         )
    );
    $posttags   =   get_the_tags();
    $i          = 0;
    if ($posttags) {
        $tags = '';
        foreach ($posttags as $tag) $tags.= $tag->term_id . ',';
        $args = array(
            'post_status'           =>  'publish',
            'tag__in'               =>  explode(',', $tags) ,
            'post__not_in'          =>  explode(',', $exclude_id) ,
            'ignore_sticky_posts'   =>  1,
            'orderby'               =>  'comment_date',
            'posts_per_page'        =>  $post_num,
            'date_query'            =>  $date_query,

        );
        query_posts($args);
        while (have_posts()) {
            the_post(); ?><div class="broke-product"><div class="pro-inner"><div class="broke-img" style="background:url(<?php
            uctheme_post_thumbnail_src(); ?>)no-repeat;background-size: 100% 100%;"><a href="<?php
            the_permalink(); ?>"></a></div><div class="broke-info"><a href="<?php
            the_permalink(); ?>"><h4><?php
            echo mb_strimwidth(get_the_title() , 0, 30, "..."); ?></h4><div class="broke-tips"><span class="broke_qa">¥</span><em><?php
            echo floatval(get_post_meta($post->ID, "quanhou_value", true)); ?></em><div class="broke_yjia">券后价</div></div><div class="broke_qquan"><span class="broke_quan">领券减:<i class="broke_huij"><?php
            echo floatval(get_post_meta($post->ID, 'youhui_value', true)); ?>元</i></span><div class="broke_lingquan"><?php
            echo get_post_meta($post->ID, "sale_value", true); ?>人购买</div></div></a></div></div></div><?php
            $exclude_id.= ',' . $post->ID;
            $i++;
        }
        wp_reset_query();
    }

WordPress通过文章标题获取文章 ID 、时间、内容等 post 信息

zone阅读(1144)

//格式
get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = 'page' )
 
$page_title 页面标题
$output 返回的类型格式,例如 OBJECT,ARRAY_A 或 ARRAY_N 选一个,默认是 OBJECT
$post_type 文章类型,默认为 page 页面,查询文章为 post
 
//WordPress 通过文章标题获取 post 信息
$post = get_page_by_title('这里就是文章的标题', OBJECT, 'post');
$postid = $post->ID; //文章 ID


ID          int     文章 ID
post_author     string  作者 user ID
post_name   string  文章别名
post_type   string  文章类型
post_title  string  文章标题
post_date   string  文章时间,格式为: 0000-00-00 00:00:00
post_date_gmt   string 文章 gmt 时间,格式为,: 0000-00-00 00:00:00
post_content    string  文章内容
post_excerpt    string  文章摘要
post_status     string  文章状态
comment_status  string  评论状态,开启或者关闭: { open, closed }
ping_status     string  ping 状态 { open, closed }
post_password   string  文章密码
post_parent     int     父 ID,默认是 0
post_modified   string  文章修改时间,格式为: 0000-00-00 00:00:00
post_modified_gmt   string  文章修改时间,格式为: 0000-00-00 00:00:00
comment_count   string  文章评论统计
menu_order  string  好像没什么用,默认是 0

WordPress替换编辑文章默认的“在此输入标题”文本

zone阅读(1107)

先说说应景场景,我们在插件开发时经常用到Wordpress的自定义文章类型,在WordPress后台编辑文章时,标题默认提示文本是“在处输入标题”,而自定义的文章类型则需要替换这个文本,提升插件可用性,同时能优化一下用户体验。

/**
 * @param string  $text 显示文本
 * @param WP_Post $post 当前文章类型.
 * @return string
 */
add_filter( 'enter_title_here', 'Yangjunwei_change_title_text', 1, 2 ); //一般调用
add_filter( 'enter_title_here', array( $this, 'yjw_change_title_text' ), 1, 2 ); //class类内部调用
function Yangjunwei_change_title_text( $text, $post ) {
	switch ( $post->post_type ) {
               //这里是判断文章类型
		case 'post':
			$text = esc_html__( '请输入申请人名称', 'zoneself' );
			break;
		case 'product':
			$text = esc_html__( '请输入产品名称', 'zoneself' );
			break;
	}
	return $text;
}

我们找到这个文本的hook是“enter_title_here”,于是替换代码自然就有了。

如果在默认文章类型中修改时,可直接去掉 $post 参数。

在WordPress代码内如何禁用自动格式化

zone阅读(875)

在WordPress代码内如何禁用自动格式化

remove_filter( 'the_content', 'wpautop' );
add_filter('the_content','wpautop' ,12); 

先移除,然后调整执行顺序即可~