Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wordpress方法 #10

Open
deepthan opened this issue Jan 13, 2018 · 0 comments
Open

wordpress方法 #10

deepthan opened this issue Jan 13, 2018 · 0 comments

Comments

@deepthan
Copy link
Owner

1. 调用博客名称和地址

 <a href="<?php bloginfo('url'); ?>">  
    <?php bloginfo('name'); ?>
 </a>  

2. 调用博客描述

<h4>
    <?php bloginfo('description');?>
</h4>

3. 调用文章


  <div class="post" id="post<?php the_ID(); ?>">

    <?php if(have_posts()): ?> 
        <?php while(have_posts()) : the_post(); ?>

           <h2>                                      // ' title= '  鼠标移上去显示的内容
                <a href="<?php the_permalink(); ?>"  title="<?php the_title(); ?>" >      -> 标题
                   <?php the_title(); ?>
                </a>
           </h2>

           <p>
                <?php the_content(); ?>                   -> 内容

                <div class="postmetadata">                -> 发表时间等
                    <?php the_category() ?>
                    <?php the_author(); ?>
                    <?php the_date(); ?>
                </div>
           </p>

        <?php endwhile; ?>

        <?php else: ?>
        <div>
            <p><?php  _e('Not Found'); ?></p>
        </div>
    <?php endif; ?>
  </div>

  • if(have_posts()): 检查是否有日志,那么当有日志的时候,执行下面 the_post()这个函数
  • while(have_posts()): 当博客有日志的时候,执行下面 the_post()这个函数
  • the_post(): 调用具体的日志来显示
  • endwhile: 关闭while()
  • endif: 关闭if
  • the_permalink(): 调用每篇日志地址的PHP函数
  • the_title(): 调用日志标题的函数
  • the_content(): 调用日志内容的函数
  • the_category(): 日志类别
  • the_author(): 日志作者
  • the_date(): 日志发布时间
  • the_ID(): 给每篇日志都添加一个id,给它进行样式添加。
    &&&& 主循环: the loop &&&&

4. 获得自定义文章

首先摸一下你的自定义的类型,显示这样的连接

http://localhost/wp-admin/term.php?taxonomy=category&tag_ID=10&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

下面的参数即使对应的

&post_type = post&  ,  taxonomy = category&, &tag_ID=10&
<?php
$args = array(
    'post_type' => 'post', //自定义文章类型名称,上面连接可以获得。
    'showposts' => 3 , //输出的文章数量,这个可以是缺省值,不用设置
    'tax_query' => array(
        array(
            'taxonomy' => '1',//自定义分类的名称
            'terms' => 10 // 上面显示的id
            ),
        )
    );
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post();?>
    <h3><?php the_title(); ?></h3>
    <p><?php the_content(); ?></p>
    <?php endwhile; wp_reset_query(); //重置query查询
} ?>

5.获得文章类型名称,和上面无关。

<?php 
    $post_type =get_post_type(36);
    //假设id为36的文章类型为附件类型attachment
    echo $post_type;
    //打印出attachment
?>
        

6.如何引入文件

  • 1: wordpress函数
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php get_footer();?>
  • 2: php函数
<?php include (TEMPLATEPATH . ‘/header.php’); ?>

7. 输出日期

 <?php the_time('Y年n月j日') ?>

8.点击某个分类名称直接进入分类内容

  • 通过分类别名获取Wordpress分类链接:
 <?php
  $cat=get_category_by_slug('wordpress');
  $cat_links=get_category_link($cat->term_id);
?>
<a href="<?php echo $cat_links; ?>" title="<?php echo $cat->name; ?>">更多</a>
 
  • 通过分类ID获取Wordpress分类链接
<?php
    $cat=get_term_by('id', 12, 'category');
    $cat_links=get_category_link($cat->term_id);
?>
<a href="<?php echo $cat_links; ?>" title="<?php echo $cat->name; ?>">更多</a>

9. 文章自定义栏目调用 :

$post : wordpress内置全局变量。
$post->ID : 获得文章的id
get_post_meta ( $post->ID, '键名', boolean ) : 文章id, 键名, 布尔值: true是输出字符串, false 输出数组。
add_post_meta( $post->ID, '键名', ' 键值' ) : 添加自定义字段
add_post_meta( $post->ID, ‘_键名', ‘键值’) : 添加私有的字段,在栏目编辑里面看不到
调用模板文件:

$name = " deepthan" ;
get_template_part(' content ', $name ) 
加载 content-$name.php文件(即content-deepthan.php文件),若不存在则调用 content.php文件。 

根据分类名称获得这个分类的链接

 <php?  $report=get_category_id('新闻'); ?>  
这里的get_categoty_id()是自己封装的函数,且固定链接为朴素模式。
<a href="<?php bloginfo('url');?>/?cat=<?php  echo $report;?>"  title="<?php echo $report->name; ?>">热点技术</a>

10. 请求数据

用php请求接口并获得返回的数据?

  1. $_post [ 'name' ] 获取POST传送过来的数据。 这个只能接收 Content-Type:application/x-www-form-urlencoded提交的数据。
  2. file_get_contents( url);请求 url地址并获得返回的数据。
  3. curl的拓展库: 可以获取其他站点的内容。跨站点传递数据


$phoneNumber = "13666666666";
$message = "this is a message";
$curlPost = 'phoneNuber='.urlencode($phoneNumber).'&MESSAGE'.urlencode($message).'&SUBMIT=send';
$ch = curl_init();
curl_init()函数的作用初始化一个curl会话,curl_init()函数唯一的一个参数是可选的,表示一个url地址。
curl_setopt($ch,CURLOPT_URL, ' http:www.baidu.com');
curl_setopt($ch,CURLOPT_HEADER ,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
culr_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $curlPost);
curl_setopt()为一个curl设置会话参数。
$data = curl_exec($ch);、
curl_exec()函数的作用是执行一个curl会话,唯一的参数是curl_init()函数返回的句柄。
curl_close($ch);
curl_close()作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。
curl_setopt_array()以数组的形式为一个curl设置会话参数。

 ajax --> 请求本地php文件(http:localhost/wp-admin/....客户端) -> php文件用 $ch传递参数请求地址 (http:localhost/服务器接口地址) --> 返回信息传递给浏览器。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant