in my custom post type (documents), I want to change the default page title (not the meta title) to a custom title. I tried several posts about it but it never works. Here'S what I have so far:
add_filter( 'post_type_archive_title', function( $title ) {
if ( is_category( 'documents' ) ) {
$title = 'Bibiliothèque virtuelle';
}
return $title;
}, 50 );
But I still see the default title
Any help would be greatly appreciated!
Related
I created a Custom Post Type, and it has some categories in it.
I created forms with User Frontend Pro for CPT categories.
I’m looking for a solution for assigning the custom templates to that records to show them on the website.
For example;
Assume that I have a CPT named Project.
The project post type has two categories; “Business” and “Ideas”
Users can post their entries with forms to these categories and their posts listed under the account dashboard.
The issue is that;
Business categories should be shown with the category-business.php custom template, and in a similar way, the Ideas category should be shown with category-ideas.php.
How can I add custom template to CPT categories?
P.S. I tried the template hierarchy solution that is in the WP documents, but it's not working. Because there is a custom view function for content. I looking for a code snippet that adds the template page attributes to custom posts automatically, if it is possible.
Assuming that you are just using the default WordPress category you can the pre_get_posts Hook to include the project as part of the category page if they are not showing by default
function include_project_to_cat_pages( $query ) {
if ( $query->is_category() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'project' ) );
}
}
add_action( 'pre_get_posts', 'include_project_to_cat_pages' );
I solved that issue with a filter.
add_filter( 'single_template', function( $template ) {
global $post;
$cat = get_the_category()[0];
if ( $post->post_type === 'project' && $cat->slug === 'business') {
$locate_template = locate_template( "custom-business-template.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
Thank you to all contributors and the community.
I'm trying to create specific templates based on custom taxonomy subcategory slugs.
Custom taxonomy is registered with CPT UI plugin.
An example explaining the problem:
Registered custom taxonomy: Fruits
Fruits has a category: Apples
Apples has a subcategories with slugs: red-apple, green-apple, yellow-apple
My goal is to use specific template for each subcategory: red-apple, green-apple and yellow-apple
What I have tried so far:
I created a files such as taxonomy-fruits-red-apple.php, taxonomy-apples-red-apple.php etc - none worked.
Used the following code:
function wpd_subcategory_template( $template ) {
$cat = get_queried_object();
if ( isset( $cat ) && $cat->category_parent ) {
$template = locate_template( 'my-template.php' );
}
return $template;
}
add_filter( 'category_template', 'wpd_subcategory_template' );
None of these methods worked.
Any ideas?
Thx in advance!
I am creating my own plugin in Wordpress. With register_post_type I can view my own posts. I also created a post status with register_post_status. When I go to the summary page I can see all posts and filter on the status.
The "problem": When I go to the summary page, I can see all posts and the filters. The "All" status is always selected on default, but I want to select my custom status on default. Is this possible? Or change the URL in the menu to post_status=&post_type= ? I am talking about the admin side.
Hope someone can help me, because I can't figure it out.
I have fixed it with this code - it changes the url in the menu:
add_action( 'admin_menu', 'wpse_admin_menu', 100 );
function wpse_admin_menu()
{
global $menu, $submenu;
$parent = 'parent';
if( !isset($submenu[$parent]) )
return;
foreach( $submenu[$parent] as $k => $d ){
if( $d[0] == 'name' )
{
$submenu[$parent][$k][2] = 'edit.php?post_status=status&post_type=type';
break;
}
}
}
If the post title is "concepts"
I want the post content to be something like that
"in "post title" we care about our client satisfaction by following "post title" high quality policy"
so i want the post content include "concepts" and replace it with post title
I think wordpress shortcode api can do something like that. But i don't know how ?
I don't fully understand your statement. But I think wordpress hooks can help you do that
You can try someting like:
function change_post_title ( $post_title ) {
if ( $post_title == 'conecpts' ) {
$title = 'The new title';
return $title;
}
return $post_title;
}
add_filter('the_title', 'change_post_title');
This will change the post title to "The new post title" if the title is "concepts"
Please make your question clear as to what is the problem and what you want to accomplish.
In your post content you will need add a shortcode where you want the title printed, int eh shortcode example below you will use [post_title].
function post_title_shortcode( $atts ){
extract( shortcode_atts( array(
'post_id' => get_the_ID()
), $atts ) );
return get_the_title( $post_id );
}
add_shortcode( 'post_title', 'post_title_shortcode' );
With that shortcode you can pass the post id so that if you want to get the title of another post you could do use the shortcode [bartag post_id="123"] where 123 is the post id of the title you want to show.
I have a made a custom post type called "Member Resources" the posts under this CPT have a few taxonomies such as categories and tags.
Tags = "Diversity" Categories = "Guidance"
When I go to the following urls:
www.domain.com/tags/diversity
www.domain.com/tags/guidance
No posts appear.
Though I have set public => true on the CPT function.
Posts are displaying if you go to the Member Resources archive page though, so they are displaying, but not when you filter them by taxonomies.
Update -
Adding the following code to my functions.php file allows the member-resources CPT to show in Category and Tags pages respectively, but now in the wordpress backend under the "Pages" tab and all other content tabs such as posts etc it seems to have overrided my pages and posts and is showing just the member-resources posts.
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
function add_my_custom_post_type( $query ) {
if ($query->is_main_query())
$query->set( 'post_type', array( 'member-resources' ) );
return $query;
}
your code looks correct. but you are including the CPT member-resources in too many of wordpress's queries. the is_main_query means "the loop" i think.
so you need to restrict this to just running when on a tag archive page.
the following code is from the wordpress site
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}
You need to do a similar thing but determine if you are in a "tags" page.