-
Author
Tagged: cpt, custom post type, templates
-
November 2, 2014 at 19:47 #33797sharmstrModerator
@abe: Short question – Is there a hook to inject information between article-media and article-content?
If not, then Long question –
I’m looking for advice on handling custom post types.
Basically I have a few custom post types. From a templates standpoint, the only real difference between my cpt and posts are that I need to inject some information between the article-media and article-content divs (or just at the top of the article-content div).
When I first did this, I created copies of all the content-* files and added a check for the cpt, then loaded my own template part if my cpt was found. Sorta like this…
COPY CODEif (get_post_type() == 'mycpt') { get_template_part('mycpt/mycpt-templatefile'); } else { the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'kleo_framework' ) ); }
That was all fine and good when I had one cpt, but now I have a few and I’d like to streamline this a bit. So instead of adding more conditions to load the correct template (remember I’d have to add this to all of the content-* files), I’d rather have a hook within those files and only have to update my functions.php file to add more conditions.
Something like in each content-* file just above the_content__
COPY CODEdo_action( 'kleo_before_article_content' );
Then in my functions file I can do something like
COPY CODEfunction add_cpt_info() { if (get_post_type() == 'mystories') { get_template_part('stories/story'); } else if (get_post_type() == 'mychapter') { get_template_part('stories/chapter'); } } add_action('kleo_before_article_content', 'add_cpt_info');
Is there a better way of doing this? If not, is this something you would consider adding so I dont have to maintain child versions of the content-* files or is this something only I would find useful?
(I’m willing to bet that I’ve overlooked something really simple)
This support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
November 10, 2014 at 01:11 #34846AbeKeymasterHi, I can add the do_action( ‘kleo_before_article_content’ ); just before the content so you can easily add that there.
Another way you could do it is to hook at the the_content filter and prepend your content to the existing content like:COPY CODEfunction add_cpt_info($content) { if (get_post_type() == 'mystories') { $content = "content before here " . $content; } else if (get_post_type() == 'mychapter') { $content = "content before here " . $content; } return $content; } add_filter('the_content', 'add_cpt_info');
but the first one is more logic so I will update theme files and send them to you later today.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution---
@ SeventhQueen we do our best to have super happy customers. Thanks for being our customer.November 10, 2014 at 02:22 #34852sharmstrModeratorooo. I didnt think about hooking into the content. Man, I have so much to learn still. That might be better abe. Then you dont have to alter kleo if its something only a few will use.
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
November 10, 2014 at 20:22 #34924AbeKeymasterif that works for you then I won’t alter it. Let me know if you prefer the action hook
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution---
@ SeventhQueen we do our best to have super happy customers. Thanks for being our customer.November 10, 2014 at 20:42 #34927sharmstrModeratorDont alter it. I’ll take care of it on my end. Thank you though 😀
Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solutionThis support site is not about custom work. If you need custom development please contact cornel@seventhqueen.com
-
AuthorPosts
The forum ‘KLEO’ is closed to new topics and replies.