How to change default post attributes? - wordpress

My site needs to have image based posts, meaning the post is only an image.
Now i tried implementing it with Custom Post Types, but have encoutred problems,
like categories didn't show the right posts, pagination caused problems etc.
Now i thought to myself that i don't need the regular posts and if i could just edit them
to have only the featured image option enabled, life would be much easier.
But i failed to find any information regarding this.
Anyone can help me please?

To add featured image support/option simply add the following code snippet to your functions.php file located inside your theme's root folder
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
}
and to show the featured image inside your template you can do
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(); // will show the featured image if you have set any for the post
}
Applicable to Wordpress version-2.9 and higher.
When you add new post just find the featured image meta box and set an featured image from there.
You can also setup image sizes (depending on that images will be displayed) at the front end, just take a look at here.

Related

WordPress - Missing Featured Image Thumbnail in Posts Page

I added a featured image to my custom post type and it appears correctly on the front-end, but on the custom post type the featured image thumbnail doesn't show up at all.
As you can see, the Remove featured image button is read, but the thumbnail is not available. I used the_post_thumbnail() in my PHP code to display the featured image, which works correctly, but I don't know why the thumbnail missing. Anyone know why this is happening?
Thanks!
just add below code to your active theme's function.php
add_action( 'after_setup_theme', 'custom_featured_image_backend' );
function custom_featured_image_backend() {
add_theme_support( 'post-thumbnails','your_custom_post_type_name');
}

Option to display Featured Image, or not

Wordpress 4.3.1
Hi,
this is my first post at stackoverflow and I hope I did everything right.
I need a function that allows to output the Featured Image, or not. Just not uploading an image is no option, because the thumbnail next the excerpts is needed in any case.
The following combinations are needed:
Thumbnail and Featured Image are identical
Thumbnail and first image in the article are different
Thumbnail and no Featured Image
I need the function for case 2 and 3. In case 2, the Featured Image will be integrated via the editor.
I have already found this plugin: https://wordpress.org/plugins/hide-featured-image/
However, that solves the task only with CSS:
.has-post-thumbnail img.wp-post-image{ display: none; }
But I would like that the picture is not even putted out and loaded in the Browser.
I have the following solution found on the net. It is working so far that the checkbox is shown inside the Featured Image box and it also saves the state if it is checked, or not, after updating the article. However, the Featured Image continues to be output and I don't understand what is going wrong.
Here is the code that I copied to the functions.php:
/**
 * Adds a checkbox to the featured image metabox.
 *
 * #param string $content
 */
  
add_filter( 'admin_post_thumbnail_html', 'optional_featured_image', 10, 2 );
/**
 * Add a 'hide the featured image' checkbox to the Featured Image area
 */
function optional_featured_image( $content, $post_ID ) {
    $content .= '<div class="hide-feat-image">';
    // add our nonce field
    $content .= wp_nonce_field( 'display_featured_image_nonce', 'display_featured_image_nonce', true, false );
    // create our checkbox
    $content .= '
    <input style="margin: 5px;" type="checkbox" id="display-featured-image" name="display_featured_image" '. checked( get_post_meta( $post_ID, 'display_featured_image', true ), true, false ) .' value="1"/>
    <label for="display-featured-image">Hide on post page?</label>
    ';
    $content .= '</div><!-- hide-feat-image -->';
    // return our appended $content
    return $content;
}
add_action( 'save_post', 'save_optional_featured_image' );
/**
 * Save our 'hide the featured image' checkbox to the Featured Image area
 */
function save_optional_featured_image( $post_id ) {
    if (
        // check nonce
        !isset( $_POST['display_featured_image_nonce'] )
        || !wp_verify_nonce( $_POST['display_featured_image_nonce'], 'display_featured_image_nonce')
        // make sure user is allowed to edit posts
        || !current_user_can( 'edit_post', $post_id )
    )
        return;
    // sanitize our input to yes or no
    $display = isset( $_POST["display_featured_image"] ) && $_POST["display_featured_image"] ? true : false;
    // update our post
    update_post_meta( $post_id, 'display_featured_image', $display );
} 
And here's the WP snippet, I use at the template for the output:
<?php the_post_thumbnail('header-image'); ?>
I'm grateful for any advice! Thank you in advance for your effort!
Firstly I'd check in the database that this 'optional_featured_image' parameter is actually being set by your code. Check the prefix_postmeta table and lookup for the post you're editing.
When the template is output you need to have a conditional to check for this 'display_featured_image' parameter you've setup. Otherwise the_post_thumbnail() will always display.
Try this code in your template:
<?php
$optional_featured_image = get_post_meta( get_the_ID(), 'display_featured_image', true);
if( !$optional_featured_image )
the_post_thumbnail('header-image');
?>

Wordpress images link

I'm just starting with wordpress. I create blog and right now I have index.php with list of posts. If I add images to posts in admin panel , images have href but they redirect to attachment - not to post. Of course I can use:
has_post_thumbnail()
but this works only if I set it in admin.
My question is : How to create redirect to post as default (not to attachment ) for images added to post.
This is typical of WordPress and is supposed to happen. It is up to the author when inserting media into the page how it is displayed. See the Attachment Settings in the Bottom Right of the Corner. You will want to select None or Custom URL based on your preferences. Yours is currently set to "Media File" Like the screenshot below.
You can add this snippet of code to your themes functions.php file to change the default settings:
function image_overrides() {
update_option('image_default_align', 'center' ); // Changes the alignment
update_option('image_default_link_type', 'none' ); // Changes the Link type
update_option('image_default_size', 'large' ); // Changes the default size
}
add_action('after_setup_theme', 'images_overrides');

remove and disable featured image in posts

I write a post in new post editor and then add media. I can see the images in the editor but when i publish them they aren't loading up and a frame with little square in the middle comes up. This is the link to one of my posts: http://getprogramcode.com/2013/03/c-program-for-bit-stuffing/ . For some people only link to the image comes up and it opens up with 404 error. See the line after OUTPUT: bit stuffing.
Also i want to remove the featured image from appearing in my posts. I have a option in my theme on a new post: "disable featured image" - but that doesnt work . Also if i dont put the image or i remove the featured image the empty image appears: see the home page: http://getprogramcode.com Please help me to solve this problem
You should not use relative paths on WordPress, only Absolute paths.
The problem is that you are using ../wp-content/... as your URL's for image paths.
When adding images, you should have the option to give it a path, you should opt to link to the media file.
For the disable feature image, if you go into the page.php or single.php code, it should have a line of code in it for calling in the featured image.
It should look something like this:
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
You just need to remove or comment out this code and it should stop showing up on the pages.

Different thumbnails sizes for different custom post types, and creating thumbnails for featured images only

I've got a wordpress site with a few custom post types. Each custom post type shows a list page, basically displaying a featured image and a title, and then you can click through to a detail page with a bunch more images.
I only need to resize uploaded "feature images". If an uploaded image isn't used as a feature image, I don't need a thumbnail for it. Furthermore, each custom post type shows a different sized feature image.
So, what I want to do is say:
a) Only create thumbnails for feature images
b) Create certain sizes for certain post types, and not for others.
Is this possible? At the moment, EVERY uploaded image is getting 5 thumbnail sizes, and my wp-content directory is WAAAY bigger than it needs to be!
Thanks in advance.
Ok I found a solution using the intermediate_image_sizes filter hook. Place this code in your functions.php and replace the 'post_type' and 'img_size_name' with the names of your post and desired image size.
add_filter( 'intermediate_image_sizes', function($sizes){
$type = get_post_type($_REQUEST['post_id']);
foreach($sizes as $key => $value){
if($type=='post_type' && $value != 'img_size_name'){
unset($sizes[$key]);
}
}
return $sizes;
});
To avoid waste of space, the best solution is to use phpthumb (http://phpthumb.sourceforge.net/) inside your page templates.

Resources