Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
Related
I'm building a website with some articles accessible only for Members. All articles, accessible or not are shown on a page and i would like to add an icon on top of the featured image for people to understand which articles they can see for free or not.
Could you help ? I've already read the doc about featured image but my brain can't make the whole thing...
Here's what i already tried :
function article_reserve( $title ) {
if ( has_tag( 'abonne' ) ) {
$title = 'test ' . $title;
}
return $title;
}
add_filter( 'the_title', 'article_reserve' );
It change the title but i'm trying to change the featured image
Also tried that after comments :
function wpse_post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
// Optionally add any logic here for determining what markup to output.
// $html will be an empty string if there is no post thumbnail.
if ( has_tag( 'abonne' ) ) {
$new_html = '<img src="https://placekitten.com/g/600/600" alt="kitten">';
}
return $new_html;
}
add_action( 'post_thumbnail_html', 'wpse_post_thumbnail_html', 10, 5 );
I would like to remove the ‘Category’ from header title text on category pages in wordpress. Have tried several plugins that claim to remove these but don’t seem to work with my version.. (4.9.6)
Here a screenshot of how it looks now:
https://imgur.com/a/E2gdr2l
So would want to have only ‘News’ displayed there. Anyone knows a way of getting rid of the Category before it?
All the best and thanks in advance!
In Magazine Base theme:
The title is added from the file inc/hooks/header-inner-page.php using the function the_archive_title() like
the_archive_title('<h1 class="entry-title">', '</h1>');
to remove Category from the output you need to add the following filter in your functions.php
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>' ;
}
return $title;
});
Code credit: Here
That would fix the issue.
Just add a line into your functions.php (After WP5.5).
add_filter( 'get_the_archive_title_prefix', '__return_false' );
check in archive.php or category.php
it is loading from here
<h1 class="entry-title">Category: News</h1>
you can check and edit any of the above mentioned files.
I want to generate a placeholder-text in the 'content editor' if post has post format 'gallery'. But I can't get it working:
functions.php:
add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
function wpse57907_default_content( $content, $post ) {
if ( 'post' == $post->post_type && has_post_format('gallery')) {
$content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project's page. Please use the fields above for text.</i>';
return $content;
}
}
This should do it:
add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
function wpse57907_default_content( $content, $post ) {
$format = get_post_format($post->ID);
if ( 'post' == $post->post_type && $format == 'gallery') {
$content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project's page. Please use the fields above for text.</i>';
}
return $content;
}
Also, please make sure that your theme is set to actually have post-format support with add_theme_support and that your post has the correct post format (gallery).
I'm using the multiple post thumbnails plugin for Wordpress and am trouble finding a way to remove the default html attributes wordpress seem to generate? When you view the html source it comes up with;
<img width="600" height="450" src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" class="attachment-post-thumbnail size-post-thumbnail" alt="hotplate2thumb" />
I want to remove the width and height because that's invalid code. And I want to remove the class too, to make the site look less wordpressy.
Ideally it should generate as:
<img src="http://localhost/news/../media/news-specials/hotplate2thumb.jpg" alt="hotplate2thumb" />
I've already got this in the functions.php
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
But that only works for wordpress' defult post thumbnails and not the multiple/secondary one.
I also tried implementing the code like so on the template:
<?php
if (class_exists('MultiPostThumbnails')):
echo "<img src=\"".MultiPostThumbnails::get_post_thumbnail_url(get_post_type(), 'secondary-image') . "\" alt=\"\" />";
endif; ?>
But that still renders a second image for all posts even if I don't have one set, so then I get broken images appearing in the theme. Not all my posts use a second image, only a few.
Thanks
Okay I solved it by adding
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
to this function
//remove class from the_post_thumbnail
function the_post_thumbnail_remove_class($output) {
$output = preg_replace('/class=".*?"/', '', $output);
return $output;
}
add_filter('post_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter('post_secondary-image_thumbnail_html', 'the_post_thumbnail_remove_class');
add_filter( 'post_secondary-image_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
I have a menu with multiple dropdowns.
I want to show bellow or beside the link of a post or posts in a menu dropdown the featured image. Is it possible?
I've attached an image to this message.
I don't want to know how to style it or something like that.
So let's say I have "Siguranta", I want to display the featured image of that post underneath and a "Read more" link under the image. Many thanks in advance.
Add filter to specific menus
add_filter('wp_nav_menu_args', 'add_filter_to_menus');
function add_filter_to_menus($args) {
// You can test agasint things like $args['menu'], $args['menu_id'] or $args['theme_location']
if( $args['theme_location'] == 'header_menu') {
add_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
}
}
Filter menu
function filter_menu_items($item)
{
if ($item->type == 'taxonomy') {
// For category menu items
$cat_base = get_option('category_base');
if (empty($cat_base)) {
$cat_base = 'category';
}
// Get the path to the category (excluding the home and category base parts of the URL)
$cat_path = str_replace(home_url() . '/' . $cat_base, '', $item->url);
// Get category and image ID
$cat = get_category_by_path($cat_path, true);
$thumb_id = get_term_meta($cat->term_id, '_term_image_id', true); // I'm using the 'Simple Term Meta' plugin to store an attachment ID as the featured image
} else {
// Get post and image ID
$post_id = url_to_postid($item->url);
$thumb_id = get_post_thumbnail_id($post_id);
}
if (!empty($thumb_id)) {
// Make the title just be the featured image.
$item->title = wp_get_attachment_image($thumb_id, 'poster');
}
return $item;
}
And then you want to remove the filter that you applied at the beginning, so that the next menu processed doesn't use the same HTML as defined above in filter_menu_items().
Remove filters
add_filter('wp_nav_menu_items','remove_filter_from_menus', 10, 2);
function remove_filter_from_menus( $nav, $args ) {
remove_filter( 'wp_setup_nav_menu_item', 'filter_menu_items' );
return $nav;
}
So, I will answer my own question. I finally did it with this code:
// get featured image
$thumbnail = get_the_post_thumbnail( $item->object_id );
//display featured image
$item_output .= $thumbnail;
I have to mention that I used this code in the walker class.