I'm working on a blog using the theme Coral Dark. This theme has support for post thumbnails, and I want to remove them from the individual posts and leave them everywhere else (home, archives, categories, etc). The thumbnails are created with a pluggable function, so in theory I should just have to override it. If I add this to the functions.php of my child theme:
function coral_dark_post_thumbnail() {}
The thumbnails effectively disappear. However, if I use a conditional to run the function in posts only:
if ( is_single() ) {
function coral_dark_post_thumbnail() {}
}
It does nothing. The thumbnails still appear everywhere. I can even replace is_single with anything else because WordPress simply ignores it. And if I add a negation to the conditional like this:
if ( ! is_single() ) {
function coral_dark_post_thumbnail() {}
}
The thumbnails disappear but everywhere. Again, I can replace is_single with whatever because the result is the same. I don't understand what's happening.
This is the original function:
if ( ! function_exists( 'coral_dark_post_thumbnail' ) ) :
/**
* Displays an optional post thumbnail.
*
* Wraps the post thumbnail in an anchor element on index views, or a div
* element when on single views.
*
* Create your own coral_dark_post_thumbnail() function to override in a child theme.
*
*/
function coral_dark_post_thumbnail() {
if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
return;
}
if ( is_singular() ) :
?>
<div class="post-thumbnail">
<?php the_post_thumbnail('large'); ?>
</div><!-- .post-thumbnail -->
<?php else : ?>
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true">
<?php the_post_thumbnail( 'post-thumbnail', array( 'class' => 'alignleft smallpostthumb', 'alt' => the_title_attribute( 'echo=0' ), 'sizes' => '(max-width: 480px) 100vw, 210px' ) ); ?>
</a>
<?php endif; // End is_singular()
}
endif;
Many thanks in advance. :)
Seems your problem is with the condition. You cannot use condition to wrap your function li
if ( is_single() ) {
function coral_dark_post_thumbnail() {}
}
Instead use:
function coral_dark_post_thumbnail() {
if ( is_single() ) {
//TODO: Something
}
}
Doing the above should work.
Related
Elementor offers a publishing widget to display articles. I use it to display personalized posts : referencing. These listings all have categories, for example "Bar", Hotel".
Elementor offers via the publishing widget a card style, which allows to display a badge. By default, this badge displays only one category.
I read a lot of topics on the Internet, for example on this GitHub.
I tried to create plugins with this code inside
add_action( 'elementor/widget/posts/skins_init', function( $widget ) {
class issue6480_skin extends \ElementorPro\Modules\Posts\Skins\Skin_Cards {
protected function render_badge() {
$taxonomy = $this->get_instance_value( 'badge_taxonomy' );
if ( empty( $taxonomy ) ) {
return;
}
$terms = get_the_terms( get_the_ID(), $taxonomy );
if ( ! is_array( $terms ) ) {
return;
}
?><div class="elementor-post__badges"><?php
foreach( $terms as $term ) : ?>
<div class="elementor-post__badge"><?php echo $term->name; ?></div>
<?php endforeach; ?>
</div>
<?php
}
public function get_id() {
return 'cards_multi_badge';
}
public function get_title() {
return __( 'Cards Multi Badge', 'elementor-pro' );
}
}
// register the skin to the posts widget
$widget->add_skin( new issue6480_skin( $widget ) );
} );
But it is impossible to make multiple categories work and display them. Do you have any idea which files I should turn to in order to create this code? Thanks
I was working on site pages here which comes in wedding category which comes in a custom taxonomy named listing.In my wedding category listing i dont want to display the price sticker but it shoud display in all other listing posts eg this page
I have tried
if ( in_category( 'wedding' )) {
?><style>.pricestricker{display:none!important;}</style><?php
}?>
also
if ( is_category( 'wedding' )) {
?><style>.pricestricker{display:none!important;}</style><?php
}?>
also
if ( has_term('listing', 'wedding' )) {
?><style>.pricestricker{display:none!important;}</style><?php
}?>
none of these worked.Please help.Thank You
Please try this once where the div "pricestricker" is present:
<div class="pricestricker" <?php if ( in_category( 'wedding' )) { echo 'style="display:none;"' }?>><span itemprop="price" class="wlt_shortcode_price">$0</span></div>
Here you should only write the php code inside the div "pricestricker" and remain will be default code.
or you can do:
<?php if ( !in_category( 'wedding' )) { ?>
<div class="pricestricker"><span itemprop="price" class="wlt_shortcode_price">$0</span></div> // This will be your default code
<?php } ?>
I have a script in my functions that adds a class to the featured image based on whether it is landscape or portrait. It works by measuring the height against the width of given post thumbnail and appending the class from attachment- into horizontal-image attatchment-.
if ( ! function_exists( 'mytheme_vertical_check' ) ) :
function mytheme_vertical_check( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$image_data = wp_get_attachment_image_src( $post_thumbnail_id , 'large' );
//Get the image width and height from the data provided by wp_get_attachment_image_src()
$width = $image_data[1];
$height = $image_data[2];
if ( $width > $height ) {
$html = str_replace( 'attachment-', 'horizontal-image attachment-', $html );
}
return $html;
}
endif;
add_filter( 'post_thumbnail_html', 'mytheme_vertical_check', 10, 5 );
However, instead of adding horizontal-image to the actual IMG class, I need it to be added to the container's class. The container is a div with the class masonry-thumbnail. So I would like the appropriate containers to instead have masonry-thumbnail horizontal as their class.
Note: The original writer of this function suggested that I used a jQuery fix, and after trying several different strings of code, I realized that it did not work well with Infinite Scroll and would therefore be better of with the modification coming from inside WordPress itself by way of functions, just like the original code (which works perfectly) appending the class when the page initially renders, as opposed to post-page load through jQuery. Besides, I'd like to use as little JavaScript as possible.
You could try something like this. Modify your function to return only 'horizontal' class if image is landscape:
function mytheme_vertical_check() {
$thumb_id = get_post_thumbnail_id();
$image_data = wp_get_attachment_image_src( $thumb_id , 'tp-thumbnail' );
$width = $image_data[1];
$height = $image_data[2];
if ( $width > $height ) {
return 'horizontal';
}
}
And inside the loop call it to add 'horizontal' class to a div of your choice:
<?php if ( has_post_thumbnail() ) : ?>
<?php printf( '<div class="masonry-thumbnail %s">', mytheme_vertical_check() );?>
<?php the_post_thumbnail( 'large' ); ?>
</div>
<?php endif; ?>
I was able to remove the featured image metabox from custom post types of pages. Below is what I used:
add_action('do_meta_boxes', 'remove_thumbnail_box');
function remove_thumbnail_box() {
remove_meta_box( 'postimagediv','page','side' );
}
However, what I really want to do is to only apply this to a specific page template. Is this possible?
Thanks!
Ah, I found a solution.
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file === 'page-template-name.php') {
add_action('do_meta_boxes', 'remove_thumbnail_box');
function remove_thumbnail_box() {
remove_meta_box( 'postimagediv','page','side' );
}
}
If there's a better solution... please don't hesitate to post.
Thanks!
For anyone else looking to remove featured image support programatically (the comments should be self explanatory):
/**
* Removes the featured image metabox from specific pages (by ID)
* #note: populate the $excluded_page_ids_array array with page IDs
*/
function eh_remove_featured_image_metabox_from_specific_pages() {
// populate with page IDs to exclude featured image metabox from
$excluded_page_ids_array = array( 38, 29 );
// store the current post ID
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
// check if the current page is in our excluded array
if( in_array( $post_id, $excluded_page_ids_array ) ) {
// remove featured image metabox
remove_meta_box( 'postimagediv','page','side' );
}
}
add_action( 'do_meta_boxes', 'remove_featured_image_metabox_from_specific_pages' );
After adjusting the array of pages to exclude, you'll want to add this snippet to your themes functions.php file.
Alternatively, you can remove the featured image from all pages, except specified ones - where the featured image metabox will remain.
/**
* Removes the featured image metabox from all pages except specified ones
* #note: populate the $page_ids array with page IDs
*/
function eh_remove_featured_images_metabox_from_all_pages_except_specified() {
// populate with page IDs to keep the featured image metabox on
$page_ids = array( 25, 31 );
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
if( ! in_array( $post_id, $page_ids ) ) {
// remove featured image metabox
remove_meta_box( 'postimagediv','page','side' );
}
}
add_action( 'do_meta_boxes', 'eh_remove_featured_images_metabox_from_all_pages_except_specified' );
There is another solution. By editing the page.php
If your feature Image div Looks like below for example
<div class="thumbnail">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
</div>
you can use
if (is_page( array( 42, 'about-me', 'Contact' ) );
// Returns true when the Pages displayed is either post ID :42, or post_name_slug "about-me", or post_title "Contact".
<?php
if (is_page( array( 42, 'about-me', 'Contact' ) ) ) {
?>
<div class="thumbnail" style="display:none;">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
</div>
<?php
}else{
?>
<div class="thumbnail">
<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>
</div>
<?php}?>
I just updated a menu on a site of mine to utilize wp_nav_menu. Setting this up with WP was fairly straight forward however I've run into one small snag with the the way wordpress is outputting its parent/ancestor classes for use in highlighting the current page that the content belongs to, particularly with single post pages...
Highlighting the current page with .current_page_item a and .current_page_parent a works perfect as long as its just on a normal page with children, however as soon as you visit a post from events or media, the blog link in the menu is highlighted instead which is incorrect obviously.
*One thing noticeably wrong when looking at Wordpress' output is that the current page classes are not even being generated on the correct li tag that the post belongs to which seems to be the root of the problem.
For future reference, the Events, Media, & Blog pages all use a special query I've written to only grab the respective category for that page, ie.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("category_name=media&paged=$paged");
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
</div>
<?php
endwhile;
else:
endif;
Hope thats enough info, if not let me know.
Best,
SB
EDIT - August 3, 2011
Below is a screen shot of what Im referring to when I say that wp_nav_menu is generating the current classes on the wrong li tag. Highlighted in Blue is the menu item that the post actually belongs to. Hightlighted in Grey is the incorrect li tag that wordpress has decided to add the current classes to instead.
http://img688.imageshack.us/img688/4180/picture2zo.png
EDIT - August 4, 2011
Maybe this will help demonstrate how I have the menu setup thus far a little better w/ Hadvig's assistance?
In my functions.php template I have -
<?php
// Add Custom Menu Support
if ( function_exists( 'register_nav_menu' ) ) {
register_nav_menu( 'epr_menu', 'EPR Main Menu' );
}
function my_menu_items_hook($items, $menu, $args) {
if ( 'epr_menu' == $menu->slug ) { // check if it is process your top menu
if ( is_single() ) { // check if single post loaded
if ( in_category('events') || in_category('media') ) {
foreach ( $items as $key => $value ) {
if ( 'blog' == $value->ID ) {
$items[$key]->classes[] = array(); //unset classes for blog item
}
// add class if post from event category
if ( in_category('events') && 'events' == $value->ID ) {
$items[$key]->classes[] = 'current-menu-item';
}
// add class if post from media category
if ( in_category('media') && 'media' == $value->ID ) {
$items[$key]->classes[] = 'current-menu-item';
}
}
}
}
}
return $items;
}
add_action('wp_get_nav_menu_items', 'my_menu_items_hook', 10, 3);
?>
In my header.php template I'm calling the menu like so -
<div id="nav_wrapper">
<ul id="nav">
<?php wp_nav_menu( array( 'container' => '', 'items_wrap' => '%3$s' ) ); ?>
</ul>
</div>
I suppose problem because you set post page as Blog and wordpress set it as parent(in your menu) for all post. You can try to change this behaviour with wp_get_nav_menu_items hook. Example:
function my_menu_items_hook($items, $menu, $args) {
if ( 'my-menu-slug' == $menu->slug ) { // check if it is process your top menu
if ( is_single() ) { // check if single post loaded
if ( in_category(EVENT_CATEGORY_ID) || in_category(MEDIA_CATEGORY_ID) ) {
foreach ( $items as $key => $value ) {
if ( BLOG_PAGE_ID == $value->object_id ) {
$items[$key]->classes[] = array(); //unset classes for blog item
}
// add class if post from event category
if ( in_category(EVENT_CATEGORY_ID) && EVENT_PAGE_ID == $value->object_id ) {
$items[$key]->classes[] = 'current-menu-item';
}
// add class if post from media category
if ( in_category(MEDIA_CATEGORY_ID) && MEDIA_PAGE_ID == $value->object_id ) {
$items[$key]->classes[] = 'current-menu-item';
}
}
}
}
}
return $items;
}
add_action('wp_get_nav_menu_items', 'my_menu_items_hook', 10, 3);
You should replace EVENT_CATEGORY_ID and MEDIA_CATEGORY_ID with your category ids(or names). Also replace EVENT_PAGE_ID and MEDIA_PAGE_ID with your page ids. Replace 'my-menu-slug' with your menu slug.
Of course this would work only if you attach post to only one category from Events, Media or Blog.
Updated.