不知道是否有人能帮我想出这个问题?
虽然在锁定,我已经把一个简单的网站,组织我的复古游戏广告收集系统,从雅达利2600到N64。我还有几千条广告要添加到网站上(需要时间),但我遇到了一个问题,我不知道如何实现修复。
你可以通过他们的单帖页面按系统浏览广告,但如果一个广告涵盖了多个系统,它就会弄乱上一帖和下一帖的链接,并将你掉进另一个系统。
例如:如果你使用的是下一篇和上一篇的链接,那么你就会掉进另一个系统。如果你使用下一个和上一个帖子的链接去通过 "mega drive genesis "部分,一旦你到了 "Battletoads and Double Dragon",当你按下一个帖子的箭头时,这个时候你会突然通过NES标签的广告,由于事实上,这是第一个与之相关的术语。
请看 。https:/www.retrogameads.comsystemmega-drive 并点击第一个广告,然后继续按下一个箭头,你会明白我的意思。
我想我可以为每个系统多次发布每个广告,但我不喜欢这个想法。
任何人都有任何建议,我如何能找出什么术语的用户浏览,并保持他们在那一个?
请记住,这个网站是一个正在进行中的工作,所以设计只是一些基本的,直到我工作了最好的方式来组织的东西。
让我知道你的想法。
更新。目前获取上一篇下一篇文章的方法...。
<?php
$terms = get_the_terms( $post->ID, 'system' );
$i = 0;
$systems = array();
foreach ( $terms as $term ) {
$systems[$i] = $term->slug;
$i++;
}
$postlist_args = array(
'posts_per_page' => -1,
'post_type' => 'portfolio',
'system' => $systems[0],
'order' => 'ASC',
'orderby' => 'title'
);
$postlist = get_posts( $postlist_args );
$ids = array();
foreach ($postlist as $thepost) {
$ids[] = $thepost->ID;
}
$thisindex = array_search($post->ID, $ids);
$previd = $ids[$thisindex-1];
$nextid = $ids[$thisindex+1];
?>
<div class="prev_next">
<?php
if ( !empty($previd) ) {
echo '<div class="older"><a rel="prev" href="' . get_permalink($previd). '">‹</a></div>';
}
if ( !empty($nextid) ) {
echo '<div class="newer"><a rel="next" href="' . get_permalink($nextid). '">›</a></div>';
}
?>
当你点击一个广告时,你基本上是在访问该广告的单页。此时,WP不知道remember用户只想看到你点击的系统的广告。
你现在是如何呈现上一个next链接的?
一个可能的解决方案是在URL中添加一个参数,然后在PHP代码中考虑到这一点,当渲染上一个下一个链接时。(/portfolio/advert-name/?system=mega-drive
(例如)
编辑:当然,URL也可以做得更漂亮......如果你想做一些额外的工作,你可以注册一个永久地址的形式。/portfolio/mega-drive/advert-name/
比如说。但这需要多做一些工作。
这能行吗?考虑到其他选择,我认为这将是最好的解决方案。
更新一下。 对于实际的实施, get_next_post_where
和 get_previous_post_where
过滤器可能会非常有用。你可以让它把系统参数考虑到上一个-下一个链接中。
另一个选择。 你也可以设置一个PHP会话,并以这种方式记住当前的系统,但是你需要一个PHP会话,这不是一件好事,而且它也会阻止你使用全页面缓存(性能优化)。
然而另一种选择是通过cookie来记住当前系统的客户端。但是这样的话,你又有缓存问题,除非你通过AJAX加载上一个next链接。
看了你的问题更新后,我有以下的说明。
system
这不是一个有效的参数,并且会被忽略。get_posts()
的调用。那么,我们该如何改进呢?
很简单。使用 get_previous_post
和 get_next_post
的功能 $post
. 它接受三个参数。下面是一个例子 get_next_post
(但 get_previous_post
基本相同)。)
get_next_post( bool $in_same_term = false, array|string $excluded_terms = '', string $taxonomy = 'category' )
现在如果你设置 $in_same_term
到 true
那么下一篇文章将是至少共享一个分类法术语(在我们的例子中是系统)的文章。
您还必须设置 $taxonomy
到 system
因为那是自定义分类法。
而另一个参数是 $excluded_terms
. 可惜没有 $included_terms
否则我们可以把我们想要的系统放在那里。但我们可以用另一种方法......。我们可以过滤掉当前的系统(在 system
获取 URL中的参数),并将所有其他系统保留为要排除的系统。
所以我们现在就来构建我们需要的东西。
global $post;
// What system did we come from?
$system = $_GET['system'] ?? null; // Set to null if nothing was specified (uses PHP's null coalescing operator available since PHP 7
// Exclude nothing by default
$excluded_term_ids = [];
// If we came here by clicking on a system, then exclude all other systems so the previous/next links will be of the same system only
if ($system) {
// Retrieve system terms for this post
$terms = get_the_terms( $post, 'system' );
// Filter the list of terms to remove the system we came from
// This way we can create a list of terms to exclude
$excluded_terms = array_filter( $terms, function ( $term ) use ( $system ) {
return $term->slug != $system;
} );
// The get_previous_post and get_next_post functions expect $excluded_terms to be an array of term IDs, so we must map the WP_Term objects into IDs
$excluded_term_ids = array_map( function ( $term ) {
return $term->term_id;
}, $excluded_terms );
}
// Retrieve previous and next post
$previous_post = $post->get_previous_post( true, $excluded_term_ids, 'system' );
$previous_post = $post->get_next_post( true, $excluded_term_ids, 'system' );
// Echo out the page links
// And don't forget to re-add the ?system= parameter to the URL
$url_suffix = $system ? ('?system=' . $system) : '';
if ( $previous_post ) {
echo '<div class="older"><a rel="prev" href="' . get_permalink($previous_post) . $url_suffix . '">‹</a></div>';
}
if ( $next_post ) {
echo '<div class="newer"><a rel="next" href="' . get_permalink($next_post) . $url_suffix . '">›</a></div>';
}
注意:未经测试的代码... 所以可能会有一个小的错误。如果你在这段代码中遇到了问题,请告诉我。
重要的是 在一个系统的页面上,你现在必须在你渲染的URLs上添加'?system=current-system',大概是这样的: echo get_permalink($advert) . '?system=' . $post->post_name
(可能会有所不同,这取决于你的代码在那个屏幕上是怎样的... )