Custom post type display - php

I work on a website with a custom post type, described in the functions.php file of a child theme.
I created a function, still within the functions file, that describes the beahavior of a page for this custom content : basically, to achieve that, I use get_the_terms() and get_post_meta(), within a function named add_thecontent(). It's called with a filter : add_filter('the_content','add_thecontent');
It works fine : I can retrieve the name, the custom taxonomies, the several fields...etc.
I also work on another website, still with a custom post type (a different one), with a different theme. I use the same mechanism as stated above (child theme + functions file + content called within a filter) but when I want to display a preview of my custom post, the engine calls the file content-single.php of the main theme. This file calls the_category() and that leads to warnings in my page, beacuse I don't have a "regular" category for my custom post, only custom taxonomies.
I tried to put a content-single.php in my child theme in order to redefine its beahavior, but I get errors. And I think it's not a proper way to proceed.
What I want is to force WP engine to display a custom post as I defined in the functions.php. How can I achieve that ? Thanks and sorry for my english

I would recommend you to install and debug using Which Template Plugin once you install this, you can know which file is being called to parse your custom post.
Also, take a look at the worpdress hierarchy page here
If you created a custom post, the you can create an individual page for that custom post as single-$posttype.php and you would be able to control the flow of the file.
Good luck!

Solved. In the parent theme, content-single.php included the following code :
<span class="cat-name"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span>
So I created a content-single.php in my child theme, and I made a test on the type of post. If the post is not a custom post I created, display normally its category. Else, do nothing, because I handle the taxonomies myself.
<?php if($post->post_type != 'myCustomPost') {;?>
<span class="cat-name"><?php $category = get_the_category(); echo $category[0]->cat_name; ?></span>
<?php } ?>

Related

Archive.php Category.php or Taxonomy.php - Which is correct for my situation?

I'm using Genesis framework and I have this page (http://staging.seedcreativeacademy.co.uk/short-courses/) showing the categories of my custom post type short_courses. I have changed the category name to course_type, by creating a new custom taxonomy.
This is how I want it to work so far (styling needs sorting out admittedly!) Im also using CPT UI plugin.
Now, when I click through to a category, is displays each 'Course in a nice masonry block as you will see here: http://staging.seedcreativeacademy.co.uk/course_type/digital-marketing/
However, I dont want this pages to look like this and I've tried adding custom template for the following:
Archive-short_courses.php & taxonomy-short_courses.php
Archive-course_type.php & taxonomy-course_type.php
But it doesnt seem to alter the layout at all...
Once I pass this hurdle I will want to alter the single.php page for these short courses, but I thought I would start with this first.
Im not sure if genesis blocks this and sets a site wide default? I know if sets a site wide default for the archive settings but I cant find anything about a template, plus i dont know if I shoujld be searching for tutorials on archive.php pages, category.php pager or taxonomy.php pages...
Can someone help me clarify things please?
course_type is a term name, not taxonomy name.
So, these are correct for your case:
category-course_type.php (category-{slug}.php is correct format. So check if course_type is correct slug of that category)
single-short_courses.php
Just in case, try reload permalinks via Settings->permalinks->save after making these changes.
Looks like your theme or some plugin is adding masnory class to body tag, which then is styled by your child theme. You need to filter that class out of your body tag and then might styling goes to non-masonary styling.
Add following code to your taxonomy-course_type.php file, and also make sure you have genesis(); call as the last thing in the template.
add_filter('body_class', 'remove_body_class', 20, 2);
function remove_body_class($wp_classes)
{
foreach($wp_classes as $key => $value)
{
if ($value == 'masonry') unset($wp_classes[$key]);
}
return $wp_classes;
}
Above could should be in custom taxonomy template, which also have genesis(); as last line.

WooCommerce: Single-product-postslug template not working?

First,I know wordpress won't allow you to use template for posts (WooCommerce's product page is built via post). So I look for the Template Hierarchy.
It says there:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post.
For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.
So I created a template and name it single-product-ccc (ccc is one of my product's slug name), but nothing happened, nothing was affected.
But by creating a template named single-product will affect all of the product pages.
Why is that happening?
I don't get it. Even a single-2313.php (2313 is one post's id) will overwrite the default single.php for that 2313 post.
Why single-product-slug is not working in the same way?
Thanks.
Let me first correct you at one point. You say: "WooCommerce's product page is built via post".
This is not correct. WooCommerce creates a new post type 'product'. You can see all custom posts types that WooCommerce creates here: https://docs.woocommerce.com/document/installed-taxonomies-post-types/
The 'product' post type uses the template single-product.php.
As you say, WordPress Template Hierarchy Docs say that you can create a template for a particular product:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
WooCommerce is a WordPress plugin and as so, it doesn't always follow WordPress rules strictly, and this is one of those cases. But you can use a filter to correct this very easily. In your theme's functions.php simply add:
add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
function custom_single_product_template_include( $template ) {
if ( is_singular('product') && (get_the_ID()==30)) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-30.php';
}
return $template;
}
where get_the_ID() is the id of your Product. Now, you can create a new template as for example single-product-30.php
Try adding this in functions.php
function your_theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'your_theme_add_woocommerce_support' );
and now onwards, you can use woocommerce/single-product.php for customisation

Read POST data in Wordpress page

I am trying to write a plugin for wordpress. I have a problem i can't solve.
Inside plugin i habe added a shortcode that show a form.
function showForm() {
echo '<form method="post" action="www.example.com/myDestinationPage">';
[...]
}
add_shortcode( 'ShowFormSC' , 'showForm' );
After that, in a page, i added the shortcode and it works perfectly. ;-)
Now the problem: how can i read POST data in myDestinationPage (another wordpress page)?
In Php would be very simple ... but in wordpress I do not know how to do.
Second problem: myDestinationPage must be a real wordpress page with another shortcode inside, or can be defined as a "virtual" page inside my plugin?
Thank you for your help!
Best Regards,
Simone
www.example.com/myDestinationPage needs to be edited to recieve the post data, just as in any other php script, wordpress or not. If 'myDestinationPage' resolves to dynamic wordpress content, then you are in muddy waters.
Let's say, myDestinationPage is a wordpress Post. That "page" doesn't exist as a file, it comes directly from the database.
You could write a shortcode which handles this though:
add_shortcode('post_parser', 'postParser');
. . .
function postParser() {
filter_input(INPUT_POST, 'my_post_value');
//do something
}
Then, you just add the '[post_parser]' shortcode to the myDestinatioPage Post. (You mentioned it's a wordpress page, but page & post are both WP_Post objects.)
Another option is to put your post processing code in the post.php (or whichever template myDestinationPage is).
1st answer: you can directly use $_POST in wordpress like in php.
2nd answer: Yes you can. If you want to use pages in your plugin, use plugins_url() to generate the path for form action.
https://codex.wordpress.org/Function_Reference/plugins_url

Show full post (not excerpt) for defined posts in wordpress

I'd like to have the possibility to define some posts in wordpress where the full post is shown and not an excerpt.
There are various solutions like:
Use a hidden html to declare this and code into the theme to either use the_content or the_excerpt
Hardcode into the theme (if postid == xx then the_content else the_excerpt)
Use post meta data and add "if" into theme to check for them
Create a plugin which adds the functionality automatically and also a checkbox "Always show full" into the post-editor.
The first one is easy but ugly, 2nd one should be doable with some googling but the 3rd one seems to be the most appealing to me but I have no idea how to achieve this.
Since usually in the template all i have is somewhere the_excerpt method from wordpress. I therefore should somehow inject some code there and check if the checkbox for the current post is set and then just use the_content instead. Is this possible at all or do I need to modify the theme anyway?
Thanks for your inputs.
I would use custom fields, it's more flexible, so you don't need to hard code the page ids.
<?php
$show_full_content = get_post_meta($post->ID, 'show_full_content', true);
if ($show_full_content == 'yes') {
the_content();
} else {
the_excerpt();
}
?>
You might be interested in ACF, it can make it more user friendly.

Conditionally display menu for page type in Wordpress

I am relatively unfamiliar with Wordpress and I am creating a custom theme for a client. I would like to either display or remove the main menu depending on the page type. I have researched several options like removing the navigation from header.php and referencing it separately and also making the menu conditional which is preferable.
I have a custom page type in my theme called 'landing page' on which I would like the menu to be never be displayed, though it will be on every other page. Ultimately there will be a lot of these and I would rather I didn't have to intervene.
I would rather not duplicate my header.php file but I can only find reference to displaying the menu conditionally like below by page name or ID which seems ridiculous.
<?php
if (is_page('contact')){
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
elseif (is_page('gallery')){
<?php include(TEMPLATEPATH.'/headerB.php'); ?>
}
else {
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
?>
Rather than including files as above, I will put the whole thing into my header and just make the navigation conditional. Does anyone know how I should approach this using my custom page type landing page rather than by page name so every page created with that type will never have a menu?
Thanks
Are you talking about a Custom Post Type (CPT) or a page called landing-page?
They are completely different. See http://codex.wordpress.org/Post_Types
In any event, this will work for a custom post type or a page:
if ( !is_singular( 'custom-post-type-name-or-page-slug-here' ) ) {
get_template_part('menu');
}
It says: "If this page is not a single page or a CPT, load the file menu.php from the theme folder."
See also http://codex.wordpress.org/Include_Tags:
The get_template_part() tag includes the file {slug}.php or
{slug}-{name}.php from your current theme's directory, a custom
Include Tags other than header, sidebar, footer.

Categories