Wordpress:如何将永久链接重定向(或更改)到WP_query循环


Wordpress: How to redirect (or change) permalinks to a WP_query loop?

所以我有一个名为"comics"的页面,它使用一个自定义模板,其中包含我网站中帖子的WP_query循环(每页1个漫画,带有上一个/下一个按钮用于导航)。我还有一个档案页面,列出了"漫画"类别中的所有帖子。

默认情况下,存档页面上的链接链接到特定的帖子本身,但我如何将其更改为链接到WP_query循环中的帖子?

我知道我可以使用301重定向插件,并为每个帖子添加正确的链接,但我这样做是为了一个客户,所以如果我能让她更容易的话会更好。

如果您需要知道,以下是comics.php页面中的WP_query循环:

<?php 
$comics_loop = new WP_Query(
    array(
        'posts_per_page' => 1,
        'paged' => get_query_var('paged'),
        'category_name' => comics
    )
);
if($comics_loop->have_posts()) :
echo '<ul class="comic-slider-container">';
while($comics_loop->have_posts()) : $comics_loop->the_post();
?>
    <li><h1 id="comic-slider-title" class="page-title">Weekly Comics &nbsp;|&nbsp; <span class="title-alt"><?php the_title(); ?></span>&nbsp;&nbsp; <a href="<?php bloginfo('url') ?>/category/comics/" id="archive-button" class="big-button">Archives</a></h1></li>
    <li><?php the_post_thumbnail( 'full' ); ?></li>
    <li><?php the_content(); ?></li>
    <li><p class="byline vcard">
        <?php
            printf(__('Posted <time class="updated" datetime="%1$s" pubdate>%2$s</time>', 'bonestheme'), get_the_time('Y-m-j'), get_the_time(__('F jS, Y', 'bonestheme')) );
            ?>
        </p>
    </li>
<?php
endwhile;
echo "</ul>";
?>

和档案页面的标题链接默认情况下:

<h3 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

为了让你的帖子具有永久链接结构,你应该使用自定义的分类法。

话虽如此,如果你还没有设置一个,这里有一个为漫画设置的代码。将其放入您的函数中。php

add_action( 'init', 'build_taxonomies', 0 );
    function build_taxonomies() {
        register_taxonomy( 'comics', 'post', array( 'hierarchical' => true, 'label' => 'Comics', 'query_var' => true, 'rewrite' => array( 'slug' => 'comics', 'with_front' => false, 'hierarchical' => true ) ) );
    }

然后回显链接的列表

$terms = get_terms('comics');
echo '<ul>';
foreach ($terms as $term) {
    //Always check if it's an error before continuing. get_term_link() can be finicky sometimes
    $term_link = get_term_link( $term, 'comics' );
    if( is_wp_error( $term_link ) )
        continue;
    //We successfully got a link. Print it out.
    echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';
}
echo '</ul>';

根据您的需要进行相应的编辑