I am new to wordpress so sorry for the basic question. How do I have a case studies page, but link 3 case studies to display a shortened version onto my homepage.
Any ideas?
Thanks!
Scott
I would start by creating a Custom Post Type called case studies. Add the following to your theme's (or child theme) function.php.
// Create Custom Post Type
function my_custom_posttype(){
register_post_type('case studies',
// Options
array(
'labels' => array(
'name' => __('Case Studies'),
'singular_name' => __('Case Study')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'case-studies'),
)
);
}
add_action( 'init', 'my_custom_posttype' );
This will add a new link in the admin to add new case studies, just like posts and pages. This is a very basic example, check the link above for more information on post types.
Adding them into the homepage will depend on what theme you are using.
Related
I made a Wordpress site to review books and I think I am in some kind of trouble.
The thing is that I created a custom post type (authors) that is, the author of a certain book. Now I am trying to create user profile system and it looks like Wordpress already uses (author) base for its inherent users.
I tried to change the base to user, it worked but when I go to a user's front end like e.g: http://localhost/wordpress/user/root/ I get redirected to the homepage.
How can I fix this problem? I want the two types to be separated and I be able to view users' profiles on my front end again.
Code
//Authors Custom Post Type
function author_custom_type() {
$args = [
'labels' => ['name' => 'Authors', 'singular_name' => 'Authors'],
'hierarchical' => false,
'public' => true,
'has_archive' => 'authors',
'menu_icon' => 'dashicons-edit',
'supports' => ['title', 'thumbnail', 'editor', 'custom-fields',],
'rewrite' => ['slug' => 'author'],
];
register_post_type('authors',
$args
);
}
add_action('init', 'author_custom_type');
//Changing site users' base from "/author" to `/user`
function wpa_82004() {
global $wp_rewrite;
$wp_rewrite->author_base = 'user';
}
add_action('init','wpa_82004');
I'm having "Tour" as CPT. In this I'm having Main page, Category page and Detail page.
Main page - https://www.exapmle.com/tour/
Category Page - https://www.exapmle.com/tour/category-page/
Detail Page - https://www.exapmle.com/tour/detail-page/
I need these as Below
Main page - https://www.exapmle.com/tour/
Category Page - https://www.exapmle.com/category-page/
Detail Page - https://www.exapmle.com/detail-page/
When registering your CPT you provided some arguments... One of the arguments is 'rewrite'. You can provide an array of options,like 'slug', 'with_front', 'pages', etc.
Maybe you can try to set the slug to be empty? Or to be '/'?
$args = [
...
'rewrite' => ['slug' => '', 'with_front' => false],
...
];
register_post_type('your-cpt-slug', $args);
I hope it works!
I have custom plugin that adds Customizer sections and options.
The sections can be seen briefly on the customizer screen but then disappear. This behavior is happening on all themes (I am using my plugin on other sites).
Maybe it might be because the setting fields are not used in the theme yet, but even if I create my own theme (for which this plugin is mainly used) and add echo get_theme_mod('setting-key') somewhere in the themes code, the sections are still being hidden by wordpress.
I have clean Wordpress install version 5.2.2, using default Twenty Nineteen theme with only jQuery updater plugin active. I have checked all JS code for potential errors and any hiding happening, but nothing on my part can be causing this.
This is how I am adding sections, in customize_register hook:
add_action('customize_register', 'setup_section');
function setup_section($wp_customize){
// Add section
$wp_customize->add_section('section_id', array(
'title' => 'Section Title',
'priority' => 160,
));
// Add settings for a field
$wp_customize->add_setting('setting_id', array(
'default' => '',
'transport' => 'refresh',
));
// Add the field into a section and assign setting id
$wp_customize->add_control('setting_id', array(
'label' => 'Option Label',
'section' => 'section_id',
'settings' => 'setting_id',
'type' => 'text',
));
}
The PHP code is working as expected, but after the page loads, all my custom sections get display: none; inline css added and the sections disappear.
Any help is appreciated.
Another reason why the custom sections kept hidden is when they don't have any controls. When a section is empty WordPress hides it by default.
Here's a full working piece of code for adding a section within Customizer with one control:
function mytheme_customize_register( $wp_customize ) {
$wp_customize->add_section('footer_settings_section', array(
'title' => 'Footer Text Section'
));
$wp_customize->add_setting('text_setting', array(
'default' => 'Default Text For Footer Section',
));
$wp_customize->add_control('text_setting', array(
'label' => 'Footer Text Here',
'section' => 'footer_settings_section',
'type' => 'textarea',
));
}
add_action( 'customize_register', 'mytheme_customize_register' );
I am building a Wordpress site for a video production company. They produce videos for adults as well as for kids and therefore I split the posts in the backend to keep a structure. So the setup is the following:
Two Custom Post Types named productions_big and productions_small
A page named Productions, placed in the main navigation with links to
For Big and For Small pages
One page listing the posts of productions_big, one listing posts of
productions_small. The page names are for-big and for-small and both are children of the Productions page
The URLs are perfectly fine for the pages as I made a parent/child relation of the relevant pages. example.com/productions leads to the right page as well as example.com/productions/for-big and example.com/productions/for-small do.
For reasons that are irrelevant to this question the templates for
the CPTs differ, but in some occasions template-big is used for productions_small and vice versa. There is no actual logic behind this and therefore the decision on which template to use is done in the backend via the Post Type Template feature introduced with Wordpress 4.7
What I want to achieve is a logical URL structure like example.com/productions/for-big/name-of-adult-production. Right now I have URLs like example.com/productions_big/name-of-adult-production as productions_big is the name of the CPT.
I am familiar with the rewrite argument you should pass when registering custom post types. I have done this in all the ways suggested at stackoverflow but it is just not working. Here is an excerpt of the code I use to register my CPTs:
add_action('init', 'all_custom_post_types');
function all_custom_post_types() {
$types = array(
array('the_type' => 'productions_big',
...
array('the_type' => 'productions_small',
...
);
foreach ($types as $type) {
$the_type = $type['the_type'];
...
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capabilities' => array(
...),
'rewrite' => array('has_archive' => false, 'slug' => 'bla', 'with_front' => false),
'has_archive' => false,
'map_meta_cap' => true,
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','editor','thumbnail', 'taxonomies'),
'taxonomies' => array( 'category' )
);
register_post_type($the_type, $args);
}
}
As you see I tried 'rewrite' => array('slug' => 'bla') because I wanted to know if it does not work because of a conflict of page names when I write 'rewrite' => array('slug' => 'productions/for-big')
But whatever value I give the slug, it always redirects me to index.php.
I thought the reason why it is not working is caused by the way I am assigning post templates (remember: as a post attribute in the backend, not a dedicated single-xxx.php file) but I couldn't find a solution anywhere. All solution-seekers posting their questions were assigning templates the good old fashioned single-name_of_cpt.php way (which is not possible for me).
Sidenote: The ultimate goal would be to make the slug "dynamic". Meaning that it changes in case the page names are altered in the backend. I might try this approach but beforehand I'll have to get rid of the basic problems described here.
Any idea is much appreciated. And thank you for reading this far.
Oh boy, seems you have to post your questions in order to find an answer yourself. After trying to setup another CPT everything worked like a charm.
That made me investigate the registration of the previous CPTs. As you can see above I tried to register my CPTs inside of one function
function all_custom_post_types() {
$types = array(
array('the_type' => 'productions_big', ...
array('the_type' => 'productions_small', ...
);
foreach ($types as $type) {
$args = array(...
'rewrite' => array('slug' => 'bla'),...
);
register_post_type($the_type, $args);
}
}
So ultimately there was no conflict between the slugs of my pages and custom post types but a conflict of giving one and the same slug to two different post types.
I have custom post type like this
add_action( 'init', 'create_post_type_feedback' );
function create_post_type_feedback() {
register_post_type( 'testimonial',
array(
'labels' => array(
'name' => __( 'Feedbacks' ),
'singular_name' => __( 'Feedback' )
),
'supports' => array('title','editor','thumbnail','custom-fields'),
'public' => true,
'has_archive' => true,
)
);
}
In a page i am showing the list of the testimonials and using the function get_permalink() to get hyperlink. To show details of a testimonial post i have "single-testimonial.php" and but whenever i click to view details of the testimonial it redirects me to a link like '..../testimonial/postname'. Though there single-testimonial. php , it shows me the 404.php's content.
How can i show details of a post of custom-post-type ?
to get the single post pages to work you will have to add a few things to your register custom post type function... ie:
'public' => true, // yes you want it to be public?
'show_ui' => true, // you want it shown in the admin area
'show_in_menu' => true, // show it in menus etc..
'rewrite' => array('slug' => 'testimonial'), // this is the permalink structure
'show_in_nav_menus' => true, // show in navigation menus
'publicly_queryable' => true, // include this in searches
'query_var' => true, // do you want to pass values?
'capability_type' => 'post', // is it like a post or page?
'menu_position' => 25, // the position on the admin menu!
adding the slug should do the trick,
NOTICE: once slug has been set, go to settings->permalinks set back to default, then set back to postname, to flush the re-write rules, and so wordpress knows its there and working! that should do the trick, you dont need to add any template code to any files,
simply add the single-testimonial.php and it should work or
page-testimonial.php depending how you want it setup..
Marty
In your single-testimonial.php, make sure you have this line at the top:
// Template Name: Single Testimonial template
This is so Wordpress can know that this is a template file and allow you to select it from the 'page attributes' section when you are creating a testimonial post.
Secondly, use the get_post_permalink() function instead of get_permalink(). Here's why