• wordpress调用的文章每5篇用ul包装起来,php代码怎么写循环?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年11月06日

    实现这个其实没有什么技术难点,主要就是靠思路,下面是其中的一个思路,您可以参考一下:

    <?php $toutal=4;?><!--先设置准备显示几个每5条一个ul包裹起来的块儿,如果是要把所有文章都按照这样的格式显示出来,那就可以通过查询先获取到符合条件的文章的总数,然后用总数除以5得到的就是一共显示的每5条一个ul包裹起来的块儿-->
    <?php for ($i=1; $i<=$toutal; $i++) {?><!--最外层使用for循环,循环输出每5条一个ul包裹起来的块儿-->
        <ul>
    	    <?php
    	        $offset = ($i-1)*5;//设置每次循环需要的偏移量
    			$args=array(
    			    'post_type'=> 'post',
    			    'posts_per_page' => 5,
    			    'offset' => $offset,
    			);
    			$wp_query = new WP_Query( $args );
    			if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
    		?>
    		    <li><?php the_title();?></li>
    		<?php endwhile; endif; wp_reset_postdata();?>
    	</ul>
    <?php }?>

    上面的方法有个弊端就是,默认的分页功能是没法和这个结合使用的,如果需要使用分页功能,那就需要自己租写分页功能了。

    使用上面的方面实现的效果就如下图一样:

    图片中我是每两条用一个ul包裹起来了,测试文章比较少,哈哈

  • WordPress如何获取自定义文章类型的所有文章

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年10月31日

    想要获取WordPress自定义文章类型的文章你可以使用WP_Query类或者query_posts(),相比较之下WP_Query类参数更加灵活丰富,所以我们可以选择WP_Query类,使用WP_Query类获取指定自定义文章类型的文章最重要的就是post_type这个参数的设置,这个参数值的设置支持单个文章类型,也支持多个文章类型组成的数组,比如post_type=>’product’,或者post_type=>array(‘post’,’product’)。整体代码使用举例如下:

    <?php  
    $args = array(
        'post_type' => 'product',//product就是你自定义文章类型的名称,如果需要同时获取多个文章类型的文章,可以使用array('post','product')
        'posts_per_page' => 10,//获取的文章数量,如果要获取全部文章,该参数值设置成 -1 即可
        //如需其他查询参数自行设置就可以了
    ); 
     
    // 自定义查询
    $the_query = new WP_Query( $args );
     
    // 判断查询的结果,检查是否有文章
    if ( $the_query->have_posts() ) :
     
        // 通过查询的结果,开始主循环
        while ( $the_query->have_posts() ) :
            $the_query->the_post(); //获取到特定的文章
     
            // 要输出的内容,如标题、日期等 
     
        endwhile;
    endif;
     
    // 重置请求数据
    wp_reset_postdata(); 
    ?>

     

  • WordPress如何制作自定义分类法分类列表模板

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年10月27日

    如果你想给你自己创建的自定义分类法的分类列表做一个自定义的列表模板,你只需要在你的主题文件夹中创建一个taxonomy-分类法名称.php,然后编辑这个文件就可以了,比如你的自定义分类法的名字是product_cat,那么你就创建一个taxonomy-product_cat.php文件,然后文件里按照你的需要获取文章列表进行展示就行了,希望对您有所帮助。

  • wordpress如何获取自定义分类法分类列表?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年10月27日

    这个其实很简单的,你可以使用get_terms()这个函数,获取到指定自定义分类法下的所有或指定条件的分类的对象,然后使用foreach循环输出单个分类的信息,并用列表形式展示就行了。具体的你可以百度一下get_terms()这个函数的使用方法,希望能够帮助到您!

  • 如何使用WordPress制作多语言网站主题?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年10月11日

    使用WordPress制作多语言网站还是比较方便的,可以使用多语言插件方式,也可以使用WordPress的多站点方式来实现,其中使用WordPress多语言插件方式制作WordPress多语言网站的方式是比较常用的,你可以了解一下Polylang这个多语言插件,我们给客户制作的多语言站点很多就是使用的这个插件

  • WordPress开发中如何禁止默认用户角色进入后台?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年09月14日

    把下面的代码放到您的functions.php中就可以了,希望能够帮助到你

    //禁止默认用户角色进入仪表盘
    if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) {
      $current_user = wp_get_current_user();
      if($current_user->roles[0] == get_option('default_role')) {
        wp_safe_redirect( home_url() );
        exit();
      }
    }
  • WordPress如何让时间显示为多久以前这样的格式?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年09月14日

    WordPress 本身调用时间的函数 the_time() 只能直接调用时间,通过他的 filter,我们可以让他显示为比较科学的几天前格式。

    将下边的代码丢到 function.php 的最后一个 ?> 前即可。

    function Bing_filter_time(){
    	global $post ;
    	$to = time();
    	$from = get_the_time('U') ;
    	$diff = (int) abs($to - $from);
    	if ($diff <= 3600) {
    		$mins = round($diff / 60);
    		if ($mins <= 1) {
    			$mins = 1;
    		}
    		$time = sprintf(_n('%s 分钟', '%s 分钟', $mins), $mins) . __( '前' , 'Bing' );
    	}
    	else if (($diff <= 86400) && ($diff > 3600)) {
    		$hours = round($diff / 3600);
    		if ($hours <= 1) {
    			$hours = 1;
    		}
    		$time = sprintf(_n('%s 小时', '%s 小时', $hours), $hours) . __( '前' , 'Bing' );
    	}
    	elseif ($diff >= 86400) {
    		$days = round($diff / 86400);
    		if ($days <= 1) {
    			$days = 1;
    			$time = sprintf(_n('%s 天', '%s 天', $days), $days) . __( '前' , 'Bing' );
    		}
    		elseif( $days > 29){
    			$time = get_the_time(get_option('date_format'));
    		}
    		else{
    			$time = sprintf(_n('%s 天', '%s 天', $days), $days) . __( '前' , 'Bing' );
    		}
    	}
    	return $time;
    }
    add_filter('the_time','Bing_filter_time');

    上边的代码可以让 30 天内发布的文章显示为几天前,而过了 30 天即显示为正常的标准格式日期。

  • WordPress前端投稿时怎么插入文章标签?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年09月14日

    使用插入文章中的tags_input参数配置就可以了,可以参考如下代码:

    $tougao = array(
        'ID' => $post_id,
        'post_title' => $title,
        'post_content' => $content,
        'post_category' => array($zhuanlan),
        'tags_input' => $tags,  //格式如:array("mobantu,mbt"),所以变量$tags应该是个数组形式,也就是要求从投稿表单中获取到的填入的标签变成数组形式赋值给变量$tags
        'post_author' => $current_user_id,
        'post_status' => 'pending',
    );
    $status = wp_insert_post( $tougao );

     

  • WordPress如何获取指定分类(不是分类下的文章)并进行分页

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年09月14日

    可以参考下面的代码实现对分类的分页

    <?php 
        $taxonomy = 'product_cat';
        $num = wp_count_terms($taxonomy,array(
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
        ));
        $page=$_GET['page'];//当前是第几页
        $pagesize=20;//每页显示记录数量
        $pages=($page-1)*$pagesize;//偏移量
        $nums=ceil($num/$pagesize);//总页数
        if(!$_GET['page']){
            $page=1;
            $pages=0;
        };
    ?>
    <ul class="courselist_out">
        <?php $terms = get_terms( array(
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
            'number' => $pagesize,
            'offset' => $pages,
        ));?>
        <?php if($terms){?>
            <?php foreach ($terms as $term) {?>
                <li>
                    <?php echo $term->name;?>
                </li>
            <?php } ?>
        <?php } ?>
        <div class="clearfix"></div>
    </ul>
    <?php $page_t=$page-1;$page_r=$page+1;?>
    <div class="page_navi text-center">
        <?php if($_GET['page'] > 1 ){?>
            <a href="<?php bloginfo('url');?>/product_cat?page=<?php echo $page_t;?>">上一页</a>
            <!--这里面的链接前缀部分根据您的具体情况调整-->
        <?php } ?>
        <?php if($nums > 1){?>
            <?php for($i=1; $i<=$nums; $i++) {?>
                <a href="<?php bloginfo('url');?>/product_cat?page=<?php echo $i;?>" class="<?php if($_GET['page'] == $i){echo "current";};?>"><?php echo $i;?></a>
                <!--这里面的链接前缀部分根据您的具体情况调整-->
            <?php } ?>
        <?php } ?>
        <?php if($nums <=1 || $_GET['page'] == $nums ){?>   
        <?php } else {?>
            <a href="<?php bloginfo('url');?>/product_cat?page=<?php echo $page_r;?>">下一页</a>
            <!--这里面的链接前缀部分根据您的具体情况调整-->
        <?php } ?>
    </div>

     

  • WordPress怎么在启用主题的时候创建一个自定义数据表?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年09月14日

    启用主题这个动作使用load-themes.php这个钩子,参考代码如下

    //创建声望记录数据表
    add_action( 'load-themes.php', 'the_table_install' );
    function the_table_install() {    
        global $wpdb; 
        $table_name = isset($table_prefix) ? ($table_prefix . 'prestige') : ($wpdb->prefix . 'prestige'); //获取表前缀,并设置新表的名称 
        if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
            $sql = " CREATE TABLE `" . $table_name . ("` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `user_id` int(11) DEFAULT NULL COMMENT '用户id',
            `post_id` int(11) DEFAULT NULL COMMENT '文章id',
            `old` int(11) DEFAULT '0' COMMENT '原始声望',
            `apply` int(11) DEFAULT NULL COMMENT '操作声望',
            `new` int(11) DEFAULT NULL COMMENT '新声望',
            `type` enum('register','login','publish_post','publish_course','comment','publish_question','answer_question','fabulous','cancel_fabulous','collect','cancel_collect') NOT NULL DEFAULT 'publish_post' COMMENT '类型:注册 登录 发文章 发教程 评论 提问 回答问题 被点赞 取消点赞 被收藏 取消收藏',
            `time` datetime DEFAULT NULL COMMENT '操作时间',
            `note` varchar(255) DEFAULT NULL COMMENT '说明备注',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=") . DB_CHARSET . (" COMMENT='声望记录表';");
            require_once (ABSPATH . ("wp-admin/includes/upgrade.php"));
            dbDelta($sql);
        }
    } 

     

  • WordPress用户投稿提交后怎么跳转到用户的文章列表页

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年09月07日

    不知道你的发布文章的提交代码是什么,你需要找到你的发布文章的代码,然后才能给你解决方法!

  • WordPress发布文章publish_post钩子对自定义文章类型不起作用怎么办?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2020年08月26日

    WordPress 的 publish_post 钩子针对的是文章(帖子)类型,适用于 WordPress 站点的 post 类型的文章。如果想要发布页面时执行某些动作,对应钩子应该是 publish_page;如果你想发布某个自定义类型的文章,如课程类型course,那么对应钩子应该是 publish_course。针对你上面的代码,你可以如下修改:

    //发布文章或提问奖励声望
    function publish_article_prestige( $post_ID ){
        global $wpdb; 
        $user_id = get_post($post_ID)->post_author; 
        $old_pretige = get_user_meta( $user_id,"shengwang",true);
        $apply_pretige = 5;
        $post_cat = get_the_category($post_ID)[0]->cat_ID;
        if($post_cat == 2){
          $publish_type = "publish_question";
        } else {
          $publish_type = "publish_post";
        }
        $date = date('Y-m-d H:i:s');
        $new_pretige = $old_pretige+$apply_pretige;
        $wpdb->insert('wp_prestige_log', array(
            'user_id' => $user_id,
            'post_id' => $post_ID,
            'old' => $old_pretige, 
            'apply' => $apply_pretige, 
            'new' => $new_pretige, 
            'type' => $publish_type, 
            'time' => $date, 
        ));
       update_user_meta($user_id,'shengwang',$new_pretige);
    }
    add_action('publish_post','publish_article_prestige');
    
    //发布课程(自定义文章类型)奖励声望
    function publish_course_prestige( $post_ID ){
        global $wpdb; 
        $post_type = get_post_type( $post_ID );
        $user_id = get_post($post_ID)->post_author; 
        $old_pretige = get_user_meta( $user_id,"shengwang",true);
        $apply_pretige = 10;
        $publish_type = "publish_course";
        $date = date('Y-m-d H:i:s');
        $new_pretige = $old_pretige+$apply_pretige;
        $wpdb->insert('wp_prestige_log', array(
            'user_id' => $user_id,
            'post_id' => $post_ID,
            'old' => $old_pretige, 
            'apply' => $apply_pretige, 
            'new' => $new_pretige, 
            'type' => $publish_type, 
            'time' => $date, 
        ));
       update_user_meta($user_id,'shengwang',$new_pretige);
    }
    add_action('publish_course','publish_course_prestige');