Posted: . At: 10:21 AM. This was 6 months ago. Post ID: 18772
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Very nice PHP script to add a meta description to a WordPress post.


Using the post excerpt, this very nice PHP script will add a meta description to a WordPress post.

	<?php
		$string = get_the_excerpt();
		function truncate($string,$length=160,$append="…") {
		$string = trim($string);

		  if(strlen($string) > $length) {
			$string = wordwrap($string, $length);
			$string = explode("\n", $string, 2);
			$string = $string[0] . $append;
		  }

		  return $string;
		}
		$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		if ($url == "www.securitronlinux.com/") {
			printf("<meta name='description' content='%s'>\n", get_bloginfo( 'description' ));
			printf("<meta name='myurl' content='%s.'>", $url);
		} else {
			$stringcut = substr($string,0,160);
			//$stringcut = substr($string, 0, strrpos(substr($string, 0, 180), ' '));
			printf("<meta name='description' content='%s'>\n", $stringcut);
			printf('<link rel="canonical" href="%s">', $url);
		};
	?>

This is a straightforward way to take care of this when using a WordPress blog. On a modern block-based theme this might not work, but on older themes like Astra, this does work. Put this in the header section, in the header.php file. This will work straight away.

	<?php astra_head_top(); ?>
	<meta charset="<?php bloginfo( 'charset' ); ?>">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta name="google-site-verification" content="Qky0241x9MEbP28vh0oKc4-7Dp-06ETsSvIUmpnT9Mw" />
	<!-- Chrome, Firefox OS and Opera -->
	<meta name="theme-color" content="#58abdb">
	<!-- Windows Phone -->
	<meta name="msapplication-navbutton-color" content="#58abdb">
	<!-- iOS Safari -->
	<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">

	<!--<meta name="description" content="Linux help, tips and information.">-->

	<?php
		$string = get_the_excerpt();
		function truncate($string,$length=160,$append="…") {
		$string = trim($string);

		  if(strlen($string) > $length) {
			$string = wordwrap($string, $length);
			$string = explode("\n", $string, 2);
			$string = $string[0] . $append;
		  }

		  return $string;
		}
		$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		if ($url == "www.securitronlinux.com/") {
			printf("<meta name='description' content='%s'>\n", get_bloginfo( 'description' ));
			printf("<meta name='myurl' content='%s.'>", $url);
		} else {
			$stringcut = substr($string,0,160);
			//$stringcut = substr($string, 0, strrpos(substr($string, 0, 180), ' '));
			printf("<meta name='description' content='%s'>\n", $stringcut);
			printf('<link rel="canonical" href="%s">', $url);
		};
	?>

	<link rel="profile" href="https://gmpg.org/xfn/11">
	<style type="text/css">
		.relatedthumb {
			background-color: #fff;
		}
		pre {
			color: #000;
		}
		.bash {
			background-color: darkSlateGrey;
		}
	</style>

	<?php wp_head(); ?>
	<?php astra_head_bottom(); ?>

Above is an example of usage in the Astra theme. This is very easy to implement.

Another very nice addition to a WordPress theme. This will print information about the post when placed in the single.php.

	<div id="primary" <?php astra_primary_class(); ?>>

		<?php astra_primary_content_top(); ?>

<p><small>Posted: <?php the_date(); ?>. At: <?php the_time( $d ); ?>. This was <?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'?>. ID: <?php $id = get_the_ID(); echo $id ?>. Page <a href="<?php echo get_permalink($post->ID); ?>">permalink</a>.<br />WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire 2 weeks after they are set.</small></p>

		<?php astra_content_loop(); ?>

I hope you find it very useful.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.