您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
6-9: 设置class属性值并验证页面详情页结果
发布时间:2023-03-30 18:36:49编辑:雪饮阅读()
拆分出header.php、sidebar.php、footer.php
拆分后single.php如:
<?php get_header();?>
<div class="container-fluid site-content" id="content">
<div class="row">
<div class="col-md-9 pdfix">
<div class="content-area">
<!-- <div class="theiaStickySidebar"> -->
<main class="site-main">
<?php if(have_posts()):?>
<?php while(have_posts()):the_post();?>
<article
class="post-1815 post type-post status-publish format-standard has-post-thumbnail hentry category-keji">
<header class="entry-header">
<h1 class="entry-title"><?php the_title();?></h1>
<div class="entry-meta">
<a class="author-info" href="<?php echo get_author_posts_url(get_the_author_meta('ID'));?>">
<?php echo get_avatar(get_the_author_meta('ID'),24);the_author();?></a>
<span class="dot">•</span>
<span class="fa fa-clock-o"></span><?php the_date('Y-m-d');?> <span class="dot">•</span>
<?php the_category(',');?>
<span class="dot">•</span>
<span class="fa fa-eye"></span><?php echo wordpresskt_get_postviews(get_the_ID());wordpresskt_set_postviews(get_the_ID());?></div><!-- .entry-meta -->
</header><!-- .entry-header -->
<div class="entry-content">
<?php if(has_post_thumbnail()):?>
<p><?php the_post_thumbnail('full');?></p>
<?php endif;?>
<?php the_content();?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php if(has_tag()):?>
<div class="taglist">
<?php the_tags('','');?>
</div>
<?php endif;?>
<div class="postsnav">
<div class="prev"><?php previous_post_link('<span>上一篇:</span> %link');?></div>
<div class="next"><?php next_post_link('<span>上一篇:</span> %link');?></div>
</div>
</footer>
<!-- .entry-footer -->
</article>
<?php endwhile; ?>
<?php else: ?>
<p>对不起,暂时没有任何内容!</p>
<?php endif;?>
<?php if(comments_open() || get_comments_number()):?>
<?php comments_template();?>
<?php endif;?>
</main>
<!-- </div> -->
</div>
</div>
<?php get_sidebar();?>
</div>
</div>
<?php get_footer();?>
获取wordpress提供的body或article(后者用于文章详情页)的class属性值
则header.php如
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>详情模板</title>
<?php
wp_head();
?>
</head>
<body <?php body_class();?>>
<header class="navbar navbar-fixed-top wpkt-header" id="masthead">
<div class="container-fluid">
<div class="row">
<div class="navbar-header">
<button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#primary"
aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="<?php bloginfo('url');?>" class="navbar-brand"><?php bloginfo("name");?></a>
</div>
<nav id="primary" class="collapse navbar-collapse">
<?php wp_nav_menu([
'theme_location' =>'nav-1',
'container'=>'',
'fallback_cb'=>null,
'walker'=>new Wordpresskt_Nav_walker,
'menu_class'=>'nav narbar-nav'
]);?>
<ul class="nav narbar-nav navbar-right">
<li class="login-out">
<a href="<?php echo wp_registration_url();?>">注册</a> | <a
href="<?php echo wp_login_url(wordpresskt_get_current_url());?>">登录</a>
</li>
</ul>
</nav>
</div>
</div>
</header>
single.php如
<?php get_header();?>
<div class="container-fluid site-content" id="content">
<div class="row">
<div class="col-md-9 pdfix">
<div class="content-area">
<!-- <div class="theiaStickySidebar"> -->
<main class="site-main">
<?php if(have_posts()):?>
<?php while(have_posts()):the_post();?>
<article <?php post_class();?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title();?></h1>
<div class="entry-meta">
<a class="author-info" href="<?php echo get_author_posts_url(get_the_author_meta('ID'));?>">
<?php echo get_avatar(get_the_author_meta('ID'),24);the_author();?></a>
<span class="dot">•</span>
<span class="fa fa-clock-o"></span><?php the_date('Y-m-d');?> <span class="dot">•</span>
<?php the_category(',');?>
<span class="dot">•</span>
<span class="fa fa-eye"></span><?php echo wordpresskt_get_postviews(get_the_ID());wordpresskt_set_postviews(get_the_ID());?></div><!-- .entry-meta -->
</header><!-- .entry-header -->
<div class="entry-content">
<?php if(has_post_thumbnail()):?>
<p><?php the_post_thumbnail('full');?></p>
<?php endif;?>
<?php the_content();?>
</div><!-- .entry-content -->
<footer class="entry-footer">
<?php if(has_tag()):?>
<div class="taglist">
<?php the_tags('','');?>
</div>
<?php endif;?>
<div class="postsnav">
<div class="prev"><?php previous_post_link('<span>上一篇:</span> %link');?></div>
<div class="next"><?php next_post_link('<span>上一篇:</span> %link');?></div>
</div>
</footer>
<!-- .entry-footer -->
</article>
<?php endwhile; ?>
<?php else: ?>
<p>对不起,暂时没有任何内容!</p>
<?php endif;?>
<?php if(comments_open() || get_comments_number()):?>
<?php comments_template();?>
<?php endif;?>
</main>
<!-- </div> -->
</div>
</div>
<?php get_sidebar();?>
</div>
</div>
<?php get_footer();?>
处理获取分类的bug
就是说single.php同时也可以做为page.php的作用,服务于wordpress后台里面的页面的详情页。
那么由于页面是没有分类属性的,只有文章是有分类信息的。那么根据优先级附件详情页也有机会出现在single.php中,而附件也是没有分类的。
所以之前获取分类哪里需要排除下页面以及附件详情进入时候的情况(当然如果你有单独的页面和附件的详情页则可以不用处理,这是我个人理解)
那么这里处理页面以及附件详情页兼容则如
<?php if(is_single() && !is_attachment()):?>
<?php the_category(',');?><span class="dot">•</span>
<?php endif;?>
实际上我这里测试了我这里进入页面详情页并没有进入这个文章详情页。
可能是我本地环境和老师的环境有某些细节不同而导致的吧。
关键字词:详情