• Hello
    i added a new page template with custom edit and called it template-mobile-app.php , it show posts and works well , Now how can i force the posts that shown in this template to view with custom single file not default one ? it’s possible ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    On this page template, what are you trying to display? The page’s content entered on it’s backend edit screen in the tinyMCE editor? Or are you querying other posts and want to display their content?

    Either way you generally use the_content(), the setup in each case is different though. I don’t quite understand what you’re trying to do. A page template is a sort of single post template that is specific for pages. Why do you want another single template? A single template is a full page template — header, body, sidebar, footer. Do you want all this within your page template that is also outputting header, body, sidebar, footer as well?

    I would think you want a template part, not a full page. Many themes compose their templates of these parts that are common to several full page templates. Thus code can be updated on one file instead of updating the same thing on several files. You load template parts with get_template_part(). Such templates need to be in the theme’s folder somewhere.

    You can also load template parts like you would load any PHP code file from another, with require or include statements. get_template_part() essentially does the same thing, but it has special functionality to find the right file in the theme and ensuring the proper text domain is loaded for it.

    Thread Starter ahmedmz

    (@ahmedmz)

    Hi , thanks for reply
    first i will tell you what i do .
    i’m working on small Mobile App that only view last posts from specified category .(will put it in Mobile App as Iframe) that’s away from default version
    i it should be without header and footer and sidebar , so i thought in using template file .
    1-i copied file template-blog.php from theme to new file with name template-mobile-app.php and i deleted header and footer from it .
    2-i created an empty page “app-template” and changed Template from Page attributes to the new one .
    3- https://www.website.com/app-template .the link works as i want, to put it in Mobile App Iframe , but when i browse posts , it shown with header and footer and it’s the issue , i want it Without it.

    (this page is only for Mobile App) .
    The idea is that this is done entirely within the specified template so that the site is not affected by this customization

    I hope you understand what is required
    how can i do it ?
    thanks

    Moderator bcworkz

    (@bcworkz)

    Thanks for the explanation, yes I understand. I’m unsure why you are getting extra page content, it may be iframe related. I agree a custom page template is a good approach. I would advise not copying any more than necessary from theme files. Only enough so that the CSS styles are properly applied. Build your template the way you want it to be.

    While you do not need to load the header, you do need to call wp_head() or external resources will not be loaded. Also call wp_footer() at the very end. Despite the names, these generally do not generate visible output, they merely do certain actions that are required to load scripts and stylesheets. You do need the minimal HTML from your header template — !DOCTYPE, html, head, charset, viewport, and body elements. Plus any major containers used by your theme.

    Make a new WP_Query object that gets the desired posts and then run a Loop to display the posts in whatever manner you wish. No need to include or load other templates. Put it all on your one page template that stands on its own.

    A completely different approach would be to get the posts data you want using the REST API. It will come as JSON data. It would be up to your app to parse the data and compose the output. Maybe you don’t want to consider this right now, but I urge you to look into this sometime soon if you intend to continue developing mobile apps.

    Thread Starter ahmedmz

    (@ahmedmz)

    thanks alot
    please can you help me to do this

    this is the template file

    <?php 
    /*
    Template Name: Mobile App Template
    */
    ?>
    
    <?php get_header('mobapp'); ?>
    
     
    	<div class="content">
     		
    		<?php if( get_query_var('page') ) $paged = get_query_var('page') ; ?>
    
     		
    		<div class="page-head">
    			<h1 class="page-title">
    				<?php the_title(); ?>
    			</h1>
    		</div>
    		
     
    		
    		<?php
    			$blog_cats = unserialize($get_meta["blog_cats"][0]);
    			if( empty( $blog_cats ) ) $blog_cats = get_all_category_ids();
    			
    			query_posts( array( 'paged' => $paged , 'category__in' => $blog_cats ));
    			get_template_part( 'loop', 'category' );
    			if ($wp_query->max_num_pages > 1) pagenavi(); 
    			?>
    		
     
    		
    		<?php comments_template( '', true ); ?>
    	</div><!-- .content -->
    
     <?php get_footer('mobapp'); ?>

    well it display list of posts with Specific header and footer
    Please keep in mind that the page of this template works without a problem , but when you go to the page displaying the Selected news like(/?p=123) it appears containing the default header and footer of theme.

    Moderator bcworkz

    (@bcworkz)

    Ah! I got it now (I hope)! So the post ID 123 is one of the posts listed on this template. When following the link, you want it to show the mobile app header/footer. But on a lap or desktop, directly requesting that same post should show the normal header/footer. Am I correct? Sorry for being a little dense, I was on another track entirely.

    Well, even though it’s not what you want, that is normal, expected behavior for WP, but that can be changed. Do you have a similar template for non-mobile devices? Is the header/footer the only difference? How do mobile users come to be using this template? Are you using wp_is_mobile()? You could use wp_is_mobile() to serve one header/footer or the other, all on a single template. This alone still would not help the single post ID 123, but you could also use the same wp_is_mobile() logic on single.php and any other templates to load the mobile content when appropriate.

    A completely different approach would be to load the clicked post via Ajax on the same template/page, either replacing the initial list or overlaying it using a modal. The way the single post is displayed would depend on what your Ajax client script and server handler do together. It’s conceivable a template could be part of this, but you are better off developing your own code. Use the same containers and classes, etc. that your theme does so the appearance is consistent. With Ajax, you would need to capture back button events to deliver the expected results because the device thinks the user is still on the your initial page and an uncaptured back button would load whatever was up before your page instead of going back to the initial list.

    It’s not so important that it must be addressed right away, but you shouldn’t be using query_posts() these days. There’s more efficient ways to do queries. Either instantiate a new WP_Query object (or use get_posts()) or use the “pre_get_posts” action to alter the original request’s query vars to meet your needs.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘custom template posts issue’ is closed to new replies.