Page Template for every custom post type - wordpress

I'm just new in wordpress, I just wanted to ask if it's a bad practice if I'll create page template for every custom post types?
I mean if I have CPT [custom post type] with different content, like my first CPT has images, my 2nd cpt has image and text, my last CPT, has slider, text and description.
Because I wanted to create page template for every type of CPT's I have. Is it a bad practice? Or are there efficient and effective ways to do such things?
Your answers are highly appreciated. Thanks!

It's better not to do that, not only because it's a server load, but also because you'll get crazy if you have an issue. Instead, use conditionals in the same page. You can read about it at WP Codex: Conditional Tags : Taxonomies and then pay attention to the is_tax and has_term tags.
This way, you can use is_tag or has_term depending on your approach, like this
if( has_term( 'jazz', 'genre' ) ) {
// do something
}

You can use taxonomy to deal with such problem. Use taxonomy to filter contents according to your requirements. Use register_taxonomy() to register a taxonomy for CPT.

Related

Tags create/edit/delete hooks in wordpress

I need hooks if tags have removed, created and edited. I have not found them in the WordPress Codex / Function Reference.
For categories:
edit_category
create_category
delete_category
Has anyone encountered this problem?
To explicitly target tags, you'll need create_$taxonomy, edit_$taxonomy and delete_$taxonomy where $taxonomy is post_tag (ie create_post_tag, edit_post_tag and delete_post_tag). They're mentioned in passing in (eg) the wp_insert_term.
Tags are a taxonomy, so the generic actions create_term, edit_term, and delete_term would work as well, though they'll also fire for other taxonomies (such as categories).
You need to use dynamic hooks to taxonomies:
edit_post_tag
create_post_tag
delete_post_tag

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

Is there way to check if a post is attached to a particular taxonomy?

I want to create a condition whereby if a post is attached to a particular taxonomy then some additional code is executed on a modified single.php but I can't find a way to directly check for the taxonomy.
I thought "is_tax" might do the job but it seems that this tag is only effective for archive pages, which I assume means a page that is generated from a template using the taxonomy name.
Is there a direct way to read the taxonomy from a single post so that I can do something similar to:
if (is_tax('chapter')) {additional code}
Thanks
I worked out one solution for this as follows:
$terms = get_the_terms( $post->ID , 'chapter');
if($terms) { *additional code to be added to the template* }
If any terms in the taxonomy 'chapter' are returned the additional code is added to the template otherwise the additional code is omitted.
I don't know if this needs any additional error checking but the code as shown works by including code on the appropriate posts and omitting it where its not required.
Any suggestions on improvements would be welcome.

Displaying a single term of a custom taxonomy in Wordpress

How am I able to show a single term of a custom taxonomy in Wordpress?
I have a custom taxonomy, which will always have only one term in it, and I want to display it in a custom loop on my home page. I have tried all possible ways, with one (blargh, can't remember which one) I managed to display the term but as a link, which I don't want. I need it as regular text, since I want to display it in my own href, which will link to the place where I want to.
Any ideas anyone? All and any help very much appreciated. This issue is slowing down my development of a custom theme. :(
Get the single term object using get_term($term_ID, $taxonomy), or alternatively get_term_by('slug', $term_slug, $taxonomy).
The term object will have, among other properties;
$term->name; // the name of the term!
$term->slug;
$term->term_id;
$term->count; // number of objects using the term

Resources