我在WP管理中创建了一个名为Blog的新页面,并为其创建了自定义页面模板。在模板中,我有一个简单的分页自定义查询。
$query = new WP_Query(array( \'post_type\' => \'post\', \'posts_per_page\' => 1,
\'paged\' => $paged ));
看起来一切正常,除了我手动键入比url的“/页/”部分后可用的页码更大的页码。页面不会抛出404错误,但会在没有自定义循环的情况下将页面呈现为正常。这是正常行为吗?如果是这样,我应该如何抛出404模板页面?
此外,我还有一个子问题:
是否可以创建自定义主题页面,而无需在wordpress管理中创建页面?在大多数情况下,我甚至不使用页面的内容,将其留空。然后,如果有人将删除该页面,或更改页面slug,则网站的一部分可能会被破坏。
是否有更简单、更方便管理员的解决方案?
附:这是一个全页的博客。php源代码:
<div id="primary" class="content-area">
<main id="main" class="site-main">
<h1><?=get_theme_mod(\'crochelou-blog-main-headline\') ?><span class="sub-headline"><?=get_theme_mod(\'crochelou-blog-sub-headline\') ?></span></h1>
<section id="page-container">
<nav id="blog-nav-wrapper"><?php
wp_nav_menu( array(
\'menu_id\' => \'category-menu\',
\'menu\' => \'category-menu\'
) );
?></nav>
<?php
$query = new WP_Query(array( \'post_type\' => \'post\', \'posts_per_page\' => 1, \'paged\' => $paged )); ?>
<section id="main-blog-loop"><?php
if($paged > 1){ ?>
<a href="<?= get_pagenum_link($paged - 1) ?>" class="pagination-arrow arrow left arrow-left prev"></a>
<?php }
?><div class="wrapper articles">
<?php
while($query->have_posts()): $query->the_post(); ?>
<article class="crochelou-card blog">
<div class="blog image" style="background-image: url(\'<?=esc_url(the_post_thumbnail_url(\'medium\')) ?>\')"></div>
<div class="categories"><?php
$categories = get_the_category();
foreach($categories as $category){
$name = $category->name;
$link = get_category_link($category->term_id); ?>
<a href="<?= $link ?>" class="catgory"><?= $name ?></a>
<?php
}
?></div>
<span class="date"><?= get_the_date(\'d\') . \'\\\\\' . get_the_date(\'m\') . \'\\\\\' . get_the_date(\'Y\') ?></span>
<h4 class="headline blog"><a href="<?= get_the_permalink() ?>"><?= get_the_title() ?></a></h4>
<div class="perex excerpt"><?php the_excerpt(); ?></div>
<a class="link read-more" href="<?= get_the_permalink() ?>" >číst..</a>
</article>
<?php endwhile;
?>
</div><?php
if($paged < $query->max_num_pages){ ?>
<a href="<?= get_pagenum_link($paged + 1) ?>" class="pagination-arrow arrow right arrow-right right"></a>
<?php }
?>
</section>
<?php if($paged < $query->max_num_pages){ ?>
<div id="blog-pagination">
<nav class="pagination">
<ul>
<?php
for($i=1; $i <= $query->max_num_pages; $i++){
if($i === $paged){ ?>
<span class="current"><?= $i ?><?php if($i < $query->max_num_pages){ echo \',\'; } ?></span>
<?php }else{ ?>
<a href="<?= get_pagenum_link($i) ?>"><?= $i ?><?php if($i < $query->max_num_pages){ echo \',\'; } ?></a>
<?php }
}
?>
</ul>
</nav>
</div>
<?php }
?>
<!-- THIS WILL JUST RENDER \'ABOUT ME\' TEST FROM ADMINISTRATION -->
<div class="separator"></div>
<?php $page = get_page_by_path( \'o-mne\' ); ?>
<article id="about">
<h3 class="title"><?= $page->post_title ?></h3>
<?php
$content = $page->post_content;
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
?>
<div id="about-content"><?= $content ?></div>
</article>
</section>
</main><!-- #main -->
</div><!-- #primary -->