Custom Pages with Wordpress 3.4.1 - php

I am having some problems trying to get a custom page to display on my site. What I want is to create a page where the client can edit the content in Wordpress. When I add the page using the Wordpress "add new page" it doesn't show, but if I create a page in .php with the same content, it works fine. This is what I did:
First I created a blank .php template called lefthome.php using the following code:
<?php
/*
Template Name: Left Template
*/
?>
<?php get_template_part( 'loop', 'page' ); ?>
I then went into Wordpress, clicked on pages > add new. I gave my page a title and added the content I wanted to appear on the page. Then from the template option in the page attributes I choose the template I had earlier created (Left Template) and clicked update.
I wanted this template to appear on the homepage, so I tried to add it to the homepage template I had created using the following code:
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php include (TEMPLATEPATH . '/lefthome.php'); ?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>
The header, slider.php and footer all show fine. The contents of the lefthome.php do not appear. I do not know how to get it to show. The only way I have got it to show is to paste the contents into the lefthome.php template. I don't want to do this though, as the client will not be able to edit the contents themselves.
I hope someone can help me.
Thanks

You won't be able to display the content of a page just by calling directly its template file. You need to query the page itself. In that matter it won't matter what template you've already assigned to it anymore.
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php
global $post;
$page_id =5; // 5 is the id of the page we want to present with Left Template.
$post = get_post( $page_id );
setup_postdata( $post );
include locate_template( 'lefthome.php' );
wp_reset_postdata(); // we are done with that. reset the query so the rest don't mess up
?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>

Related

Insert a Wordpress contact form 7 in a custom template page

I've been trying to insert a WP contact form 7 in a custom 'blank' page template.
Actually what I'm trying to do is insert the WP CF7 functionality in a very basic, clean page (just a nice background, able to adjust font, images, ...) but I don't want the page the look like this blogpost from the WP theme..
I've tried to upload a white page template with FTP,
but when I try to embed to WPCF7 code in the new page with the blank template, nothing happens...
Anybody who can help me out?
Thanks a lot!
White page template
<?php /* Template Name: Blank Page
*
* A blank custom page template.
*
* The "Template Name:" bit above allows this to be selectable
* from a dropdown menu on the edit page screen.
*
*/
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
Write Your code Like this:
<?php
/* Template Name: Blank Page
*
* A blank custom page template.
*
* The "Template Name:" bit above allows this to be selectable
* from a dropdown menu on the edit page screen.
*
*/
get_header();
$id= get_the_ID();
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
get_footer();

Proper way to make function calls in WordPress

What is best practice for making a function call on a wordpress page?
For instance if you want to call my_special_function(); on the home page, where is the proper place to put the function call (ie home-page.php, /template-parts/content-page.php etc.).
<?php if( function_exists( my_special_function ) ) {
my_special_function();
} ?>
FYI Im using Underscores theme.
I've seen a few comments regarding shortcode. Would something like the below WP page template with the shortcode inserted be a best practice over just calling the function on that page?
So if I wanted the function to be called in the page template below I would just insert the shortcode wherever I want it on that page template or just is just calling the function is sufficient or best practice
<?php
/**
* Template Name: Home Page
*
* The template for displaying the home page.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Rollins_Ridge
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
// insert Shortcode
<?php echo do_shortcode('[special_function_shortcode]') ;?>
// or just call the function
my_special_function();
// or something else im not aware of
code here;
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
Don't edit themes core files.
the proper way is using functions.php file in child theme directory.
you can add your function in this file and create shortocde using wordpress hook
like
my_special_function(){
//Your content goes here.
echo "This is proper way";
}
add_shortcode('special_function_shortcode','my_special_function');
and use following shortocde anywhere on site.
in wordpress page use [special_function_shortcode]
and in php use
<?php echo do_shortcode('[special_function_shortcode]') ;?>

Hook pages by id on static front-page

I would like to build a one page website upon the Wordpress CMS. I'm looking for a way too hook my pages on a static frontpage by id, since I have several page types.
I've tried:
$id=6;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
But this returns only the content and not the content with the HTML. Any tips on how to achieve this? or other ways to build a one-page website upon Wordpress?
Thanks in advance
To output the HTML properly, try using PHP's HTML entity encoding/decoding related functions to convert entities back to their applicable characters (e.g. < and >).
WordPress has an internal function called wpautop that converts double linefeeds (\n) into paragraph splits. wp_texturize (or something similarly named) converts some characters to proper variants, such as quote marks and similar.
Try this:
$content = apply_filters( 'the_content', wpautop( wp_texturize( html_entity_decode( $post -> post_content ) ) ) );
// Note: not properly tested, but should probably output properly "HTMLized" contents.
If you want to output something else than the HTML tags that are inside the post contents (and inserted using the post editor in wp-admin), you'll have to work with the theme templates.
EDIT:
To insert whole templates into the homepage to create a single-page website, you'll need to create each "page" as a template file. Assuming you have front-page.php, content-about.php, content-portfolio.php and so on. Additionally you most probably have header.php and footer.php.
In simplified form, each of the content templates that would be inserted to front-end.php could look something like this:
<?php
/**
* content-about.php
*/
// Get the wanted content as WP Post.
$page_obj = get_page_by_title( 'About' );
$page_content = wpautop( $page_obj -> post_content );
?>
<article id="about"> <!-- Begin a new section for the one-page template. -->
<h2><?php echo $page_obj -> post_title; ?></h2>
<?php echo $page_content; ?>
</article>
Additional templates (content-portfolio.php, content-contact.php and so on) should follow a similar structure.
Then in front-page.php you need to include your header.php and footer.php as you normally would. Then inbetween use get_template_part calls to fetch and display the content-abc.php templates within front-page.php:
<?php
/**
* front-page.php
*/
get_header(); ?>
<!-- Add some markup if you want to. -->
<?php get_template_part( 'content', 'about' ); // 'content-about.php' ?>
<?php get_template_part( 'content', 'portfolio' ); // 'content-portfolio.php' ?>
<?php get_template_part( 'awesometemplate' ); // 'awesometemplate.php' ?>
<!-- Add some markup if you want to. -->
<?php get_footer();
Now after WordPress and PHP parse the front-page.php template, the resulting output might look like this (depending on what you insert into each included template):
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<!-- header.php visible contents here -->
<!-- content-about.php: -->
<article id="about">
<h2>About</h2>
<p>Well hello there! This is some <em>nice</em> content from content-about.php.</p>
</article>
<!-- content-portfolio.php: -->
<article id="portfolio">
<h2>Portfolio</h2>
<ul>
<li>
...
</li>
</ul>
</article>
<!-- awesometemplate.php: -->
<article id="awesome">
<h2>Awesome!</h2>
<table>
...
</table>
</article>
<!-- footer.php visible contents here -->
</body>
</html>
Now you've got separated templates that hold content and are embedded into front-page.php to create a single-page master template. The it's up to you to use CSS and JS to make it flashy if you want to.
Note: you can also use PHP's very own include or require functions to insert the sub-templates to front-page.php.
You can also use WP_Query to construct a custom query. You can use the Post and Page parameters to call your pages you need to display on your front page.
Inside the loop you'll have the ability to directly work with the template tags like the_content. You can also load template tags and HTML structures conditionally inside the loop on a per page basis with the use of is_page() or is_page_template()
Here is an example from the codec. Modify to suite your needs
$the_query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Wordpress Page of Posts

I am building a personal website to host my university work, personal projects and photos etc.
The menu is a hierarchical structure made up of pages and links. Take my university pages for example. What I would like to achieve is to display posts that are related to the module code which is the page's slug.
I've used the following link http://codex.wordpress.org/Page_Templates#A_Page_of_Posts and managed to get it working but I have hard coded the module code into the template, meaning for each module I will have to have a separate template and the only thing that will be different from one file to the next is 5 characters which isn't great for code re-use.
What I am asking, is, is there a way to get the slug from the page I'm looking at and use that for the WP_Query arguments.
If you go to http://michaelnorris.co.uk/ and look at the menu structure. Navigate to University -> Year Three -> Individual Project, you will notice the url is http://michaelnorris.co.uk/uni/three/ci301 where ci301 is the module code for the Individual Project. I want to have this system on each of the module pages so that I can tag posts and they are displayed in the relevant module.
Ok, I actually found the answer myself, but for others looking to do the same. Below is a solution.
Solution found here on the Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts
Name the file pageofposts.php and edit the Page within the Wordpress Dashboard and set the Template (in the dropdown) to 'Page of Posts'. Bingo!
<?php
/*
Template Name: Page Of Posts
*/
/* This example is for a child theme of Twenty Thirteen:
* You'll need to adapt it the HTML structure of your own theme.
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
/* The loop: the_post retrieves the content
* of the new Page you created to list the posts,
* e.g., an intro describing the posts shown listed on this Page..
*/
global $post;
$slug = get_post( $post )->post_name;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display content of page
get_template_part( 'content', get_post_format() );
wp_reset_postdata();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// Change these category SLUGS to suit your use. category_name is comma separated.
'tag' => $slug,
'paged' => $paged
);
$list_of_posts = new WP_Query( $args );
?>
<?php if ( $list_of_posts->have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
<?php // Display content of posts ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>

Get ID for static homepage, not post

I am starting to write a custom theme for Wordpress. I have created a new page called 'Home' and set the front page to be a static page, selecting 'Home'. I want this page to show all posts with a category of 'news' plus a couple of images.
I then added front-page.php with the following:
<?php get_header(); ?>
<div class='detail'>
<?php if ( have_posts() ) {
query_posts( 'category_name=news' );
while ( have_posts() ) : the_post(); ?>
<h4><?php the_date('d/m/Y'); ?> - <?php the_title(); ?></h4>
<div class='post'><?php the_content(); ?></div>
<?php endwhile; }?>
</div>
<?php get_footer(); ?>
I've uploaded a couple of images and attached them to the 'Home' page. I now want to get the URL for the attached images. I've tried something like:
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID());
but this only returns the ID of the most recent post being shown, even if I put the above code outside the loop. If I remove the static front page and navigate to the Home page then I get the correct results, any ideas how I can get the images for the Home page when I use it as a static page?
Forgive me if this is simple, first foray into PHP and wordpress development
I think your issue is that you're using get_the_ID(); outside of The Loop. the_ID(); and get_the_ID(); grab the current post ID from the loop; if used outside of The Loop, all you'll get is the last one. See: http://codex.wordpress.org/Function_Reference/get_the_ID
To get the ID of the current page, try:
$page=get_page_by_title($page_name);
$images =& get_children('post_type=attachment&post_mime_type=image&post_parent='.$page->ID);
If that doesn't work, there's a function at http://wordpress.org/support/topic/how-to-get-page-id-using-php?replies=5 (Where I found the above code) that does the same thing.
Hope this helps!

Categories