wordpress tutorial, how to check custom post type in wordpress. Many we want to execute some code on custom post type or want to execute code on post. Many we want to execute some code on custom post type or some times we want to execute code on normal pages or post.
how to check custom post type in wordpress
So here is solution. You just need to just add following code into your functions.php file which you can find in your wordpress theme folder.
function check_custom_post_type() {
global $wp_query;
$post_types = get_post_types(array('public' => true,'_builtin' => false),'names','and');
foreach ($post_types as $post_type ) {
if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) {
return true;
} else {
return false;
}
}
}
After this you can use following function in any wordpress file. For example you can use this method in single.php file.
if (check_custom_post_type()) {
//Current post is a custom post type
}
How to show related posts wordpress without plugin using category. Showing related article in wordpress site is always good for users to attract visitors. In many blogs people are showing the related or linked articles. Showing similier or related articles will increaze your site SEO. show related posts without wordpress plugin using category in wordpress site is always good for users to attract the visitors.
show related posts wordpress without plugin
More visitor will visit your site. For showing the related articles you need copy following code into your functions.php file.
[viral-lock message=”Code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]
// "Similier Articles"
function wpapi_more_from_cat( $title = "Similier Articles:" ) {
global $post;
// We should get the first category of the post
$categories = get_the_category( $post->ID );
$first_cat = $categories[0]->cat_ID;
// Let's start the $output by displaying the title and opening the <ul>
$output = '<h3>' . $title . '</h3>';
// The arguments of the post list!
$args = array(
// It should be in the first category of our post:
'category__in' => array( $first_cat ),
// Our post should NOT be in the list:
'post__not_in' => array( $post->ID ),
// ...And it should fetch 5 posts - you can change this number if you like:
'posts_per_page' => 5
);
// The get_posts() function
$posts = get_posts( $args );
if( $posts ) {
$output .= '<ul>';
// Let's start the loop!
foreach( $posts as $post ) {
setup_postdata( $post );
$post_title = get_the_title();
$permalink = get_permalink();
$output .= '
<ul>
<li>permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</li>
</ul>
';
}
$output .= '</ul>';
} else {
// If there are no posts, we should return something, too!
$output .= '<p>Sorry, this category has just one post and you just read it!</p>';
}
echo $output;
}
[/viral-lock]
After that open your single.php file and use following code.
wpapi_more_from_cat();
Using above code you can show the related articles in your wordpress site.
One of my client faced issue with Autor drop down which is in Admin section.
Display the authors in dropdown menu using hooks.
While creating the New post there was problem with the Author field. There are hundreds of irrelevant selections (users) and it’s difficult to select the right one.
WordPress is by default showing all the users in author drop down. I don’t want to show the other users in author drop down.
I searched for wp_dropdown_users hook or filter. But I did not found any proper solution.
Following articles are found helpful to me.
http://wordpress.org/support/topic/filter-for-post-quick-edit-author-drop-down
http://codex.wordpress.org/Function_Reference/wp_dropdown_users
Using that code I modified the code and I am able to fix the issue. You can put following code in to functions.php file.
/*
* Hook for showing Admin and Author in Add new Post - Admin section dropdown menu
*/
function wpapi_override_wp_dropdown_users($output) {
global $post, $user_ID;
//get the Admin-role users IDs
$admins = getUsersWithRole('admin');
//get the author-role users IDs
$authors = getUsersWithRole('author');
//merge the array
$result = array_merge($admins, $authors);
//array converted into comma seprated string
$authorsall = implode(",", $result);
// return if this isn't the theme author override dropdown
if (!preg_match('/post_author_override/', $output))
return $output;
// return if we've already replaced the list (end recursion)
if (preg_match('/post_author_override_replaced/', $output))
return $output;
// replacement call to wp_dropdown_users
$output = wp_dropdown_users(array(
'echo' => 0,
'name' => 'post_author_override_replaced',
'selected' => empty($post->ID) ? $user_ID : $post->post_author,
'include_selected' => true,
'include' => $authorsall
));
// put the original name back
$output = preg_replace('/post_author_override_replaced/', 'post_author_override', $output);
return $output;
}
add_filter('wp_dropdown_users', 'wpapi_override_wp_dropdown_users');
/*
* Find User IDs by Role
*/
function getUsersWithRole($role) {
$wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
return $wp_user_search->get_results();
}
Using above code, you can load multiple role users in author drop down.
In this article, wordpress tutorial, how to display post count of all wordpress tags. In wordpress post we use post tags. For SEO purpose we can show post tags.
how to display post count of all wordpress tags
If we want to show the post count related to wordpress tags.
You can use following code:
<?php
//list all tags that are assigned to posts
$taxonomy = 'post_tag';
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
if ($term->count > 0) {
echo '<p>' . '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
}
}
}
?>
WordPress tutorial for, How to get page id by slug wordpress theme. First find the page id using page title or page page slug. we given sample code here.