分类: 建站知识

  • WordPress the_category与single_cat_title

    在wordpress网站主题开发用常会用到调用分类目录的名称,the_category与single_cat_title都可以调用出分类目录的名称。

    <?php single_cat_title(); ?>
    <?php the_category(); ?>
    

    但是,不少人搞不清楚二者有什么区别,其实很简单。

    如果用single_cat_title调用分类名称,在分类里没文章时可以显示分类名称。用the_category分类名称,在分别里没文章时不会显示分类名称。

    就这么点区别,其它的没什么区别。

  • WordPress调用当前文章作者头像

    制作wordpress博客主题时经常会到用,需要调用wordpress当前文章作者头像的时候,用下面的这段代码即可。

    <?php if (have_posts()) : the_post(); update_post_caches($posts); ?>
    //wodepress.com
    <?php echo get_avatar( get_the_author_email(), '80' );?>//80代表头像的大小
    <?php endif; ?>
  • wordpress获取父页面的2种方法

    wordpress模板制作的过程中有时会用到获取父页面的ID,下面是wordpress获取父页面的2种方法。

    方法一

    global $post;
    $id = $post -> ID;
    $parent = get_post_ancestors($post -> ID);
    print_r($parent);

    方法二

    global $post;
    $parent_id = $post -> post_parent;
    echo $parent_id;
  • wordpress调用当前页面ID

    在制作wordpress模板时常会用到的,wordpress调用当前页面ID代码。

    <?php global $post;
    $id = $post -> ID;
    echo $id; ?>

    在需要的位置上这段代码,即可显示出该页面的ID。

  • wordpress调用全站随机文章

    wordpress调用全站随机文章代码

    <?php
    
    global $post;
    
    $postid = $post->ID;
    
    $args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array($post->ID), ‘showposts’ => 10); // 显示篇数
    
    $query_posts = new WP_Query();
    
    $query_posts->query($args);
    
    ?>
    
    <?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>
    
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
  • wordpress调用同分类随机文章

    wordpress调用同分类随机文章代码

    <?php
    
    $cat = get_the_category();
    
    foreach($cat as $key=>$category){
    
    $catid = $category->term_id;}
    
    $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid ); // 显示篇数
    
    $query_posts = new WP_Query();
    
    $query_posts->query($args);
    
    while ($query_posts->have_posts()) : $query_posts->the_post();?>
    
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile;?>
    
    <?php wp_reset_query(); ?>
  • wordpress调用全站热门文章

    wordpress调用全站热门文章代码

    <?php
    
    $post_num = 10; // 显示篇数
    
    $args = array(
    
    ‘post_password’ => ”,
    
    ‘post_status’ => ‘publish’, // 只选公开的文章.
    
    ‘post__not_in’ => array($post->ID),//排除当前文章
    
    ‘caller_get_posts’ => 1, // 排除置顶文章.
    
    ‘orderby’ => ‘comment_count’, // 依评论数排序.
    
    ‘posts_per_page’ => $post_num
    
    );
    
    $query_posts = new WP_Query();
    
    $query_posts->query($args);
    
    while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
    
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php } wp_reset_query();?>
  • WordPress调用指定分类文章

    wordpress按分类ID调用指定分类的文章代码

    <?php
        $args=array(
            'cat' => 1,   // 分类ID
            'posts_per_page' => 10, // 显示篇数
    
        );
        query_posts($args);
    
        if(have_posts()) : while (have_posts()) : the_post();
    ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </li>
    <?php  endwhile; endif; wp_reset_query(); ?>
  • wordpress调用最新文章

    wordpress调用最新文章的代码

    <?php query_posts('showposts=6&cat=-1'); ?>  // 显示篇数和排除分类
    <ul>  
    <?php while (have_posts()) : the_post(); ?>  
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
    <?php endwhile;?>  
    </ul>  
  • wordpress怎么调用页面别名

    在制作wordpress主题时常常会用到调用页面的别名,下面这段代码可以完美解决,wordpress页面别名调用的问题。

    <?php if( is_page() ){$post_data = get_post($post->ID, ARRAY_A); echo $post_data['post_name'];} ?>

    把上面这么代码放在page.php页面中需要显示别名的位置即可。