More than 200 hooks exist in WordPress. Below you will find a few examples of common hooks in use.
Register a Custom Menu in the Admin
Register a Custom Menu in the Admin
[contact-form-7 id="1234" title="Contact form 1"]
. To insert the contact form into your post, copy the shortcode and paste it into the post content.
Registers a new image size. This means WordPress will create a copy of the image with the specified dimensions when a new image is uploaded.
To set the default Featured Image (formerly Post Thumbnail) dimensions use: set_post_thumbnail_size().
<?php add_image_size( $name, $width, $height, $crop ); ?>
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails');
set_post_thumbnail_size( 200, 200, true );
}
What does above code really mean?get_post_thumbnail_id($post->ID)
points to full-sized thumbnails (like 2500x1200). I don't really need that big images...if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails');
set_post_thumbnail_size( 900, 600, true );
add_image_size( 'foo', 400, 300, true );
add_image_size( 'bar', 200, 250, true );
}
<?php add_image_size( $name, $width, $height, $crop ); ?>
<?php
$post_type = get_post_type(); // get's the post type of a post in the loop
$args=array(
'public' => true,
'_builtin' => false
);
$output = 'names'; // names or objects
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types($args,$output,$operator); // gets all post types you registered
$post_types = array_values($post_types); // makes it a numerically indexed array
if(in_array( $post_type, $post_types)){ // post is a custom post type -> show post type size thumbnail
$custom_post_thumbnail = get_the_post_thumbnail($post->ID, $post_type.'-thumbnail');
if($custom_post_thumbnail != ''){ // there is a custom post thumbnail size
echo $custom_post_thumbnail;
} else { // no custom post thumbnail size -> show normal thumbnail
echo get_the_post_thumbnail($post->ID);
}
} else { // show normal thumbnail for posts and pages
echo get_the_post_thumbnail($post->ID);
}
?>
You still have to add the image sizes manually in your theme's functions.php
If you have custom post types review and movies:
add_image_size( 'review-thumbnail', 150, 200, true );
add_image_size( 'movies-thumbnail', 400, 9999 );
<?php
function
getFeaturePosts(){
?>
<ul
class
=
"related-posts"
>
<?php
$default_thumbnail
=
'http://img.techbrij.com/techbrij%20logo.gif'
;
$the_query
=
new
WP_Query(
'showposts=5&orderby=rand&category_name=featuredcategory'
);
while
(
$the_query
->have_posts()) :
$the_query
->the_post(); ?>
<li>
<div
class
=
"related_thumbnail"
>
<a href=
"<?php the_permalink() ?>"
rel=
"bookmark"
title=
"<?php the_title_attribute(); ?>"
>
<?php
if
(has_post_thumbnail()):
the_post_thumbnail();
else
:
?>
<img src=
"<?php echo $default_thumbnail; ?>"
alt=
"<?php the_title(); ?>"
/>
<?php
endif
; ?>
</a>
</div>
<div style=
"clear:both;"
></div>
<div
class
=
"related_permalink"
>
<a href=
"<?php the_permalink() ?>"
rel=
"bookmark"
title=
"<?php the_title_attribute(); ?>"
><?php the_title(); ?></a>
</div>
</li>
<?php
endwhile
; ?>
<?php wp_reset_query(); ?>
</ul>
<?php
}
?>
In the above code, you need to define $default_thumbnail parameter and assign default thumbnail link. If there is no thumbnail then defined $default_thumbnail link will be displayed.
Following line is the most important line.
$the_query = new WP_Query(‘showposts=5&orderby=rand&category_name=featuredcategory’);
showposts=5: define number of posts to be displayed
orderby=rand: posts are randomly selected
category_name=featuredcategory: define category of featured posts.
3. Add following styles in your css file:
/* Related OR Feature Posts */
.related-posts {
list-style
:
none
;
margin
:
0
;
padding
:
0
;
}
.related-posts li {
display
:
block
;
text-align
:
center
;
float
:
left
;
margin
:
0
auto
;
padding-left
:
3px
10px
3px
0px
;
width
:
150px
;
height
:
175px
;
}
.related-posts li :hover{
}
.related_permalink{
text-align
:
center
;
padding-top
:
5px
;
padding-bottom
:
5px
;
margin-bottom
:
2px
;
}
.related_permalink a{
color
:
black
;
font-weight
:
bold
;
text-decoration
:
none
;
}
.related_permalink a:hover{
text-decoration
:
underline
;
}
.related_thumbnail{
text-align
:
center
;
}
.related_thumbnail img
{
border-width
:
0px
;
width
:
100px
;
float
:
none
;
margin
:
0
auto
;
}
/* Related Posts End */
you can change thumbnail size by defining height and width in .related_thumbnail img style.
4. Now call method where you want to display featured posts.
<?php getFeaturePosts(); ?>
See following sample display of featured posts: