admin管理员组文章数量:1127638
With this code
register_activation_hook( __FILE__, 'rm_to_excerpt' );
function rm_to_excerpt()
{
$args = array(
'post_type' => 'post'
, 'numberposts' => -1
);
$posts = get_posts( $args );
foreach ( $posts as $post )
{
if( '' == $post->post_excerpt )
{
$rm = get_post_meta( $post->ID, 'rank_math_description' ,true);
if( '' != $rm )
{
$po = array();
$po = get_post( $post->ID, 'ARRAY_A' );
$po['post_excerpt'] = $rm;
wp_update_post($po);
}
}
}
}
i was able to use rank math meta description as my post excerpt, i want to display this same meta description at the beginning of my single post, please how can i achieve this?
With this code
register_activation_hook( __FILE__, 'rm_to_excerpt' );
function rm_to_excerpt()
{
$args = array(
'post_type' => 'post'
, 'numberposts' => -1
);
$posts = get_posts( $args );
foreach ( $posts as $post )
{
if( '' == $post->post_excerpt )
{
$rm = get_post_meta( $post->ID, 'rank_math_description' ,true);
if( '' != $rm )
{
$po = array();
$po = get_post( $post->ID, 'ARRAY_A' );
$po['post_excerpt'] = $rm;
wp_update_post($po);
}
}
}
}
i was able to use rank math meta description as my post excerpt, i want to display this same meta description at the beginning of my single post, please how can i achieve this?
Share Improve this question edited Dec 27, 2023 at 16:55 Pat J 12.3k2 gold badges28 silver badges36 bronze badges asked Dec 27, 2023 at 16:25 Solex D-manSolex D-man 151 silver badge5 bronze badges2 Answers
Reset to default 1Option 1
If you want to inject the Rank Math meta description into the post content using the the_content
filter, you can do so by adding a filter to your theme's functions.php
file. Here's how you can achieve that:
function inject_rank_math_description($content) {
if (is_single() && !is_admin()) {
// Check if Rank Math meta description exists
$rank_math_description = get_post_meta(get_the_ID(), 'rank_math_description', true);
if (!empty($rank_math_description)) {
// Output Rank Math meta description before the post content
$rank_math_description = '<div class="rank-math-description">' . esc_html($rank_math_description) . '</div>';
$content = $rank_math_description . $content;
}
}
return $content;
}
add_filter('the_content', 'inject_rank_math_description');
Note: This is very similar to the answer by Pat J, except I've added some things.
This code checks if you are viewing a single post (not in the admin area) and if a Rank Math meta description exists. If both conditions are met, it adds the Rank Math meta description to the beginning of the post content using the the_content
filter.
Make sure to add this code to your theme's functions.php file.
Option 2
You can achieve displaying the Rank Math meta description at the beginning of a single post (and definitely before your featured image) by modifying your theme's single post template. You can use the rank_math_description
meta value you have already fetched and display it before the featured image. Here's how you can do it:
<?php
// Check if Rank Math meta description exists
$rank_math_description = get_post_meta(get_the_ID(), 'rank_math_description', true);
if (!empty($rank_math_description)) {
// Output Rank Math meta description
echo '<div class="rank-math-description">' . esc_html($rank_math_description) . '</div>';
}
// The rest of your single post template code follows here
// This includes the featured image, post title, content, etc.
?>
Make sure to replace the comment // The rest of your single post template code follows here
with your existing code for displaying the single post content, featured image, and other elements as needed. This code checks if the Rank Math meta description exists for the current post and displays it before the featured image if it's available.
Remember to place this modified code in your theme's single post template file where you want the Rank Math meta description to appear.
You can use the the_content
filter to add the metadata to the start of the post:
add_filter( 'the_content', 'wpse_421555_prepend_to_post' );
function wpse_421555_prepend_to_post( $content ) {
if ( is_single() ) {
global $post;
$meta = get_post_meta( $post->ID, 'rank_math_description', true );
if ( ! empty( $meta ) ) {
$content = $meta . $content;
}
}
return $content;
}
References
the_content
filteris_single()
本文标签: excerptDisplay Rank Math Meta description at the beginning of single post
版权声明:本文标题:excerpt - Display Rank Math Meta description at the beginning of single post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736706150a1948688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论