There are lot of ways to do this. But this is simplest of all but not simpler than responsive site. This method is used to redirect mobile users to another sub-domain. So that you can reduce the load of the server from the same domain. Here I used another sub-domain for mobile version.
First you have to add redirect function to your sub-domain (http://m.example.com). So open the functions.php from your theme folder. Add the redirect function as given below:
function check_is_mobile(){
$cur_url = esc_url("http://".$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$mob_url= 'http://m.example.com/';
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android'))
{
$ex = explode("/",$cur_url);
$postt_id= $ex[4];
if(!empty($postt_id)){
wp_redirect($mob_url.'post.php?id='.$postt_id);
exit;
}
else{
wp_redirect($mob_url);
exit;
}
}
}
add_action('wp_head','check_is_mobile',1);
Then create an index.php file in your sub-domain folder and include the wp-blog-header.php. Then display list of post with the below code.
<?php require($_SERVER["DOCUMENT_ROOT"].'/blog/wp-blog-header.php');?>
<div id="content">
<?php
$args = array( 'numberposts' => 10, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
$postslist = get_posts($args);
foreach ($postslist as $post) : setup_postdata($post); ?>
<?php add_filter('excerpt_more', 'new_excerpt_more');?>
<div class="inner_box">
<p class="art_head"><a href="<?php echo $_SERVER["REQUEST_URI"]; the_ID(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
<div class="description">
<?php the_excerpt();?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php
function new_excerpt_more( $more ) {
return '[.....]';
}
?>
No comments:
Post a Comment