我有一个函数,该函数基于URL参数和特定值生成post多页:
?content=multipage
问题是,当用户单击下一页时,URL参数被删除,帖子重置为onepage格式。
如果我手动将URL参数添加到下一页,那么帖子将是多页的
根据我的研究,解决问题的最佳方法是在以下情况下设置cookie:
?content=multipage
这样,这个URL参数就可以在页面之间传递,并且在地址栏中不可见。
我尝试了多种cookie设置方法,发现secookie ()
显然应该起作用的函数。
以下是我目前的工作:
function onepage() {
//**Removes shortcodes**//
function remove_shotcode($content) {
return str_replace(\'[/shortcode1]\', \'\', $content);
return str_replace(\'[shortcode2]\', \'\', $content);
}
add_filter( \'the_content\', \'remove_shotcode\');
/*
*Removes <!--nextpage-->
*@see http://wordpress.stackexchange.com/a/183587/26350
*/
add_action( \'the_post\', function( $post )
{
if ( false !== strpos( $post->post_content, \'<!--nextpage-->\' ) )
{
// Reset the global $pages:
$GLOBALS[\'pages\'] = [ $post->post_content ];
// Reset the global $numpages:
$GLOBALS[\'numpages\'] = 0;
// Reset the global $multipage:
$GLOBALS[\'multipage\'] = false;
}
});
}
//**IF Statement that should set the cookie**//
if ( empty( $_GET[\'content\'] ) || "multipage" !== $_GET[\'content\'] ) {
//**Only removes onepage() when content=multipage**//
add_action(\'wp\',\'onepage\');
//**Should set cookie only when content=multipage**//
setcookie( \'content\', \'multipage\', time()+3600, COOKIEPATH, COOKIE_DOMAIN );
}
生成post multipage的代码如下:
//**PAGEBREAK SECTION - BEG**//
function pagebreak( $args = \'\' ) {
global $page, $numpages, $multipage, $more;
$defaults = array(
\'before\' => \'<p class="post-nav-links">\' . __( \'Pages:\' ),
\'after\' => \'</p>\',
\'link_before\' => \'\',
\'link_after\' => \'\',
\'aria_current\' => \'page\',
\'next_or_number\' => \'next\',
\'separator\' => \' \',
\'nextpagelink\' => __( \'Next page\' ),
\'eog\' => \'<a href="http://127.0.0.1:10080/wordpress/">\',
\'previouspagelink\' => __( \'Previous page\' ),
\'pagelink\' => \'%\',
\'echo\' => 1,
);
$params = wp_parse_args( $args, $defaults );
/**
* Filters the arguments used in retrieving page links for paginated posts.
*
* @since 3.0.0
*
* @param array $params An array of arguments for page links for paginated posts.
*/
$r = apply_filters( \'wp_link_pages_args\', $params );
$output = \'\';
if ( $multipage ) {
if ( \'number\' == $r[\'next_or_number\'] ) {
$output .= $r[\'before\'];
for ( $i = 1; $i <= $numpages; $i++ ) {
$link = $r[\'link_before\'] . str_replace( \'%\', $i, $r[\'pagelink\'] ) . $r[\'link_after\'];
if ( $i != $page || ! $more && 1 == $page ) {
$link = _wp_link_page( $i ) . $link . \'</a>\';
} elseif ( $i === $page ) {
$link = \'<span class="post-page-numbers current" aria-current="\' . esc_attr( $r[\'aria_current\'] ) . \'">\' . $link . \'</span>\';
}
/**
* Filters the HTML output of individual page number links.
*
* @since 3.6.0
*
* @param string $link The page number HTML output.
* @param int $i Page number for paginated posts\' page links.
*/
$link = apply_filters( \'wp_link_pages_link\', $link, $i );
// Use the custom links separator beginning with the second link.
$output .= ( 1 === $i ) ? \' \' : $r[\'separator\'];
$output .= $link;
}
$output .= $r[\'after\'];
} elseif ( $more ) {
$output .= $r[\'before\'];
$prev = $page - 1;
$next = $page + 1;
if ( $next <= $numpages ) {
if ( $prev ) {
$output .= $r[\'separator\'];
}
$link = _wp_link_page( $next ) . $r[\'link_before\'] . $r[\'nextpagelink\'] . $r[\'link_after\'] . \'</a>\';
/** This filter is documented in wp-includes/post-template.php */
$output .= apply_filters( \'wp_link_pages_link\', $link, $next );
} elseif ( $next = $numpages ) {
$link = _wp_link_page( $next ) . $r[\'eog\'] . $r[\'nextpagelink\'] . $r[\'link_after\'] . \'</a>\';
}
$output .= $r[\'after\'];
}
}
/**
* Filters the HTML output of page links for paginated posts.
*
* @since 3.6.0
*
* @param string $output HTML output of paginated posts\' page links.
* @param array $args An array of arguments.
*/
$html = apply_filters( \'wp_link_pages\', $output, $args );
if ( $r[\'echo\'] ) {
echo $html;
}
return $html;
}
/**
* Replace [nextpage] with <!--nextpage--> through the \'the_posts\' filter.
*
* @see http://wordpress.stackexchange.com/a/183980/26350
*/
! is_admin() && add_filter( \'the_posts\', function( $posts )
{
$posts = array_map( function( $p )
{
if ( false !== strpos( $p->post_content, \'[pagebreak]\' ) )
$p->post_content = str_replace( \'[pagebreak]\', \'<!--nextpage-->\', $p->post_content );
return $p;
}, $posts );
return $posts;
});
//**PAGEBREAK SECTION - END**//
我查看了浏览器的cookie列表,显然这确实设置了cookie
content
到值
multipage
.
然而,它似乎设置了cookie,无论URL是否具有URL参数。
当我转到下一页时,布局会重置为一页,因此我假设站点不读取cookie。
是否可以存储?content=multipage
在cookie中,使站点成为multipage
对于该用户或?content=multipage
必须从一页传递到另一页?
最合适的回答,由SO网友:Sally CJ 整理而成
我同意将查询字符串从一个页面传递到另一个页面是最可靠的解决方案,例如,您可以使用如下过滤器post_link
和term_link
添加content
帖子/类别链接的查询字符串。
但是,它需要挂接到各种过滤器,如您所见here. 此外,多页URLexample.com/page/2?content=multipage
没有额外的编码就无法工作
因此,您可能会更好/更容易地将多页状态存储在浏览器的cookie中,就像您尝试的那样&mdash;代码中的问题是,您只是在设置cookie,而没有读取它。您可以使用$_COOKIE[\'content\']
读取cookie的值,如下面的示例所示。
代码/解决方案首先从代码中删除:
//**IF Statement that should set the cookie**//
if ( empty( $_GET[\'content\'] ) || "multipage" !== $_GET[\'content\'] ) {
//**Only removes onepage() when content=multipage**//
add_action(\'wp\',\'onepage\');
setcookie( \'content\', \'multipage\', time()+3600, COOKIEPATH, COOKIE_DOMAIN );
}
并在
//**PAGEBREAK SECTION - END**//
在代码中:
add_action( \'init\', \'set_check_content_cookie\', 0 );
function set_check_content_cookie() {
if ( is_admin() ) {
return;
}
$is_multipage = false;
// If the query string "content" is not set, check if the "content" cookie value is exactly "multipage".
if ( ! isset( $_GET[\'content\'] ) && isset( $_COOKIE[\'content\'] ) && \'multipage\' === $_COOKIE[\'content\'] ) {
$is_multipage = true;
}
// If the query string "content" is set and its value is "multipage", set the cookie.
elseif ( isset( $_GET[\'content\'] ) && \'multipage\' === $_GET[\'content\'] ) {
setcookie( \'content\', \'multipage\', time()+3600, COOKIEPATH, COOKIE_DOMAIN );
$is_multipage = true;
}
// Hook onepage() to `wp`, if applicable.
if ( ! $is_multipage ) {
add_action( \'wp\', \'onepage\' );
}
}
Note: 您应该记住,使用此解决方案意味着必须在浏览器中启用Cookie(即受支持且用户正在接受Cookie)