gpt4 book ai didi

php - WordPress 博客,只对当前页面使用存档?

转载 作者:可可西里 更新时间:2023-10-31 23:47:15 26 4
gpt4 key购买 nike

我正在构建一个 WP 站点,该站点将包含新闻和博客。

它们将在不同的页面中,一个用于新闻,一个用于博客,它们将按类别分开。

例如,我在“新闻”上有这段代码,它停止了获取帖子的循环:

<?php 
$uncat = get_cat_ID('uncategorised');
$uncat2 = get_cat_ID('blog');

$args = array(
'posts_per_page' => 3,
'category__not_in' => array($uncat, $uncat2)

);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a style="color:#333; text-decoration:none;" href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>

</div>
<div class="contentfaq">
<h4><?php the_title(); ?></h3>
<span class="entry-date-blue"><strong><?php echo get_the_date('d/m/y'); ?></strong></span>

<?php $trimexcerpt = get_the_excerpt();

$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… <br/> <a href="">Read More ...</a>' );

echo '<a style="color:#333; text-decoration:none;" href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';

?>
</div>
</div>
</div>
</a>


<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>

这很好用,但我在右侧也有“文件”,它按发布日期过滤。问题是,这将从新闻和博客中获取帖子,这违背了将它们分开的想法。

有没有办法将它们分开,这样如果用户在存档中单击“2015 年 3 月”,它只会从 NEWS 中获取本月的帖子?

这是我当前的 Archive.php 代码

     <?php if (have_posts()) : ?>
<!-- First, the loop checks whether any posts were discovered with the have_posts() function. -->



<!-- If there were any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So, as long as the function have_posts() returns a true value, the while loop will keep looping (repeating). -->
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-12 col-sm-12 col-xs-12">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>

</div>
<h5 class="captext"><?php the_title(); ?></h5>
<span class="entry-date-orange"><?php echo get_the_date(); ?></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>

<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</a>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

最佳答案

试试这个,它会回显列表

    <h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>

<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>

完整示例代码

<?php
/*
Template Name: Archives
*/
get_header(); ?>

<div id="container">
<div id="content" role="main">

<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>

<?php get_search_form(); ?>

<h2>Archives by Month:</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>

<h2>Archives by Subject:</h2>
<ul>
<?php wp_list_categories(); ?>
</ul>

</div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

关于php - WordPress 博客,只对当前页面使用存档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com