• 为什么使用register_sidebar_widget()创建的自定义小工具只能使用一次?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2022年01月15日

    register_sidebar_widget()创建的小工具算是临时创建了一个小工具,他的特点就是只能使用一次,正确创建可重复使用的小工具你需要使用创建小工具基础类WP_Widget创建继承类的方式来实现,比如下面是一个创建投稿链接的一个小工具。

    首先,讲下面的代码放到一个你自己命名的php文件中,比如tougao.php,然后放到你的主题中(具体放的位置根据你的需要)。

    <?php
    class WP_Game_Tougao_Widget extends WP_Widget {
        /**
         * Constructor
         */
        function __construct() {
          $widget_ops = array('classname' => 'mx_game_tougao', 'description' => '投稿链接');
          parent::__construct('mx_game_tougao', $name='投稿链接', $widget_ops);
        }
    
        function widget($args, $instance) {
            extract( $args );
            $title = apply_filters('widget_title', empty($instance['title']) ? '投稿链接' : $instance['title']);
            $tougao_description = strip_tags($instance['tougao_description']);
            $tougao_pagelink = strip_tags($instance['tougao_pagelink']);
        ?>
            <?php echo $before_widget; ?>
                <div class="game-tougao-widget-box">
                    <h3><?php echo $title;?></h3>
                    <p><?php echo $tougao_description;?></p>
                    <a href="<?php echo $tougao_pagelink;?>"><i class="iconfont icon-icon_fabu"></i><span>立即投稿</span></a>
                </div>
            <?php echo $after_widget; ?>
        <?php }
      
        function form($instance) {
          $title = strip_tags($instance['title']);
          $tougao_description = strip_tags($instance['tougao_description']);
          $tougao_pagelink = strip_tags($instance['tougao_pagelink']);
        ?>
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_attr_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 $title; ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id('tougao_description'); ?>">投稿描述:</label>
                <textarea class="widefat" id="<?php echo $this->get_field_id('tougao_description'); ?>" name="<?php echo $this->get_field_name('tougao_description'); ?>"><?php echo $tougao_description;?></textarea>
            </p>
            <p>
                <label for="<?php echo $this->get_field_id('tougao_pagelink'); ?>">投稿页面链接:</label>
                <input class="widefat" id="<?php echo $this->get_field_id('tougao_pagelink'); ?>" name="<?php echo $this->get_field_name('tougao_pagelink'); ?>" type="text" value="<?php echo $tougao_pagelink; ?>" />
            </p>
        <?php }
      
        function update($new_instance, $old_instance) {
            $instance = $old_instance;
            $instance['title'] = strip_tags($new_instance['title']);
            $instance['tougao_description'] = strip_tags($new_instance['tougao_description']);
            $instance['tougao_pagelink'] = strip_tags($new_instance['tougao_pagelink']);
            return $instance;
        }
    }
    register_widget('WP_Game_Tougao_Widget');//把你写的小工具文件创建成小工具
    ?>

    然后,在你的functions.php文件中引入这个文件就可以了。回头时间允许了,我们会针对创建自定义小工具录制一期视频教程。

  • wordpress登录提示错误:Cookies被阻止或者您的浏览器不支持有什么解决办法啊

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2021年12月24日

    你可以在wp_config.php 中最后一行增加一句cookie域定义

    define('COOKIE_DOMAIN', '.YouDomainName.com');

    注意一下域名签名有个

  • WooCommerce Product Add-ons

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2021年12月22日

    @LONG379 用js控制就行了啊,默认用css给上面的价格部分display:none;然后用js监控input框当有输入内容的时候,给这个input对应的价格部分添加display:block样式

  • 请教关于.htaccess伪静态的问题

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2021年12月18日

    有没有装什么缓存插件?如果有的话禁用一下试试看。

  • 如何根据附件的url路径获取附件ID?

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

    其中一种方法就是自定义一个函数,查询数据库来获取,代码如下:

    /**
    *根据附件url获取附件ID 
    *WordPress中文社区 https://www.wpshequ.cn
    */
    function mx_get_attachment_id($image_src){
        global $wpdb; 
        $postid_img  = $wpdb->get_var(
            "SELECT ID FROM $wpdb->posts 
             WHERE guid = '$image_src' 
             AND post_type='attachment' LIMIT 1");
        return $postid_img;
    }

    在你需要的地方使用函数mx_get_attachment_id(‘图片url’)来获取就行了。

    希望对你有帮助。

  • 如何获取WordPress指定文章中所有图片路径?

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

    不多说了,直接上代码,把下面代码放到你主题的functions.php中

    /**
    *获取当前文章中所有图片 
    *WordPress中文社区 https://www.wpshequ.cn
    */
    function mx_get_post_images(){
        global $post;
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $post->post_content, $matches, PREG_PATTERN_ORDER);
        return $matches[1];
    }

    然后再你需要使用图片url的地方使用mx_get_post_images()这个函数获取出来,然后循环输出成你需要的样式就行了。希望对你有帮助!

  • CSS绝对定位元素宽度如何实现自适应?

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

    给绝对定位的这部分添加一个word-break: keep-all就可以了

  • WordPress主题开发中怎样给自定义文章类型添加post_tag分类法?

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

    假如说你创建的自定义文章类型的名字为question,那么你只需要把下面的代码放到您的主题functions.php文件中就行了。

    add_action( 'init', 'wpshequ_add_post_tag_taxonomy_to_post_type' );
    function wpshequ_add_post_tag_taxonomy_to_post_type() {
        register_taxonomy_for_object_type( 'post_tag', 'question' );
        //如果你还想把默认的cateory分类法(默认文章分类)添加到question文章类型,可以再添加下面的代码
        //register_taxonomy_for_object_type( 'category', 'question' );
    }

     

  • 想问下博主的视频课程板块内容是用wp那个自定义字段插件开发的还是其他?

    最佳
    WordPress日记
    超哥
    行业大佬
    行业大佬
    时间: 2021年02月02日

    @dongdongxiao https://www.wpshequ.cn/course_cat/主题开发 这个页面就是创建了一个页面模板,然后程序获取所有课程(每个课程是创建了一个分类),然后显示出来就行了。

    https://www.wpshequ.cn/course_cat/wp-development-video-collection/ 这个就是列表页,因为每个课程名称是一个分类,下面具体章节就是一篇一篇的文章,当然这个页面有用到ACF这个自定义字段插件,用来展示课程摘要和课程简介。

  • 如何给WordPress分类目录链接添加上斜杠?

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

    您只需要把下面的代码放到您的functions.php文件中,然后再重新保存一下WordPress的固定连接就可以了。

    // 给WordPress分类目录链接添加斜杠
    function mx_trailingslashit($string, $type_of_url) {
     if ( $type_of_url != 'single' )
     $string = trailingslashit($string);
     return $string;
    }
    add_filter('user_trailingslashit', 'mx_trailingslashit', 10, 2);
  • 如何让WordPress的搜索在搜索的时候也支持去查询文章的评论中是否包含搜索关键词?

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

    如果您的搜索使用的是WordPress默认的搜索的话,可以把下面的代码放到您的functions.php中来实现,但是可能对于自定义搜索不起作用。

    /** 
    * WordPress搜索文章支持查询评论 
    * https://www.wpshequ.cn 
    */ 
    function sort_by_sticky( $query ) {
        if($query->is_search && !empty($query->query['paged'])){
            $search = htmlspecialchars($query->query['s']);
            global $wpdb;
            $querystr = "
            select $wpdb->posts.ID
                       from $wpdb->posts
            left join $wpdb->comments
                       on $wpdb->comments.comment_post_ID = $wpdb->posts.ID
                       where $wpdb->posts.post_content like \"%$search%\"
                       or $wpdb->comments.comment_content like \"%$search%\"
                       or $wpdb->posts.post_title like \"%$search%\"
                       or $wpdb->comments.comment_author like \"%$search%\"
                       group by $wpdb->posts.ID
            ";
            $match_posts = $wpdb->get_results($querystr, OBJECT);
            $query->set('s', '');
            $match_post_ids = array();
            foreach($match_posts as $match_post){
                $match_post_ids[] = $match_post->ID;
            }
            $query->set('post__in', $match_post_ids);
        }
    }
    add_action( 'pre_get_posts', 'sort_by_sticky' );

     

  • WordPress主题开发中如何获取指定分类法下的文章总数?

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

    这个可以通过get_terms()函数先获取出来该分类法下所有的分类信息,然后通过循环并求和返回值count,得到该分类法下所有文章总数,具体实现方法是,把下面的代码放到您的functions.php中:

    //获取指定分类法下文章总数
    //WordPress中文社区-https://www.wpshequ.cn
    function mx_get_taxonomy_postcount($taxonomy) {
        $terms = get_terms( array(
    	    'taxonomy' => $taxonomy,
    	    'hide_empty' => true,
    	) );
    	foreach ($terms as $term) {
    	  // 子孙分类文章数累加
    	  $count +=$term->count;
    	}
        return $count;
    }

    然后在使用的地方使用<?php echo mx_get_taxonomy_postcount(‘分类法别名’);?>可以直接显示该分类法下的文章总数,希望对您有所帮助