WordPress主题开发常用标签

博客信息

<?php echo get_bloginfo('name'); ?>
<?php echo get_bloginfo('description'); ?>
<?php echo get_bloginfo('url'); ?>

<?php echo get_template_directory_uri(); ?> // 输出主题文件夹路径

index/archive页面函数

主循环

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

    <div class="post">

        <?php if (is_single()) { ?>
            <h1 class="post-title"><?php the_title(); ?></h1>
        <?php } else { ?>
            <h2 class="post-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <?php } ?>

        <p class="post-date"><?php echo get_the_date('Y年n月j日 l H:i'); ?></p>

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

        <div class="post-tags"><span class="category">分类:<?php the_category(','); ?></span><?php the_tags(); ?>
        </div>

    </div>

<?php endwhile; else: ?>

<h2>404 没有这个页面或没有搜索结果</h2>

<?php endif; ?>

输出分页

<span class="nav-previous"><?php next_posts_link('← 较旧的博文'); ?></span>
<span class="nav-next"><?php previous_posts_link('较新的博文 →'); ?></span>

<?php if ($url1 = get_previous_posts_page_link()): ?>
    <li>
        <a href="<?php echo $url1; ?>" class="previous">
            Newer
        </a>
    </li>
<?php endif; ?>

<?php if ($url2 = get_next_posts_page_link()): ?>
    <li>
    	<a href="<?php echo $url2; ?>" class="next">
            Older
        </a>
    </li>
<?php endif; ?>

文章页面(single.php/page.php)

输出文章上下文章

<?php if (is_single()) {
    $prev_post = get_previous_post();
    $next_post = get_next_post();
    ?>

    <?php if (!empty($prev_post)): ?>
        <p>上一篇博文:<?php previous_post_link('%link', '%title'); ?></p>
    <?php endif; ?>

    <?php if (!empty($next_post)): ?>
        <p>下一篇博文:<?php next_post_link('%link', '%title'); ?></p>
    <?php endif; ?>

<?php } ?>

获取相关文章(相同分类)

<?php
$categories = get_the_category($post->ID);
if ($categories) {
    $category_ids = array();

    foreach ($categories as $individual_category) {
        $category_ids[] = $individual_category->term_id;
    }

    $args = array(
        'category__in' => $category_ids,
        'post__not_in' => array($post->ID),
        'ignore_sticky_posts' => 1,
        'showposts' => 6,
        'orderby' => 'rand',
    );

    $my_query = new wp_query($args);
    if ($my_query->have_posts()) {
        while ($my_query->have_posts()) {
            $my_query->the_post();
            ?>
            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
            <?php
        }

    }
    wp_reset_postdata();
}
?>

最后修改日期

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

编辑文章按钮

<?php edit_post_link('编辑文章') ;?>

自动重命名上传的媒体

function git_upload_filter($file) {
    $time = date("YmdHis");
    $file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
    return $file;
}
add_filter('wp_handle_upload_prefilter', 'git_upload_filter');

wp图片尺寸:

  • Thumbnail size (150px square)
  • Medium size (maximum 300px width and height)
  • Large size (maximum 1024px width and height)
  • Full size (full/original image size you uploaded)