wordpress主题 建站

82 阅读3分钟

常用函数烂笔头

<a href="<?php echo wp_login_url($_SERVER['REQUEST_URI']);?>" 如果未登录,弹出登录页面后跳转回原页面

wp_trim_words(get_the_title($qy[$i]->ID),9) 截取字符串

get_template_part(); 获取自定义模板

<? echo get_bloginfo('charset'); ?> //编码方式

$from_name = isset($_POST['tougao_from_name']) ? trim(htmlspecialchars($_POST['tougao_from_name'], ENT_QUOTES)) : ''; wordpress接收参数

sprintf() 字符串替换函数

<?php
$number = 2;
$str = "Shanghai";
$txt = sprintf("There are %u million cars in %s.",$number,$str);
echo $txt;
?>
<?php
$number = 2;
$str = "Shanghai";
$txt = sprintf("There are %u million cars in %s.",$number,$str);
echo $txt;
?>

<? bloginfo('description'); ?>  获取副标题

<? bloginfo('stylesheet_url'); ?> 样式表 style.css获取

<?php echo get_option('home'); ?>  获取站点首页

<?php bloginfo('name'); ?> 获取站点名称

$view = get_option('view'); 获取自定义参数

wp_is_mobile()  判断是否是手机端

<? wp_nav_menu() ?> 获取菜单

        <? wp_register(); ?>

        <? wp_loginout(); ?>

   <? get_links(); ?> 友情链接

<? wp_list_pages(); ?> 页面列表

<? wp_list_cats(); ?> 分类

update_option( 'view' , $view + 1 ); 更新自定义参数

                //$zan = get_post_meta( $post->ID, '_zan', true );
update_post_meta( $post->ID, '_zan', $zan + 1 );
//delete_post_meta( $post->ID , 'download' );

                <?
$downloads = get_post_meta( $post->ID, 'download' , false );
if( !$downloads )
{
add_post_meta( $post->ID, 'download', 'hcsem.com/000.rar' );
}
?>

<? the_category(','); 获取分类

<? the_author(); ?>

<? the_time( 'Y-m-d' ); ?>

<? edit_post_link( __( 'Edit','huangcong' ), ' <span>|</span> ', '' ); ?> 编辑文章页面

<? single_cat_title(); ?> 获取当前文章分类

-----------------------------------------------------------------------------------------

//根据分类名称调用分类模板,

$cat=get_the_category($post->ID);

$cat_name=$cat[0]->slug;

-----------------------------------------------------------------------------------------

获取页面别名

if( is_page() ) {

    $content = $content . get_option('display_copyright_text');

     $post_data = get_post($post->ID, ARRAY_A); echo $slug = $post_data['post_name'];

     }

-----------------------------------------------------------------------------------------

根据自定义字段 大小排序

  1. $args=array(
  2. 'meta_key' => 'views',
  3. 'orderby' => 'meta_value_num',
  4. 'posts_per_page'=>10,
  5. 'order' => 'DESC'
  6. );
  7. query_posts($args); while (have_posts()) : the_post();
  8. //输出代码段,如带上HTML代码
  9. endwhile;wp_reset_query();

-----------------------------------------------------------------------------------------

根据自定义字段 大于0 少于 99 进行搜索

$args = ``array``(

    ``'post_type'         => ``'post'``,

    ``'post_status'       => ``'publish'``,

    ``'posts_per_page'    => -1,

    'meta_query' => ``array``(

```array``(`

```'key'       => 'price',`

```'value'     => array($min_price , $max_price),`

```'compare'   => 'BETWEEN',`

```'type'      => ``'NUMERIC'`

```)`

```)`

);

 

// The Result

$naruco = ``new WP_Query( ``$args );

 

var_dump(``$naruco``);

----------------------------------------------------

面包屑导航

function中定义

    function wz()
{
if( !is_home() ){ ?>
<div class="wz">
<a href="<? bloginfo('url'); ?>">首页</a> > 
<?
if( is_category() ) { single_cat_title(); }
elseif ( is_search() ) {  echo $s; }
elseif ( is_single() ) { 
$cat = get_the_category();
$cat = $cat[0];
echo '<a href="' . get_category_link( $cat ) . '">' . $cat->name . '</a> > 文章内容';
}
elseif ( is_page() ) { the_title(); }
elseif ( is_404() ) { echo '404 错误页面'; }
?>
</div>
<? }
}

 

调用

<div class="c">

    <? wz(); ?>

</div>

-----------------------------------------------------------------------------------------

固定链接 与伪静态  页面传参,添加规则

function.php中

    add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );

    add_filter( 'query_vars','my_insert_query_vars' );

    add_action( 'wp_loaded','my_flush_rules' );

 

    // 如果伪静态规则里面没有我们的规则,则进行重置

    function my_flush_rules(){

        $rules = get_option( 'rewrite_rules' );

        

        if ( ! isset( $rules['url-(.*).html$'] ) ) {

            global $wp_rewrite;

            $wp_rewrite->flush_rules();

        }

    }

 

    //添加一个新的伪静态规则

    function my_insert_rewrite_rules( $rules )

    {

        $newrules = array();

        $newrules['url-(.*).html$'] = 'index.php?pagename=url&v=$matches[1]';

        return $newrules + $rules;

    }

    

    //添加一个变量名称

    function my_insert_query_vars( $vars )

    {

        array_push( $vars, 'v' );

        return $vars;

    }

-----------------------------------------------------------------------------------------

根据页面 定义title     调用相应css

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=<? echo get_bloginfo('charset'); ?>" />

    

    <?

        if( is_home() ){ $title = get_bloginfo('name'); }

        else{ $title = wp_title( '', false ) . "_黄聪的博客"; }

        

        if( $paged > 0 ){ $title .= "-第" . $paged . "页"; }

    ?>

    <title><? echo $title; ?></title>

    

    <? if( is_home() ){ ?>

    <meta name="description" content="<? bloginfo('description'); ?>" />

    <? } ?>

    <link rel="stylesheet" href="<? bloginfo('stylesheet_url'); ?>" type="text/css" />

    <script type="text/javascript" src="<? bloginfo('template_directory');?>/js/jquery-1.8.3.min.js"></script>

    

    <? if( is_category() ){ ?><link rel="stylesheet" href="<? bloginfo('template_directory');?>/style-cat.css" type="text/css" /><? } ?>

    <? if( is_search() ){ ?><link rel="stylesheet" href="<? bloginfo('template_directory');?>/style-search.css" type="text/css" /><? } ?>

    <? if( is_404() ){ ?><link rel="stylesheet" href="<? bloginfo('template_directory');?>/style-404.css" type="text/css" /><? } ?>

    <? if( is_single() ){ ?><link rel="stylesheet" href="<? bloginfo('template_directory');?>/style-single.css" type="text/css" /><? } ?>

    <? if( is_page() ){ ?><link rel="stylesheet" href="<? bloginfo('template_directory');?>/style-page.css" type="text/css" /><? } ?>

    

    <? wp_head(); ?>

</head>

-----------------------------------------------------------------------------------------

第几页 第几页

        <div class="posts_nav_link">

            <? posts_nav_link(); ?>

        </div>

文章页 上一篇 下一篇

<? previous_post_link('上一篇:%link'); ?><br />
<? next_post_link('下一篇:%link'); ?>

-----------------------------------------------------------------------------------------

激活小工具

激活小工具需要在functions.php中注册至少一个侧边栏

register_sidebar( array(
'name' => __( '默认侧边栏', 'Bing' ),
'id' => 'widget_default',
'description' => __( '侧边栏的描述', 'Bing' ),
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
) );

前台调用

<?php dynamic_sidebar( 'widget_default' ); ?>

使用the_widget()函数直接调用小工具

<?php the_widget($widget, $instance, $args); ?>

参数$widget,小工具类名

  • WP_Widget_Archives – 存档小工具
  • WP_Widget_Calendar – 日历小工具
  • WP_Widget_Categories – 分类小工具
  • WP_Widget_Links – 链接小工具
  • WP_Widget_Meta – Meta小工具
  • WP_Widget_Pages – 页面小工具
  • WP_Widget_Recent_Comments – 最近评论小工具
  • WP_Widget_Recent_Posts – 最新文章小工具
  • WP_Widget_RSS – RSS小工具
  • WP_Widget_Search – 搜索小工具
  • WP_Widget_Tag_Cloud – 标签云小工具
  • WP_Widget_Text – 文本小工具
  • WP_Nav_Menu_Widget – 菜单小工具

参数$instance

表示每个widget的设置,例如Archives是用dropdown菜单显示还是列表显示

参数$args

widget的sidebar参数,包括before_widget、after_widget、before_title和after_title

调用文章归档小工具举例

<?php the_widget('WP_Widget_Recent_Posts'); ?>

-----------------------------------------------------------------------------------------

get_options();方法,获取文章方式

    <div class="sbox">

        <h2>随机文章</h2>

        <?

            $args = array(

                'numberposts' => 3,

                'category' => '1',

                'orderby' => 'rand'

            );

            $rand_posts = get_posts( $args );

        ?>

        <ul>

        <? foreach ( $rand_posts as $post ) {

         setup_postdata( $post ); ?>

         <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

        <? } wp_reset_postdata(); ?>

        </ul>

    </div>

-----------------------------------------------------------------------------------------

获取文章

        <?

            if( have_posts() ){

                while( have_posts() ){

                    //获取下一篇文章的信息,并且将信息存入全局变量 $post 中

                    the_post();

                    ?>

                    <div class="post-item">

                        <div class="post-title"><h2><a href="<? the_permalink(); ?>"><? the_title(); ?></a><h2></div>

                        <div class="post-content"><? the_content(); ?></div>

                    </div>

                    <?

                }

            }else{

                echo '没有日志可以显示';

            }

        ?>

 

 

1 先找到静态页面 包括所有的css js html 。还有一个主题的模板 这里用的是 _s-master

2 主题_s-master 上传到theme文件夹中。并在 _s-master 中建立文件夹 存放 所有静态页面

3 主题 function.php 文件中包含自定义的 my_function.php文件,添加自己的方法

4  my_function.php中引入所有的主题的 css js

//将 css js引入到页面

 function add_theme_scripts() {

wp_enqueue_style( 'slick', get_template_directory_uri() . '/my_include/css/slick.css', array(), '1.1', 'all');
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/my_include/css/bootstrap.css', array(), '1.1', 'all');
wp_enqueue_style( 'jquery_mmenu_all', get_template_directory_uri() . '/my_include/css/jquery.mmenu.all.css', array(), '1.1', 'all');
wp_enqueue_style( 'style', get_template_directory_uri() . '/my_include/css/style.css', array(), '1.1', 'all');
wp_enqueue_style( 'font_size', get_template_directory_uri() . '/my_include/font-awesome-4.5.0/css/font-awesome.min.css', array(), '1.1', 'all');
wp_enqueue_style( 'animate', get_template_directory_uri() . '/my_include/css/animate.css', array(), '1.1', 'all');
wp_enqueue_style( 'error404', get_template_directory_uri() . '/my_include/css/error404.css', array(), '1.1', 'all');
wp_enqueue_style( 'photo', get_template_directory_uri() . '/my_include/css/photo.css', array(), '1.1', 'all');
wp_enqueue_style( 'slider', get_template_directory_uri() . '/my_include/css/widget.css', array(), '1.1', 'all');

    wp_enqueue_script( 'jquery', get_template_directory_uri() . '/my_include/js/jquery.min.js', array ( 'jquery' ), 1.1, true);
wp_enqueue_script( 'jquery_mmenu_all', get_template_directory_uri() . '/my_include/js/jquery.mmenu.all.min.js', array ( 'jquery' ), 1.1, true);
wp_enqueue_script( 'slick', get_template_directory_uri() . '/my_include/js/slick.min.js', array ( 'jquery' ), 1.1, true);
wp_enqueue_script( 'wow', get_template_directory_uri() . '/my_include/js/wow.js', array ( 'jquery' ), 1.1, true);    
wp_enqueue_script( 'jquery_placeholder', get_template_directory_uri() . '/my_include/js/jquery.placeholder.min.js', array ( 'jquery' ), 1.1, true);
wp_enqueue_script( 'public', get_template_directory_uri() . '/my_include/js/public.js', array ( 'jquery' ), 1.1, true);    

// wp_enqueue_script( 'script', get_template_directory_uri() . '/my_include/js/jquery.colorbox-min.js', array ( 'jquery' ), 1.1, true);
// wp_enqueue_script( 'script', get_template_directory_uri() . '/my_include/js/jquery.mousewheel.min.js', array ( 'jquery' ), 1.1, true);
// wp_enqueue_script( 'script', get_template_directory_uri() . '/my_include/js/lg-fullscreen.js', array ( 'jquery' ), 1.1, true);
// wp_enqueue_script( 'script', get_template_directory_uri() . '/my_include/js/lg-thumbnail.js', array ( 'jquery' ), 1.1, true);
// wp_enqueue_script( 'script', get_template_directory_uri() . '/my_include/js/lightgallery-all.min.js', array ( 'jquery' ), 1.1, true);
// wp_enqueue_script( 'script', get_template_directory_uri() . '/my_include/js/masonry.pkgd.js', array ( 'jquery' ), 1.1, true);
// wp_enqueue_script( 'script', get_template_directory_uri() . '/my_include/js/plug.js', array ( 'jquery' ), 1.1, true);

      if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );

5 建立 头部 尾部  分类模板 首页模板 用 get_template_part( 'template-parts/my', 'home_news' ); 进行分部分引入

6 定义每一个小的部分的具体实现

这里一般都是调用 当前分类下的文章 的标题 缩略图 链接啥的。显示出一个分类下的文章列表

具体的一些知识点总结如下:

①获取分类下文章列表

                            $posts=get_posts("category=6&numberposts=3");  //根据分类获取文章
if($posts){
foreach ($posts as $key => $post) {
# code...
setup_postdata( $post ); //装载到post
echo '            <li class="clearfix">
<div class="news_left clearfix">
<div class="pic">
<a href="'.get_the_permalink().'">'.get_the_post_thumbnail().'</a>  //获取 链接   获取特色图片
<i></i>
</div>
<div class="txt">
<a href="'.get_the_permalink().'">'.get_the_title().'</a> //获取链接 获取标题
<p>
'.get_the_excerpt().'   //获取摘要
</p>
</div>
</div>
<div class="news_right">
<span>'.get_post_time("m-d").'</span> //获取时间月日
<time>'.get_post_time("Y").'</time> //获取年
</div>
</li>';
}
}

②                         <a href="'.get_the_permalink().'">'.get_the_post_thumbnail().'</a> //获取上一篇 链接
<a href="'.get_the_permalink().'">'.get_the_title().'</a> //获取下一篇链接

 <?php previous_post_link($format='%link', $link='上一篇:%title', $in_same_cat = true, $excluded_categories = '') ;  ?>
<?php next_post_link($format='%link', $link='下一篇:%title', $in_same_cat = true, $excluded_categories = ''); ?>

                   <?php echo get_template_directory_uri();?> //获取当前主题目录

 

        <?php 
the_post(); //获取当前文章
?>
<span class="show_t"><?php the_title();?></span>
<span class="fa_time">来源:<?php the_author();?>   &nbsp;&nbsp;    发布时间:<?php the_time('Y-m-d H:i:s'); ?></span>
<?php the_content();?> //获取文章内容

③ 获取自定义的参数

                $posts=get_posts("category=1&numberposts=1");
if($posts){
foreach ($posts as $key => $post) {
# code...
setup_postdata( $post );
echo '            <p class="wow bounceIn">'.get_the_content().'</p>
<ul class="jing_zi_ul clearfix">
<li class="wow bounceIn">'.get_post_meta(get_the_ID(),'pic1',true).'<i></i></li> //获取在写文章时候定义的参数
<li class="wow bounceIn">'.get_post_meta(get_the_ID(),'pic2',true).'<i></i></li>
<li class="wow bounceIn">'.get_post_meta(get_the_ID(),'pic3',true).'><i></i></li>
</ul>';
}
}
?>

<?php get_search_form(); ?>  获取搜索表单

7 不同文章调用不同的页面模板  single.php

//不同分类文章调用不同的模板文件
//$cat=get_the_category($post->ID);//获取当前文章的分类信息 方式一
$cat=get_the_category(get_the_ID());//获取当前文章的分类信息
$name=$cat[0]->slug;
$slug_name='single_'.$name;
get_template_part( 'template-parts/my',$slug_name);
get_footer();

不同文章调用不同模板 single.php

            <?
the_post();
$cat = get_the_category( get_the_ID() );
$name = $cat[0]->slug;
//加载 content-wpcj.php 模版文件,如果文件不存在,则调用content.php
get_template_part( 'content', $name );
?>

评论模板的调用

<div id="comment-box">
<h3>评论</h3>
<ol class="commentlist">
<? if ( !comments_open() ) { ?>
<li class="tip-box"><p>评论功能已经关闭!</p></li>
<? } else if ( post_password_required() ) { ?>
<li class="tip-box"><p>请输入密码再查看评论内容!</p></li>
<? } else if ( !have_comments() ) { ?>
<li class="tip-box"><p><a href="#respond">还没有任何评论,你来说两句吧</a></p></li>
<? } else { wp_list_comments(); } ?>
</ol>
<div class="clr"></div>
<div class="clr"></div>
<? if ( get_option('comment_registration') && !is_user_logged_in() ) { ?>
<p>你必须 <a href="<? echo wp_login_url( get_permalink() ); ?>">登录</a> 才能发表评论.</p>
<? } else if( comments_open() ){ comment_form(); } ?>
</div>

搜索页面模板调用

<div class="c">
<div class="cat-title">搜索词:<? echo get_search_query(); ?></div>
</div>
<div class="c">
<div id="left-box">
<div id="home-loop">
<?
if( have_posts() ){
while( have_posts() ){

//获取下一篇文章的信息,并且将信息存入全局变量 $post 中
the_post();
?>
<div class="post-item">
<div class="post-title"><h2><a href="<? the_permalink(); ?>"><? the_title(); ?></a><h2></div>
<div class="post-content"><? the_content(); ?></div>
<div class="post-meta">
<? _e( 'category', 'huangcong' ); ?>:<? the_category(','); ?><span>|</span>
<? _e( 'author', 'huangcong' ); ?>:<? the_author(); ?><span>|</span>
<? echo _( 'time', 'huangcong' ); ?>:<? the_time( 'Y-m-d' ); ?>
<? edit_post_link( _
( 'Edit','huangcong' ), ' <span>|</span> ', '' ); ?>
</div>
</div>
<?
}
}else{
echo '没有日志可以显示';
}
?>
</div>
<div class="posts_nav_link">
<? posts_nav_link(); ?>
</div>
</div>

404模板调用

<div class="c">
<div id="page-box">
<div class="post-item _404">
<div class="post-content">您当前访问的页面不存在!<font id="jump">3</font> 秒后返回首页,或<a href="<?php bloginfo('url'); ?>">直接返回</a></div>
</div>
<script>
function countDown(secs){
$("#jump").html(secs);
if(--secs>0){ setTimeout( "countDown(" + secs + ")",1000 ); }
else{window.location.href="<?php bloginfo('url'); ?>"; }
}
countDown(3);
</script>
</div>
</div>