How to create two different single.php for same post type? - wordpress

I have a post type named 'Property'.
I want to show single post in two different way.
if anyone click on post then it will shows a simple layout with name of post and description.
Now i have also category for beds. now if anyone goes in category '2 Bed' then you can see all post with '2 Bed' categories('its done'). but now if anybody click on post then it have to show different single page.
my English is very bad so please excuse it.

You can set up individual templates for a single category by using the single_template hook.
Put this in your functions.php file:
function my_category_templates($single_template) {
global $post;
if ( in_category( 'property' )) {
$single_template = dirname( __FILE__ ) . '/single-property.php';
}
// Copy the above for your other categories
return $single_template;
}
add_filter( "single_template", "my_category_templates" );
You can then create individual single templates for each category, just add more conditions and point them to the template you create.

There is the concept of Category Templates as dictated by the
Template Hierarchy but because you are asking how to display a "single" post based on category, you will want to use the in_category() Conditional Tag in the Template file you use to display singe posts. The Loop article has an example of using in_category.
Or look at this concept:
http://www.nathanrice.net/blog/wordpress-single-post-templates/
Or this:
http://justintadlock.com/archives/2008/12/06/creating-single-post-templates-in-wordpress
Or this plugin:
http://wordpress.org/extend/plugins/custom-post-template/
Or this plugin:
http://guff.szub.net/2005/07/21/post-templates-by-category/

So according to the Wordpress template hierarchy there is only one single.php, that cannot be separated by category (like the archive page for example.)
https://developer.wordpress.org/themes/basics/template-hierarchy/
So in this case I suggest you read the current category id in your single.php file and then adjust the content to your needs. You can use get_the_category() do do this (reference: https://developer.wordpress.org/reference/functions/get_the_category/) which will return you an array with categories. In my simple example I just pick the first category to do something:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
if($category_id == 1) echo 'do something here';

Thanks for you help. I am done with it myself.
I create single.php for simple format and and in category result i didn't use the_permalink and call id of post and made a url like http://localhost/demo/page?id=$id

Related

Make Custom Post Type with custom field inherit category's custom field when empty

been struggling finding a solution to my problem for weeks.
Case :
I have a custom post type named : design. This CPT have a custom field (made with ACF plugin) called thematique. I created the same custom field (thematique) for design's categories.
Expected behaviour:
I want that whenever if we make a get_posts() or WP_Query if a design's thematique field is empty, it should inherit its categorie's thematique.
I've investigated into the pre_get_posts hook but I'm not quite sure how to handle it.
Anybody has an idea ?
Thanks in advance, I really appreciate your help !
You can just do this the easy way and inside your WP Query where you have the formatting for each returned item add this:
<?php $thematique = get_field('thematique'); //Gets current posts value of fiels
<?php if (empty($thematique)){ //Checks if the field is empty, if so do the following
$postCat = get_the_category(); //Get current posts category ID
$catID = 'category_' . $postCat; //Merge category ID and needed string for ACF
$thematique = get_field('thematique', $catID); //Updated the value of $thematique with categories value
}?>
Although not tested this should indeed work as it's how ACF says to get the value from categories. Find out more here.
#Ali_k
I'm not so sure about how to go about it though. I would need something like :
// Designs Thematique priority mechanic
function design_thematique_priority($query){
if($query->query['post_type'] == "design"){
foreach($query->posts as $post){
if($post->thematique == ""){
$post->thematique = $post->category->thematique;
}
}
}
}
add_filter( 'pre_get_posts', 'design_thematique_priority' );
But I don't think there is any loop I can use to loop through posts in pre_get_posts right ?

Custom Columns Don't Display Anything

I have added a new column to a custom post type to display the post's ID. This works for the WordPress core post type, but not for my custom post type. I have tried using the manage_{post_type}_custom_column hook and just applying it to all posts, but neither works.
It DOES add the custom column headers, but I can't populate them with anything at all when viewing the custom post type.
This is what it looks like when viewing the custom post type
and
this is what it looks like when viewing a regular core post.
// Add post ID column to use an order ID in all posts view.
add_filter('manage_posts_columns', 'oms_order_id_header');
add_action('manage_posts_custom_column', 'oms_order_id_column', 10, 2);
function oms_order_id_header($columns) {
//Remove title column
//unset($columns['title']);
//Add new columns
$columns['order_id_header'] = 'Order ID';
$columns['customer_header'] = 'Customer';
$columns['author'] = 'Owner';
return $columns;
}
function oms_order_id_column( $column, $post_id ) {
if ($column == 'order_id_header') {
echo $post_id;
}
}
This turned out to be an issue with the post type being set to hierarchical. Hierarchical post types need to be targeted by a different action hook than the one used here.
Instead of manage_posts_custom_column, hierarchical post types need to use manage_pages_custom_column.
I just tried you code and it seems to be working perfect on my WordPress installation in both cases: Custom Post and Post.
Maybe your error is happening because your posts are drafts? I don't think so but maybe. (I tried your code also with drafts on my installation and it worked). Here is the screenshot:
Try printing "hello world" instead of the $post_id to check if it prints in all cases.

Add specific words to WordPress custom type slug

I have a WordPress installation with a custom type (places) wich produces this permalink structure:
http://example.com/places/the-first-site/
And I would like to show all my sites like this:
http://example.com/places/visit-the-first-site-and-enjoy/
where 'visit' and 'and-enjoy' would be always those specific words (constants).
Even better I would like to put a custom taxonomy I have (year) as a metadata
places/visit-the-first-site-and-enjoy-1985/
I can access the DB and modify the post name of all post, but I would have to do for the new post also, and I'm looking for some automated rule, but can't find how to do.
Maybe some rewrite rule, but I don't know how to do it.
Any ideas??
You do not have to edit anything in your database nor any codes in your WordPress installation; you can achieve that directly from your WordPress administration panel.
From your Dashboard, click on the Permalink sub-menu under Settings; there, you will have to select the Custom Structure option and set it as follow in order to achieve your desired effect:
/places/visit-%postname%-and-enjoy-%year%/
Please note: here, we made use of both %postname% and %year% Structure Tags so as to get names of posts with their corresponding year of publication respectively.
Don't forget to click on the Save Changes button on the page in order to effect your changes.
... Read more on Permaklinks (for general knowledge).
For a custom post type and taxonomies as expressed further in comments, you will need a custom solution which will require a little bit of coding and or tweaking, depending on your abilities; you may use this handy plugin (Custom Post Type Permalinks) from the WordPress.org Plugins repository.
This posts should equally be of great help to you, to getting started and understanding further, should you chose to code.
You have différent hook like save_post or pending_to_publish, where you can set or change the "post_name" (that's corresponds to the permalink slug).
To Add specific words to WordPress custom type slug You have to Register custom rewrite rules.
suppose this is Your URL http://example.com/places/the-first-site !
and you have a post type places.
function add_rewrite_rules()
{
$wp_rewrite->add_rewrite_tag('%places%', '([^/]+)', 'places=');
$wp_rewrite->add_rewrite_tag('%state%', '([^/]+)', 'state=');
$wp_rewrite->add_permastruct('places', 'places/visit-%state%-and-enjoy/', false);
}
function permalinks($permalink, $post, $leavename)
{
$no_data = 'no-data';
$post_id = $post->ID;
if($post->post_type != 'story' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
return $permalink;
$state = get_post_meta($post_id, 'location', true);
if(!$state)
$state = $no_data;
$permalink = str_replace('%state%', $state, $permalink);
return $permalink;
}
add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'permalinks', 10, 3);
put this code in your custom post type plugin. and if get page not found error please change your permalink setting. or make a custom template file to show this post.

Woocommerce: How to remove "Archive" in title?

I'm trying to remove the word "Archive" from the page title.
How to remove it? By adding a filter?
Example:
My collections Archive | My sitename
Best to use Wordpress SEO by Yoast.
Within Titles & Metas go to the Taxonomies tab and update the Title template for Product Categories to remove the word Archive. Mine ended up looking like this:
%%term_title%% %%page%% %%sep%% %%sitename%%
you can rewrite the filter in functions.php so it fails, causing WooCommerce not the render it.
function override_page_title() {
return false;
}
add_filter('woocommerce_show_page_title', 'override_page_title');
If you are using the Yoast SEO (Versión 8.1.2), the options were move to:
Yoast SEO -> Search Appearance -> Taxonomies.
In there look up by Product categories (product_cat) and open it.
Then remove the Archives string from the head.
I know this is an old post but I tried the answers here and it didn't work.
I found the solution basing it off renegadesk's answer, but in addition of going to the Taxonomies Tab I also had to change it in the Post Types tab. See below:
Get Wordpress SEO by Yoast if you don't already have it, then click SEO > Titles & Metas > Post Types. Scroll down and look for the heading "Custom Post Type Archives". On the "Products", you can edit the content of this title. Mine now looks like this:
%%pt_plural%% %%page%% %%sep%% %%sitename%%
In woocommerce there seem to be 3 instances of Archives (case sensitive) one in
\wp-content\plugins\woocommerce\woocommerce-hooks.php around line 50
and 2 in \wp-content\plugins\woocommerce\woocommerce-template.php
around lines 240 ish
Perhaps one of those is what you're looking for and you may be able to modify code.
in addition to Ted C:
For this purpose Wordpress has already predifined functions starting with __return_.
In this case the function you are looking for is __return_false.
add_filter('woocommerce_show_page_title', '__return_false',10,0); will also work.
The last two params are the order of the function and the number of params passed to it.
It is always a good pratice to set them up but this is only necessary if you want to modify the default order (10) and the number of params passed (1).
You have this filter for all cases : "get_the_archive_title".
But then there are different cases.
For exemple for simple category you can do :
function prefix_category_title( $title ) {
if(is_category()){
$title = single_cat_title( '', false );
}
return $title;
}
add_filter( 'get_the_archive_title', 'prefix_category_title' );
For custom post type you should do :
function prefix_category_title( $title ) {
if(is_archive('slug-of-your-custom-post-type')){
$title = single_cat_title( '', false );
}
return $title;
}
add_filter( 'get_the_archive_title', 'prefix_category_title' );
So finally you have many condition like :
is_category
is_archive
is_tag
is_author
is_year
is_tax
https://developer.wordpress.org/reference/functions/get_the_archive_title/
Vincent.
wp-content/plugins/woocommerce/templates/archive-product.php
Edit this file archive-product.php and remove the below 3 lines.
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
This will remove page title on shop page and all other pages.

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

Resources