wordpress 第一个博客记录下

<?php wp_head(); ?><!--引入head文件 css 文件--> <!--//需要开启sidebar-->

<?php get_footer(); ?><!--引入footer文件 js文件--> <!--//需要开启sidebar-->

<?php get_sidebar(); ?><!--引入sidebar文件-->  <!--//需要开启sidebar-->

文章页 singular.php  :写到这里面

<?php if(have_posts()): ?>
    <?php while(have_posts()): the_post() ?>
    
          <a class="author-info"
                                        href="<?php echo get_author_posts_url(get_the_author_meta("ID")); ?>"><!--作者所有文章的url-->
                                        <?php echo get_avatar('get_the_author_meta("ID")',30) ?><!--作者的头像  30是图像大小->
                                        <?php the_author(); ?> </a><!--作者名称-->
            <?php the_time('Y-m-d') ?><!--文章发布时间-->
             <?php 
                                       if(is_single() && !is_attachment()){ //如果是文章页和不是富文本也的话执行
                                        the_category(',');//所属分类
                                        echo '<span class="dot">•</span>';
                                       }
                                     ?> 
 
        <?php if(has_post_thumbnail()){
                                    the_post_thumbnail('full'); //如果有图片  显示图片
                                } ?>
  
        <?php the_content(); ?> //显示内容
        
         <?php if(has_tag()){
                                    the_tags('小标签: ',''); //如果有标签的话 显示标签
                                } ?>  
          
<?php echo get_previous_post_link('<span>上一篇: <span> %link'); ?> <!--上一篇-->
<?php echo get_next_post_link('<span>下一篇: <span> %link'); ?>    <!--下一篇-->
 
<?php if( comments_open() || get_comments_number() ): ?> <!--添加评论功能-->
                            <?php echo comments_template();  ?>
                        <?php endif; ?> 
 
 
  <?php endwhile; ?>
<?php endif; ?>
 
 
archive.php  文章列表
<?php the_archive_title();//当前分类标题 ?>
          <a href="<?php the_permalink(); ?>"> <!--当前文章的链接-->
                                    
                                        <?php if(has_post_thumbnail()): ?>
                                            <?php  the_post_thumbnail('thumbnail'); ?><!--如果有图片 显示图片-->
                                        <?php  else: ?>
                                            <img alt='' src='<?php echo get_theme_file_uri().'/assets/img/default-thumbnail.png'; ?>'/><!--如果没有图片显示默认图片-->
                                        <?php  endif; ?>    
                                        
                                        </a>
          <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><!--当前文章的链接 当前文章标题  -->
 
          <?php if(has_excerpt()): ?>
                                        <?php the_excerpt(); ?> 
                                    <?php else: ?>      
                                <?php echo wp_trim_words( get_the_content(), 160 ); // 如果没有摘要就显示文章内容 限定160个字?>
                                <?php endif; ?>
 

首页 index.php

            <?php if(is_sticky()): ?>
                                            <span class="sticky">置顶</span><!-- 是否置顶 -->
                                        <?php endif; ?>
 
 
functions.php  非常重要
<?php


// 评论添加@
function wp_comment_add_at( $comment_text, $comment = '') {
    if( $comment->comment_parent > 0) {
    $comment_text = '@<a href="#comment-' . $comment->comment_parent .'">'.get_comment_author( $comment->comment_parent ) . '</a>: ' . $comment_text;
    }
    return $comment_text;
    }
    add_filter( 'comment_text' , 'wp_comment_add_at', 20, 2);

//如何获得WordPress文章浏览次数的统计
function record_visitors()   
{   
    if (is_singular())   
    {   
      global $post;   
      $post_ID = $post->ID;   
      if($post_ID)   
      {   
          $post_views = (int)get_post_meta($post_ID, 'views', true);   
          if(!update_post_meta($post_ID, 'views', ($post_views+1)))   
          {   
            add_post_meta($post_ID, 'views', 1, true);   
          }   
      }   
    }   
}   
add_action('wp_head', 'record_visitors');   
/// 函数名称:post_views   
/// 函数作用:取得文章的阅读次数   
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)   
{   
  global $post;   
  $post_ID = $post->ID;   
  $views = (int)get_post_meta($post_ID, 'views', true);   
  if ($echo) echo $before, number_format($views), $after;   
  else return $views;   
}

//调用二级菜单函数
function wordpresskt_load_self_nav_walker(){
    //加载walker类
    include get_theme_file_path().'/inc/class-my-nav-walker.php';
}
add_action('after_setup_theme','wordpresskt_load_self_nav_walker');


//获取用户登录后返回浏览的网页
function wordpresskt_get_current_url(){
    global $wp,$wp_rewrite;

    //获取重写规则,朴树模式规则为空
    $rewrite = $wp_rewrite->wp_rewrite_rules();

    //非朴素模式下,返回当前网址
    if(!empty($rewrite)){
        return home_url( $wp->request );
    }

    //在朴素模式下,返回当前网址
    return home_url( '?'.$wp->query_string );

}


//引入css和js文件
function wpkt_style(){
    wp_enqueue_style('main-css',get_theme_file_uri().'/assets/css/app.css');//css

    wp_enqueue_script('require',get_theme_file_uri().'/assets/js/require.js',array(),'',true);//js
    wp_enqueue_script('config',get_theme_file_uri().'/assets/js/config.js',array(),'',true);
    wp_enqueue_script('app',get_theme_file_uri().'/assets/js/app.js',array(),'',true);

    //点击评论回复时 网页不刷新 体验超好 其实就是引入一个js文件
    if( is_singular() && comments_open() && get_option('thread_comments') ){
        wp_enqueue_script('comment-reply');
    }
}
add_action('wp_enqueue_scripts','wpkt_style');



//开启主题的一些功能
function wpkt_setup(){
    //开启页面标题功能
    add_theme_support('title-tag');

    //开启特色图像
    add_theme_support('post-thumbnails');

    //注册导航位置
    register_nav_menus(array(
        'nav-1' => '顶部导航',
        'nav-2' => '底部导航',
    ));
}
add_action('after_setup_theme','wpkt_setup');

//开启边栏工具
function wpkt_sidebar(){
    register_sidebar(array(
        'name' => '边栏1',
        'id'   => 'sidebar-1',
        'description' => '第一个边栏',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ));
}
add_action( 'widgets_init', 'wpkt_sidebar' );


?>
原文地址:https://www.cnblogs.com/xm666/p/14103531.html