Im trying to use a conditional statement on my single.php page.
What I want to do is, if it is of the Custom Post Type current-products, use a specific single -product.php template page, if not (ie a standard blog post) use the default single.php page.
I think Im using the right statement, but don't know what to do afterwards (to get the template to be used):
if ( is_single( 'current-products' == get_post_type() ) {
// If the post type is "Current Products" use this template...
// Please help me out on this bit
} elseif ( is_single() ) {
// If not, use the standard one...
// please help me out on this bit
}
I think that's right...?
WordPress automatically uses a different template page for different post types, if your post type is called products the files should be named single-products.php. You can read more about it from there Codex
In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
where {post_type} is the $post_type argument of the register_post_type() function.
You can use is_singular() function, according to WordPress documentation: Ref Link
This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type book.
is_singular('book');
This will accomplish your task.
I believe if you want to use for posts try this:
is_singular('post');
Thanks
Related
I've only written very basic shortcodes without variables and am just not figuring out how to write one that allows someone to enter a variable to pull info from a specific post.
I have a custom post type called "Events". I want to put a shortcode on the site FrontPage that will display the contents of a specific event within that custom post type. I'm assuming a variable is the way to do this, but I don't know what the best way is. Should I be having the user find the post ID number to use as the variable? Note that I am not trying to display an arcive of the post type, only the contents of a specific post, as indicated by the shortcode variable. I can see something like the following, but don't know how to achieve it:
[display-event id="77"]
Really, this is advanced for me, so any direction you can give me would be much appreciated.
~Laura
Have a look at WordPress's documentation for add_shortcode().
You would need to add something like the following in your functions.php file:-
function baztag_func( $atts, $content = "" ) {
// What you would like your short code to do
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
add_shortcode
I am not able to selectively disable plugins in mu-plugins folder using the is_singular() functions in a conditional. It works with functions such as is_user_logged_in etc..
But i need to disable plugin for posts hence trying the is_singular() but again it does not work in mu-plugins on a multi site, even if I declare the blog id.
I also tried many version such as 'post' == $post->post_type still no luck. Any ideas for a working conditional in muplugin for multisite to check for post types? Thanks
is_singular() conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.
True when viewing a post of the Custom Post Type portfolio.
is_singular( 'portfolio' );
Try to add custom post type in condition.
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.
Is it possible to check what template is a single post type using?
I have a single post page and a single post portfolio page. Post page is single.php and portfolio is single-portfoilo.php. In my body I can see the classes loaded based on what page I'm on (what template is used).
I cannot use is_page_template(), because that will only work on pages, not posts. My alternative is to use is_single(), but that will target the regular posts, and I only want the portfolio posts to be targeted by the if clause.
Is there a workaround for this? It's really impractical :\
You can use something like
if ( is_singular( 'portfolio' ) ) {
// conditional content/code
}
Or if you have more than one custom post types you can insert an array like this
if ( is_singular( array( 'portfolio', 'book' ) ) ) {
// conditional content/code
}
I am trying something out on wp. I want to display custom post types title on page post type.
here is my code.
function get_post_type_title(){
if('movie_ronny' == get_post_type()){ // if post type is movie_ronny, get post title
$movie = get_the_title(); //hold the post title in $movie.
}
if(is_page() ){ // if viewing page, display movie_ronny title
echo $movie;
}
}
add_filter('wp_head','get_post_type_title');
Above code does not display the title of movie post-type when viewing page. Any help is highly appeciated.
Your post type will most likely be shown on its own single-movie_ronny template. In which case of course you wouldn't need a conditional -
if('movie_ronny' == get_post_type()){...}
The other reason why this question is a bit strange is that your loop query must define within it's arguments what type of post, page or custom post type it is querying. So you would also then know which post type you are messing with and not need the first conditional.
And no matter what your situation is:
if(is_page() ){ // will only return true for pages. not posts or CPT's
Also,
get_the_title($id); // needs a post ID outside of the loop.
Well, there are two ways this function will fail (i.e. do nothing):
If the first if block is ignored then the second if block will not do anything (I believe it will actually show a notice, because $movie would not be defined).
If is_page() does not evaluate to true then nothing will be done in any case.
If you know anything about programming then this should be obvious. So, either you have no idea what you are doing, or I am misunderstanding something here.
Try this one:
<?php
$post_type = get_post_type_object( get_post_type($post) );
echo $post_type->label ;
?>