Difference between revisions of "Week Eleven (MGDP2060)"

From Wiki @ Karl Jones dot com
Jump to: navigation, search
(In the News)
(Advanced Custom Fields (WordPress plugin))
Line 80: Line 80:
  
 
See [[Advanced Custom Fields (WordPress plugin)]].
 
See [[Advanced Custom Fields (WordPress plugin)]].
 +
 +
Code samples:
 +
 +
<pre>
 +
<p><?php the_field('field_name'); ?></p>
 +
</pre>
 +
 +
And:
 +
 +
<pre>
 +
<?php
 +
 +
$variable = get_field('field_name');
 +
 +
// do something with $variable
 +
 +
?>
 +
<?php
 +
 +
And:
 +
 +
if(get_field('field_name'))
 +
{
 +
echo '<p>' . get_field('field_name') . '</p>';
 +
}
 +
 +
?>
 +
</pre>
  
 
== Displaying custom fields in custom post types ==
 
== Displaying custom fields in custom post types ==

Revision as of 16:58, 10 November 2016

This article lists topics for week eleven of Web Design and Development III (MGDP2060).

See also Week Ten and Week Twelve.

In the News

Displaying post thumbnail (featured image)

Use this code to display the post thumbnail (featured image) for an individual post:

// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
	the_post_thumbnail();
} 
the_content();


Making a Smarter Product Page

Individual product pages display unnecessary information, like author name.

Individual product pages are displayed by the single.php template.

Highlighting Products on Sale

The "Better Home Page" example is meant for smaller websites, where the number of items is small enough to display well on the home page.

Larger websites, with a large number of items, may need some other approach.

For example a "Products on sale" or "Featured" category.

Each product belongs to both "Sofas" and "Featured" categories.

Or perhaps "Featured1", "Featured2", and "Featured3", corresponding to three columns.

Cleaning Out the Templates (536)

Individual product pages are displayed by the single.php template.

The single.php template gets help from another template, depending on type of content it's displaying.

Ordinary posts use the content-single.php template to do the work.

When displaying a post that uses a custom post type, WordPress automatically looks for a template file in this form:

single-type.php

Where type is the registered name of the custom post type, in this case "product":

single-product.php

Use this template to display content for individual products.

Creating single-product.php (537)

Make a copy of single.php. Name the copy single-product.php.

Remove the navigation links.

Modify the get_template_part function:

<?php get_template_part( 'content', 'single-product' ); ?>

Creating content-single-product.php

Make a copy of content-single.php. Name the copy content-single-product.php.

Custom Fields (539)

See WordPress Custom Field.

Advanced Custom Fields (WordPress plugin)

See Advanced Custom Fields (WordPress plugin).

Code samples:

<p><?php the_field('field_name'); ?></p>

And:

<?php

$variable = get_field('field_name');

// do something with $variable

?>
<?php

And:

if(get_field('field_name'))
{
	echo '<p>' . get_field('field_name') . '</p>';
}

?>

Displaying custom fields in custom post types

Find the code for 'Products' custom post type in functions.php as shown below:

'supports' => array ( 'title', 'editor', 'thumbnail', 'excerpt'),

Add code, the new code looks like this:

'supports' => array ( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-field'),

This will add custom field(s) to the screen options for Products. See below for more information about custom fields.

Classifying Your Posts with Custom Taxonomies (542)

You add custom taxonomies of your own: like Categories or Tags, but using terminology customized for a specific purpose.

See

Exercises

See Week Eleven Exercises (MGDP2060).