Hot answers tagged single
7
You can use the following instead,
if (is_singular('post')) {
//your code here...
}
Where by is_singular is the WordPress API conditional function for testing for the existence of a post type. You can also pass an array of post types if you wish.
http://codex.wordpress.org/Function_Reference/is_singular
6
Although I'm also curious as to why you'd want to do this, and would probably suggest using a custom post type instead, this would probably work (actually works for any single post type except pages and attachments):
add_action( 'pre_get_posts', 'wpse44983_single_post_404' );
function wpse44983_single_post_404( $query ) {
if ( $query->is_main_query() ...
6
You don't want a post to be the front page, you want a custom post type entry to be the front page. Now that we have the terminology right, yes it's possible.
A client once asked me to do the exact same thing. They had a custom post type they needed displayed on the front page. Doing so was as simple as adding a filter to allow them to select a "stack" ...
5
There's nothing stopping you using AJAX for a singlepage website that loads in other pages dynamically rather than going to a whole new page. I would advise you build the site without the AJAX and then add AJAX on top so that everything degrades gracefully should an error occur or if javascript is turned off.
So assuming that you've now got a site that ...
4
You could try something like (http://codex.wordpress.org/Function_Reference/get_post_type) :
to check if it's not a specific post-type :
if ( is_single() && 'portfolio' != get_post_type() ) {
// DO STUFF
}
or to check if it's a post and not a post-type :
if ( is_single() && 'post' == get_post_type() ) {
// DO STUFF
}
3
Well, for anyone else looking to solve a similar problem, I found this post, and it led me in the right direction: http://wordpress.org/support/topic/pagination-on-singlephp-causing-301-redirect?replies=9.
I added this snippet of code to my functions.php, and I can paginate in custom queries without issue on single.php now. I don't know enough about it to ...
3
According to the WordPress conditional docs it should be:
if(is_singular( 'video' ) ){
// do conditional stuff
}
else{
//do other stuff
}
Also if you want a blank page or no content to show when the CPT is not video you don't really need the else so you could omit it. You could ALSO do something like:
if(!is_singular( 'video' ) ){
// do ...
3
Unless you have added the functionality with a plugin or custom code, Pages are not placed in Categories. Pages are organized with Parent Pages and Children Pages. This is why (I suspect) in_category() does not work in page.php
The following code in functions.php will add categories to Pages.
/**
* Add categories to Pages.
* from ...
3
Use the post thumbnail for the single blog pages too. You can use the add_image_size() function to create an additional image size that is displayed on the blog posts.
add_image_size('single-post-image', 650, 250, true); // name, width, height, crop mode
Then in single.php
the_post_thumbnail('image-size');
Simply place the first function in your ...
3
While much of comments implementation became simpler in modern WP versions, it still isn't quite make-comments-happen button.
You will need to put together this part of template, most of related functionality is handled by:
wp_list_comments()
comment_form()
Check native WP themes for complete start to end implementation, although they tend to be a ...
3
There are a number of template filters you can use to alter the template hierarchy. Have a look at the template hierarchy page at the filters and example provided.
Here's a modified version of the example that uses single_template. It checks for a specific custom post type and loads the theme's page.php template if it exists:
function ...
3
You can use the filter hook for 'single_template'. Create single-cat1.php, single-cat2.php and single-cat3.php (cat1, cat2 and cat3 are the category names should be replaced by your category names)
function template_change( $template ){
if( is_single() && in_category('cat1') ){
$templates = array("single-cat1.php");
} elseif( ...
3
There are some global variables available (or not) to detect the current page number:
if ( empty ( $GLOBALS['multipage'] ) or $GLOBALS['numpages'] === $GLOBALS['page'] )
echo '<a id="lastPageLink" href="#comment">comment</a>';
The best way to understand what they do is a look at the internals of wp_link_pages().
(bool) ...
3
is_single() returns TRUE or FALSE, not a string. Additionally, you can only test for a specific post with is_single() function by putting the post slug into the function call:
if ( is_single( 'your-post-slug' ) )
{
# do something
}
If you want to test for the proper post type use:
if ( is_singular() and 'your-post-type' == get_post_type() )
or ...
2
image_send_to_editor
function insert_img_rel_attrib( $html, $id, $caption, $title, $align, $url, $size, $alt )
{
/* Run your magic on the image $html to insert rel attrib */
return $html;
}
add_filter( 'image_send_to_editor', 'insert_img_rel_attrib' );
2
Your best bet is to use the WordPress rewrite API. No .htaccess required.
First, hook into init and add a rewrite endpoint to your permalinks. This tells WordPress that whens some visits /category/post-slug/gallery match the new endpoint rewrite. it also takes care of adding the query variable for you so you don't have to do that.
<?php
add_action( ...
2
It's not a global variable; in fact it's not a variable at all. It's just placeholder within the sprintf() function. Take a look at the sprintf PHP function documentation.
In the example you cite, the author is using the 'argument swapping' placeholder syntax: %n$t where %n is the placeholder number that corresponds to the argument order (eg: %1, %2 etc) ...
2
Yes, there is 'the_content' filter. See the codex.
For instance:
add_filter( 'the_content', 'my_the_content_filter', 20 );
/**
* Add a test to the end of every post page.
*/
function my_the_content_filter( $content ) {
if ( is_singular('hotels') ){
//If a single post is being viewed, add something
//Or get the value of a custom ...
2
A slight modification on the function above. You'll want to use is_singular('post') instead, unless you also want to target other areas
add_shortcode( 'hidetext', 'rkv_hidetext_shortcode' );
function rkv_hidetext_shortcode( $atts, $content = null) {
if( is_singular('post') )
return $content;
else
return;
}
tested and works.
2
add_shortcode( 'hidetext', 'my_hidetext_shortcode' );
function my_hidetext_shortcode( $atts, $content = '' ) {
if( is_single() )
return $content;
else
return;
}
Docs: add_shortcode(), is_single()
Solution is untested.
2
There are many ways to accomplish this, though some are more advanced than others:
Mark the blog post as sticky, and then set Posts per Page to 1 (Dashboard -> Settings -> Reading)
Create a custom front-page.php template, and query the post in question, either via the sticky post designation, or via custom post meta
Create a custom front-page.php ...
2
Look at http://codex.wordpress.org/Function_Reference/in_category
Example:
<?php if ( in_category('rabbis-weekly-commentary') ) : ?>
<div id="inner_header">
<img src="http://www.ifcj.ca/ifcj_ca/images/elements/commentary_header.gif" border="0">
...
<?php elseif ( in_category('yaels-weekly-commentary') ) : ?> ...
2
I'm making a couple of assumptions about how your theme is setup:
You have two files:
index.php. This is the file that begins the execution of the loop.
content.php. This is the file in which your generic content template exists.
You are familiar with get_template_part(). If not, it's easy to implement.
For example purposes, I have two users in my ...
2
This should help, if you need more info: http://codex.wordpress.org/Template_Hierarchy
Simply put, Wordpress looks for template files in the following order for single posts:
single-{post_type}.php,
single.php,
index.php.
If you want to override the standard single.php file, that's how (for a custom post type only, as you're using)
2
Instead of making it category specific you could use post formats and use different content-templates. In single.php you can write
<?php get_template_part( 'content', get_post_format() ); ?>
Then create different post formats
add_theme_support( 'post-formats', array( 'withpictures', 'withcomments' ) );
Then create different post templates ...
2
You can use the locate_template() function to include the 404 template. Remember to do this instead of using get_header() or generating any output; otherwise you will have a duplicate.
<?php
if ( ! $myVar ) {
status_header(404);
global $wp_query;
$wp_query->is_404 = true;
locate_template('404');
return;
}
// otherwise, show the ...
2
I don't think get_categories() is the best option for you in this case because it returns a string with all the categories listed as anchor tags, fine for displaying, but not great for figuring out in code what the categories are. Ok, so the first thing you need to do is grab the product/post object for the current page if you don't already have it:
global ...
2
Check out get_next_post_sort and get_previous_post_sort filters. You can modify the ORDER BY clause to whatever you want (including ). For example, if you want to sort the 'next post' to be the post with the smallest adjacent post ID:
function wp28041_get_next_post_sort($where){
return 'ORDER BY ID ASC LIMIT 1';
}
add_filter('get_next_post_sort', ...
2
As @Ravs mentions the third parameter in next_post_link()/previous_post_link() should be a boolean value indicating whether to restrict the link to a post in the same category. By default this is false. If set to true, it will choose the next (or previous) post which is in the same category as the current post. This includes all parent & child ...
2
I think this approach will work.
1.create template for single post like singlepost.php( default single post template),singlepost-99.php,singlepost-101.php.
2.now put just this code in single.php
<?php
global $post;
get_template_part('singlepost',$post->ID);
?>
what this code does check for single post template for current post by post id if not ...
Only top voted, non community-wiki answers of a minimum length are eligible