【问题标题】:Wordpress: How to redirect (or change) permalinks to a WP_query loop?Wordpress:如何将永久链接重定向(或更改)到 WP_query 循环?
【发布时间】:2013-08-04 16:33:06
【问题描述】:

所以我有一个名为“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>

【问题讨论】:

  • 改成链接到哪个帖子?
  • @标记任何帖子。例如我希望存档中的帖子 #2 链接到包含 wp_query 循环的页面中的帖子 #2,而不是实际帖子本身。
  • wp_query 页面中的 Post #2 是什么?它的另一页?一节?你有更具体的信息吗?
  • @Mark 是的,这是另一页。我复制了 page.php 文件以创建另一个名为 Comics.php 的页面模板。在 Comics.php 中,我添加了 wp_query 循环。现在帖子#2 是localhost:8888/wordpress/comics/page/2,这是正确的。但从档案中,帖子#2 链接到localhost:8888/wordpress/post-2。是否可以按照我想要的方式进行?
  • 啊,好吧 - 'comics' 是自定义分类法吗?

标签: php wordpress redirect permalinks


【解决方案1】:

为了使您的帖子具有该固定链接结构 - 您应该使用 custom taxonomy

话虽如此 - 如果您还没有设置一个,这里有一个代码来设置一个漫画。把它放到你的functions.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>';

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

【讨论】:

  • 好吧,我尝试了你的建议..然后去阅读分类法等,并意识到这不是我所需要的......或者我认为。决定放弃这种方法并以另一种方式进行(仍然缺少一些功能),因为我的时间不多了。还是谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-05-04
  • 2011-06-25
  • 2013-08-14
  • 2016-02-11
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多