wordpress常用函数 主题开发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liu709127859/article/details/81321261

1  wp_head(); wp_footer(); 分别放在 head body结束标签的上一行,获取后台控制台

2 bloginfo(‘param1’)打印 options表中的数据,参数为字段名

  bloginfo('name');  bloginfo('description');  bloginfo('stylesheet_url'); bloginfo('description');

3 get_option('字段名');获取到 options表中的数据 update_option( 'view' , $view + 1 );

                $view = get_option('view');

                update_option( 'view' , $view + 1 );

            <p>访问量:<? echo $view; ?></p>

4  have_posts()判断是否有文章,the_title();文章的标题 the_content(); 文章的内容

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

    the_permalink();文章的连接

            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 '没有日志可以显示';

            }

5 the_category(','); 获取文章分类 the_author(); 获取文章作者  the_time( 'Y-m-d' ); 获取文章创建时间

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

7  语言包翻译相关  

  --() 获取数据 _e()  打印 语言包中的 数据 _e( 'category', '语言包' );

function.php

添加

    add_action('after_setup_theme', 'my_theme_setup');
    function my_theme_setup(){
        load_theme_textdomain('huangcong', get_template_directory() . '/languages');
    }

8 <? posts_nav_link(); ?>  文章默认显示十篇,在设置  阅读中设置。这个是获取底部导航,上一篇,下一篇的方法

9 <? get_sidebar(); ?> index.php中获取侧边栏   

   function.php中

    //注册一个小工具

    register_sidebar(

        array(

            'name' => '侧边栏',

            'before_widget' => '<div class="sbox">',

            'after_widget' => '</div>',

            'before_title' => '<h2>',

            'after_title' => '</h2>'

        )

    );

sidebar.php中 调用方法 获取侧边栏中选择的小工具 <? dynamic_sidebar(); ?>

10  single.php页面 上一篇 下一篇

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

                    <? next_post_link('下一篇:%link'); ?>

11 single.php自定义字段的增删改查

                <!--获取参数-->

                    <?php $download = get_post_meta( $post->ID, 'urlfrom' , true );?>

                <!--添加参数-->

                <?php //add_post_meta($post->ID, '_testdata' , '这是测试数据' ); ?>

                <!--修改数据-->

                <?php //update_post_meta($post->ID, '_testdata' , '这是更新的测试数据' ); ?>

                <!--删除数据-->

                <?php delete_post_meta($post->ID, '_testdata' ); ?>

12  //获取当前文章的分类,并根据当前文章分类参数获取不同的模板文件

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

                var_dump($cat[0]);

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

                //加载模板,如果不存在则调用默认模板

                get_template_part('content/autor','like');

13 the_post(); 加载当前页面数据到缓存中

14 comments.php评论模板

//获取评论模板

comments_template();

<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>

14 search.php

get_search_query(); 获取搜索的关键词

15  <? wp_nav_menu(array("menu"=>'liuyuwen2')) ?>

调用菜单。无参数表示默认。有参数,传递一个数组格式的参数。

16 判断是哪个页面,调用相应的 css文件     

 <?
        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" /><? } ?>

17  面包屑导航

    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>

        <? }

    }

猜你喜欢

转载自blog.csdn.net/liu709127859/article/details/81321261