WordPress 网页跳转方案
在特定页面判断没有登录时,跳转到登录页
if (is_page('page-redirecte-to-home')){
if( ! is_user_logged_in() ) {
echo '<meta http-equiv="refresh" content="0;URL=\''. home_url() . '/login/' .'\'" />';
exit;
}
}
在function.php中判断跳转:
function antto_redirect()
{
if( ! is_user_logged_in() )
wp_redirect( '/wp-login.php' );
}
add_action( 'get_header', 'antto_redirect' );
js判断跳转:
eg:当select选择value为admin时,跳转到相应页面;
jQuery(document).ready(function(){
jQuery('#register_role').change(function() {
if(jQuery(this).val() == 'admin' ){
window.location.href = "<?php echo esc_url(home_url('/')); ?>admin-registration/";
}
});
});
js延时跳转
jQuery(document).ready(function(){
jQuery('#submit_button').submit(function () {
setTimeout(function() { window.location.href = "http://www.anttoweb.com/";}, 500);
});
});
扩展:
页面(page)模板跳转;
新建page-redirect.php文件;页面选择当前模板,访问页面时会自动跳转到当前页面的第一个子页面;
<?php
/*
Template Name: 跳转模板
*/
//Page Redirect to first sub page
?>
<?php get_header(); ?>
<div id="site-container">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$post = get_post();
$args = array(
'sort_order' => 'asc',
'sort_column' => 'menu_order',
'hierarchical' => 1,
'child_of' => $post->ID,
'post_status' => 'publish'
);
$pagekids = get_pages($args);
if ($pagekids) {?>
<?php
$firstchild = $pagekids[0];
$redirect_url = get_permalink($firstchild->ID);
echo '<meta http-equiv="refresh" content="0;URL='.$redirect_url.'" />';
exit;
?>
<?php }?>
<!-- PAGE TITLE -->
<?php endwhile;?>
<?php endif;?>
</div>
<?php get_footer(); ?>
分类(模板)跳转;
利用ACF新建一个checkbox, redirect_cat_url,勾选时会跳转到第一个子分类;
<?php get_header(); ?>
<div id="site-container">
<!--分类自动跳转到子分类-->
<?php
//得到当前的分类id
$cus_tax_id = get_queried_object_id();
//ACF插件提供的字段
$queried_object = get_queried_object();
$redirect_cat_field = get_field('redirect_cat_url', $queried_object );
if($redirect_cat_field){
$args = array(
'orderby' => 'id',
'order' => 'ASC',
'parent'=> $cus_tax_id,
'hide_empty' => 0,
'hierarchical' => 1,
'taxonomy' => 'product_cats' //注册的分类名
);
$categories_list = get_categories($args);
$redirect_cat_id = $categories_list[0];
$redirect_cat_url_final = get_term_link($redirect_cat_id, 'product_cats' );
echo '<meta http-equiv="refresh" content="0;URL='.$redirect_cat_url_final.'" />';
exit;
}
?>
</div>
<?php get_footer(); ?>
分类的排序因为默认不包含排序这个字段,可以自行修改orderby和order的属性来达到目的;如果分类较多,不能完美实现需要的结果,推荐插件Category Order and Taxonomy Terms Order,一次性解决所有分类排序问题。
跳转自定义文章页面
<?php echo get_post_type_archive_link( get_post_type() ); ?>
跳转到一个新页面
<?php echo get_site_url();?>/page_name
2,628 total views, 12 views today
Revisions
- 2021年1月8日 @ 18:02:04 [当前版本] by Bruce
- 2021年1月8日 @ 18:02:04 by Sky
- 2021年1月8日 @ 17:58:10 [自动保存] by Sky
- 2021年1月8日 @ 17:12:31 by Sky
- 2020年12月12日 @ 14:13:38 by Anson
Comments are closed.