Add a parent to posts in wordpress - wordpress

i'm setting up a premate theme but it has a weird template for the blog main page.
i found a solution to remove the blog page from "reading" section in wordpress but i want now to add a parent "/blog/" to all my posts.
Is there any solution for this?

Not having hierarchy is sort of the defining feature of 'post' vs 'page'. They are the same object with different features toggled – features like 'hierarchy'.
The hierarchy functionality only works among the same 'post_type', so all 'page' 'post_type' can be related to one another, but not to 'post' 'post_type', or any other custom post type. (Confusing because 'post' is a 'post_type'.)
All this to say there is not a clean solution for this.
Perhaps there is another way to accomplish your goal, like WP_Query().

Related

Singular/Plural URL Structure in WordPress for Post Types

I am working on a client's website, and he has asked me a very genuine question, and I am unable to figure it out.
He is Creating a Website for Piano Chords, where he will write tutorials about chords in general and each individual chords too. And he wants them to be of a similar post type.
So I registered a CPT Chords with the slug chords using the documentation on Codex.
Now the problem is the site has multiple posts about chords. And they will be of 2 kinds.
One will be generic to all chords, hence the word needed in the slug is chords and not the singular form.
e.g. -> https://mypianonotes.com/chords/overview/
This is fine. Now problem is when we need content like "C Major Chord". This is supposed to be singular. So the slug/url should be chord/c-major/ and not chords/c-major/.
Is there any solution to this problem? Or will I have to create 2 separate post types?
Register the post type as 'my_chords' then add a rewrite rule parameter in the arguments like this
<?php
$args = array(
'rewrite' => array('with_front'=>false, 'slug'=>'chord'),
);
register_post_type( 'my_chords', $args );
?>
Then create a page for 'chords' with a child page of 'overview'.
Hope this helps :)

Page Template for every custom post type

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.

How to make your own post format in Wordpress?

How can I create my own custom post formats?
Or how can make my custom post type make work with a function like
get_post_format();
For example i have a custom-post type with the type of "accordion" and i like to be able to use it with as content element in the loop, but only if it exists...
get_template_part( 'content', get_post_format() );
So i am looking for a function like
get_custom_post_format();
which does not exists in Wordpress.
Anybody tried something similar?
I'm not sure if you're asking how to create custom post formats or custom post types so I've provided the answer to both.
If you're asking whether you can create custom post formats...
...then the answer is no. See the quote below from Post Formats on the WordPress codex:
The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature. Themes are not required to support every format on the list. New formats cannot be introduced by themes or even plugins. The standardization of this list provides both compatibility between numerous themes and an avenue for external blogging tools to access this feature in a consistent fashion.
If you're asking how to create a custom post type:
The most basic example of creating (registering) your own custom post type is to add the following code to your functions.php file inside your theme.
function register_recipes_post_type() {
$args = array( 'public' => true, 'label' => 'Recipes' );
register_post_type( 'recipe', $args );
}
add_action( 'init', 'register_recipes_post_type' );
The above code hooks our register_recipes_post_type function to be executed when the init action is triggered by WordPress core.
Once you've added this code, if you go to your wp-admin you'll see a new menu on the left called 'Recipes', that's your new custom post type. If you add a new recipe, give it a title and some content, publish it and then try to preview it, you'll notice that you get a 404 error. After creating a new custom post type you need to go to your Settings > Permalinks in your wp-admin, just visiting that page will fix your permalinks to include the new custom post type so if you now go back and refresh the preview of the recipe you just created you'll see that it now works rather than 404s.
Now if you create a new file called single-recipe.php and put some code inside it, just put 'test' now for the purpose of seeing that it works and once you have, refresh the preview of the recipe you just created once again and you should see that it just displays the word 'test'. Using that file you can create a completely custom template to be displayed for showing single entries (posts) of that custom post type, or you could use content-recipe.php if your single.php includes a get_template_part( 'content', get_post_format() ); as you said in your original post.
Obviously your custom post type probably won't be for recipes but just update instances of recipe and recipes to whatever you want it to be.
There are also other post type specific templates you can create too for your archive of the post type etc. The above should be enough to get you started though.
There are also other arguments you can pass when registering your post type, you can see the full list here: http://codex.wordpress.org/Function_Reference/register_post_type
I hope this helps. Good luck! =)
Creating New post format is not allowed currently by WordPress. you can’t define any post format apart from what WordPress allows.
Reference:
1. http://wp.tutsplus.com/tutorials/proof-using-post-formats/

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.

In WordPress how do you register built-in taxonomies with custom post types in code?

The WordPress codex has a lot of examples of how to register custom taxonomies with custom post types, but I couldn't find much about using built-in taxonomies (tags & categories) with cpts.
I have a cpt called listings, and I need to add the standard category and tag UI elements to the listing cpt page. I also need to do this with code in my functions.php, rather than using a plugin.
Not a problem at all. When you register the post type, just add this argument to the array:
'taxonomies' => array( 'category', 'post_tag' )
Suppose you defined your cpt (custom post type) by the following:
register_post_type('listings', $args); // where $args is an array of your cpt settings
Then you could use the following to add taxonomy:
// category-like:
register_taxonomy('listing_category', array('listings'), array('hierarchical' => true, ...));
// tag-like:
register_taxonomy('listing_tag', array('listings'), array('hierarchical' => false, ...);
In fact, I personally put those custom type definitions in my own plugin (not open for public as it provide my own site functionalities, which obviously not suit the others at all).
The problem of putting in functions.php increases the difficulty to change to a new theme (although changing theme is not so often, but for self-owned blog, it do happen in some day).
Moreover, the custom post types should be site-wide, not depending on the current theme. So semantically it should not be in the theme's directory.

Resources