How to show some html after featured image? - wordpress

I have a related post code and I wants place it just after featured image on single post page. I have tried,
add_action( 'loop_start', 'wcr_related_posts', 10, 0 ) ;
but it doesn't work as I want to show content. It does show content at about proper place but the sidebar is not moving, just content is being shown a bit below and I also want the sidebar to come below due to code that wcr_related_posts generates.
I have not been able to find a hook that actually works that way I want it to.

You can do this using the 'the_content' filter:
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID, 'post-single'), $content, 1 );
return $content;
}
Get the more details and examples as follow link : https://wordpress.stackexchange.com/questions/61272/how-do-i-add-the-featured-image-to-the-content-after-the-first-paragraph

Related

Add blocks to custom location on shop page?

On my woocommerce shop page I want to remove the description/blocks from the all-in-one woocommerce product grid block. Then add them back above it separately for layout reasons.
So I've removed the description like this:
remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
Then I thought I could just add it back in with a shortcode block in the archive-product.html template.
add_shortcode('shop_description', 'woocommerce_product_archive_description');
The problem is, this adds it to the very top of document even before the <html> tag, instead of where I put the shortcode block...
I realised the woocommerce_product_archive_description function uses echo and to make it work with a shortcode it needs to instead use return.
So I made a new function for the shortcode and boiled it down to be:
$shop_page = get_post( wc_get_page_id( 'shop' ) );
$allowed_html = wp_kses_allowed_html( 'post' );
$description = wc_format_content( wp_kses( $shop_page->post_content, $allowed_html ) );
if ( $description ) {
return $description;
}

Tag comments like <!-- sample comment --> in html for pages and posts in content WordPress

I write content in html with comments, followed by insertion on the page (visual editors and builders are disabled).
I insert this html into the WordPress page and the comments, although not visible, are taken into account, this leads to a bunch of unnecessary vertical indentation.
Question: how to fix? Who has encountered it?
UPD: in the page code, it turns into paragraphs: <p><!-- comment --></p>
Thank you.
UPD2:
It became clear that it is possible (necessary) to use the functions add_filter() and the_content
But how do you do it right? Prompt?
It is necessary that there are tags on the page or records and "nothing" is displayed in their place.
Something like.. (I don't know what to write myself to display in place of the "nothing" comment tags.
/* Add empty paragraph only to Pages. */
function tag_comment_empty_added_page_content ( $content ) {
if ( is_page() ) {
return $content . '<!-- * -->';
}
return $content;
}
add_filter( 'the_content', 'tag_comment_empty_added_page_content');
Or maybe in that vein?
<?php
function tag_comment_empty_replace_content( $text_content ) {
if ( is_page() ) {
$text = array(
'<!-- * -->' => '',
);
$text_content = str_ireplace( array_keys( $text ), $text, $text_content );
}
It all doesn't work, somewhere a mistake.
Assuming you store your content as posts, can create a filter for the_content to remove all comments when the content is echoed out.
https://developer.wordpress.org/reference/hooks/the_content/

Adding author info outside the loop in Genesis

I'm posting this question as I'm having a little trouble adding the author info to my post hero on my website.
I'm using the Genesis framework with Wordpress, so what I did is removing the post info from the post and adding it back in the post hero. That all works, except that the author name is not being shown anymore as it's not yet fetched in the post loop.
// Remove entry title
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
// Remove post info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
// Add page title
add_action( 'hero-info', 'genesis_do_post_title' );
// Add page info
add_action( 'hero-info', 'genesis_post_info', 12 );
To be able to add the post author info back in the post hero, I've looked up stackoverflow and found a link where the OP was able to fix it by creating a shortcode for it and running it in the hero-info
function author_shortcode() {
global $post;
$author_id=$post->post_author;
the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');
This shortcode [author] is then added into
add_filter( 'genesis_post_info', 'custom_post_info' );
function custom_post_info( $post_info ) {
if ( is_archive() || is_home() ) {
$post_info = __( 'Article by [author] [post_author_posts_link] on [post_date] - [post_comments zero="Leave a Comment" one="1 Comment" more="% Comments" hide_if_off="disabled"]', 'tcguy' );
return $post_info;
}
}
This is the result now: http://imgur.com/a/6lX5J
It is shown in the wrong place for some reason. Anybody knows how this can be?
The site can be found here: http://websforlocals.com/business/
Hope I gave enough info, and that someone with the same problem can be helped out.
It is issue in your ShortCode registering php code.
When adding short-code we should not ECHO anything as this way it will not be echoed at the place we want to but at the top of the post content.
So always return the output in short code function, and then echo the short-code function.
Now WordPress have a convention for functions which echo the result and which returns the result, i.e. the_author_meta vs get_the_author_meta (first one which you are using will display/echo the result, however get_ functions will return the values).
We need to use get_the_author_meta instead of the_author_meta in your shortcode registering block and it will solve your displaying location issue.
function author_shortcode() {
global $post;
$author_id=$post->post_author;
return get_the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');

can't get apply_filter to work in wordpress thematic

I am learninig to create templates in wordpress using the thematic framework
In the header file I find:
// Filter provided for altering output of the header opening element
echo ( apply_filters( 'thematic_open_header', '<div id="header">' ) );
// Action hook creating the theme header
thematic_header();
echo ( apply_filters( 'thematic_close_header', '</div><!-- #header-->' ) );
I want to add a wrapper div element around the div id="header".../div created above
I have tried to edit the apply_filter lines like this:
echo ( apply_filters( 'thematic_open_header', '<div id="headerWrapper"><div id="header">' ) );
but it doesn't work, just printout out div id="header"
How can I get it to create the html that I want it to?
Thanks
Filters are used in functions.php or in a plugin like this:
add_filter( 'thematic_open_header', function(){
return '<div id="headerWrapper"><div id="header">';
});
The basic difference between apply_filters and do_action is that the first filters some value and returns something, while add_action will be used to do anything you want with the parameters sent or output something to the browser.
Some articles of interest: [1], [2], [3] and [4].

How to add a button or box below Post Title?

I'm writing a Wordpress plug-in that will add a button (or any HTML element) below the post title (or above if below is not possible). How can I do that?
You want to use the filter responsable for printing titles. I'm not sure exactly which one it is, but here's the function (it's likely single_post_title as demonstrated):
add_filter( 'single_post_title', 'the_post_title', 10, 2 );
function my_more_link( $post_title ) {
$button_code = "your_button_HTML_here";
return str_replace( $post_title, $post_title . $button_code );
}

Resources