With the help of the answer given at Place category_description in the meta description (wordpress) I think I've got this almost figured out, but it just doesn't seem to be working - when I view the page source for any page, the meta description is empty:
<meta name="description" content="" />
Here's what I've got:
In functions.php
<?php
if( is_single() || is_page() ) $description = get_the_excerpt();
elseif( is_category() ) $description = category_description();
else $description = "Free French lessons and language tools from Laura K. Lawless, including verb conjugations and bilingual articles to help you improve your reading and listening comprehension.";
$description = substr($description,0,500);
?>
In header
<meta name="description" content="<?= $description ?>" />
Any ideas? TIA!
Try this function, it will return something in most cases.
// functions.php
function blog_description() {
$content = get_queried_object();
if ( is_singular() ) {
$content = !empty( $content->post_excerpt ) ? $content->post_excerpt : ( !empty( $content->post_content ) ? $content->post_content : $content->post_title );
return str_replace( PHP_EOL, ' ', substr( wp_filter_nohtml_kses( $content ), 0, 155 ) );
} elseif ( is_category() ) {
$content = !empty( $content->description ) ? $content->description : get_bloginfo( 'name' ) . ' - ' . $content->name;
return str_replace( PHP_EOL, ' ', substr( wp_filter_nohtml_kses( $content ), 0, 155 ) );
}
return get_bloginfo( 'description' );
}
// header.php
<meta name="description" content="<?php echo blog_description(); ?>" />
Related
I am trying to make custom meta box and display it's value in single post,but code doesn't display value in single post.The value is stored in post at admin side,i want to display that value as front end side as well.Right now i am using twenty seventeen theme.i am using this code for custom meta box in function.php file:
add_action( 'add_meta_boxes', 'm_param_meta_box_add' );
function m_param_meta_box_add() {
add_meta_box( 'm_param_post', 'Box Title', 'm_param_post_meta_box_cb', 'post', 'normal', 'high' );
}
function m_param_post_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
if ( isset( $values['m_meta_description'] ) ) {
$m_meta_description_text = esc_attr( $values['m_meta_description'][0] );
}
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="m_meta_description">Post Description</label></th>
<td><textarea rows="3" cols="50" name="m_meta_description"><?php echo $m_meta_description_text; ?></textarea></td>
</tr>
</table>
<?php
} // close m_param_post_meta_box_cb function
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// Make sure your data is set before trying to save it
if( isset( $_POST['m_meta_description'] ) ) {
update_post_meta( $post_id, 'm_meta_description', wp_kses( $_POST['m_meta_description']) );
}
}
add_action('wp_head', 'add_to_wp_head');
function add_to_wp_head( )
{
if (is_single())
{
global $post;
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo '<meta name="description" content="' . $m_meta_description . '"/>';
}
}
The code that i am trying in single.php is look like:
<?php
add_action('wp_head', 'add_to_wp_head');
function add_to_wp_head( )
{
if (is_single())
{
global $post;
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo '<meta name="description" content="' . $m_meta_description . '"/>';
}
}
?>
Try to insert below code in your "template-parts/post/content" file
<?php
$m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
echo 'meta box value: ' . $m_meta_description;
?>
$meta = get_post_meta($post->ID,'meta-box-text', true);
if($meta != '') {
echo $meta;
}else {
echo "Can't Display The Content";
}
Use this code.
Here meta-box-text is the name attribute.
Don't forget to insert this code in loop
Another way:
$meta_print_value=get_post_meta(get_the_ID(),'meta-box-text',true);
echo($meta_print_value);
Im trying to get the following information on a woocommerce single product page in order to replace the default price style of woocommerce.
When Item is on sale, I want to be able to get the original price and discounted price and echo it inside a dive for further styling. Im trying to achive something like this.
<div id="orig-price">Original Price: <?php echo $price; ?></div>
<div id="sale-price">Sale Price: <?php echo $sale-price; ?></div>
<div id="saved">You saved: <?php $saved=$price-$saleprice; echo $saved; ?></div>
I tried opening price.php but did not find what I am looking for. here is what I get.
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"><?php echo $product->get_price_html(); ?></p>
<meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
<meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
<div><?php echo $sale_price_in_percent; ?></div>
</div>
Am I on the right place? Anyone know how can I access these attributes?
Thanks
Main function for displaying prices is get_price_html() in /woocommerce/includes/abstracts/abstract-wc-product.php file, and there are some filters to allow users to customize the html for prices:
woocommerce_sale_price_html
woocommerce_price_html
woocommerce_empty_price_html
woocommerce_free_sale_price_html
woocommerce_free_price_html
woocommerce_get_price_html
This function performs a complete job for showing prices including taxes and on sale calculations, and since you are completely rewriting the html the example below uses last filter there woocommerce_get_price_html. It extracts the prices from the original HTML and insert them into a new HTML structure.
add_filter( 'woocommerce_get_price_html', function( $price, $product )
{
global $woocommerce_loop;
// check if we are in single product page, in main section, and if product has price and is on sale
if ( is_product() && !isset( $woocommerce_loop ) && $product->get_price() && $product->is_on_sale() ) {
// collect prices from $price html string
$prices = array_map( function( $item ) {
return array( $item, (float) preg_replace( "/[^0-9.]/", "", html_entity_decode( $item, ENT_QUOTES, 'UTF-8' ) ) );
}, explode( ' ', strip_tags( $price ) ) );
$price = isset( $prices[0][0] ) ? '<span class="orig-price">Original Price: ' . $prices[0][0] . '</span>' : '';
$price .= isset( $prices[1][0] ) ? '<span class="sale-price">Sale Price: ' . $prices[1][0] . '</span>' : '';
if ( $product->get_regular_price() ) {
// set saved amount with currency symbol placed as defined in options
$price .= '<span class="saved">You saved: ' . sprintf( get_woocommerce_price_format(), get_woocommerce_currency_symbol(), $prices[0][1] - $prices[1][1] ) . '</span>';
}
}
return $price;
}, 10, 2 );
I'm using span tags here instead of divs used in question because the output is still wrapped in a paragraph. For further customization override the price.php template by placing him inside /themes/yourtheme/woocommerce/single-product/ folder.
This can be achieved by overriding woocommerce template. Create a new PHP file, write your div structure in that file.
Now refer this article on How to override template in WooCommerce
Hope this will move you in right direction
I have a custom theme for an online shop (http://themeforest.net/item/mayashop-a-flexible-responsive-ecommerce-theme/2189918) that comes with multiple content types (besides the default post type) and I need to add some kind of vote system to one of this non-default content types ( specifically to the product content type so users can vote the products they like of my shop).
Is there any plugin that provides this funcionality?
Thanks
There are a lot of plugins that do this , you can just google them up,
But if you want to know how it can be easily (and primitivly) done with the help of custom fields , then here you go :
add_action( 'wp_ajax_nopriv_o99__action', 'o99__ajax_cb' );
add_action( 'wp_ajax_o99__action', 'o99__ajax_cb' );
// this fanction is the ajax callback.
function o99__ajax_cb(){
// Verify the nonce to make sure the request is ours and legael...
if( !isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'o99__nonce' ) ) die( '-1' );
$post_id = isset( $_REQUEST['post_id'] ) ? absint( $_REQUEST['post_id'] ) : 0;
if( ! $post_id ) die( '-1' );
$counter = get_post_meta( $post_id, 'o99_good_post', true );
$counter = absint( $counter );
$cookie = 'o99_post_vote' .$post_id;
if( $counter){
if (!isset($_COOKIE[$cookie]) ) {
$counter++;
}
else {$counter=$counter;}
}
else{
$counter = mt_rand(20,150);
}
update_post_meta( $post_id, 'o99_good_post', $counter ); //hidden field
echo $counter;
die();
}
function o99__good_bad(){
global $post;
// send a simple cookie
$cookie = 'o99_post_vote' .$post_id;
$count = get_post_meta( $post->ID, 'o99_good_post', true ); //hidden field
if (!$count ){
$count = '0';
}
$icon = '<img src="';
$icon .= get_bloginfo('template_url') ; // better using get_stylesheet_uri() now
if (!isset($_COOKIE[$cookie]) ) {
$icon .= '/images/dl-star16.png"/>'; // set up your icons according to case
}
else {
$icon .= '/images/dl-v16.png"/>';
}
?>
<span id="o99__click" title="Click Here to Vote this Post up"><?php echo $icon; ?> click here to vote up
This post was voted <span id="o99__count"><?php echo strip_tags( $count );?>
</span> times</span>
<?php
}
// just injecting the JS into head --
add_action( 'wp_head', 'o99__head' );
function o99__head()
{
if( ! is_singular() ) return;
$post_id = get_queried_object_id();
?>
<script type="text/javascript">
var o99__data = {
action: 'o99__action',
post_id: '<?php echo absint( $post_id ); ?>',
nonce: '<?php echo wp_create_nonce( 'o99__nonce' ); ?>',
}
jQuery(document).ready(function(){
jQuery( '#o99__click' ).click(function(){
jQuery.get(
'<?php echo site_url( 'wp-admin/admin-ajax.php' ); ?>',
o99__data,
function( data ){
if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') != null) {
alert( 'You can only vote once per post!' );
};
if(jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>') == null) {
if( '-1' != data )
{
jQuery( 'span#o99__count' ).html( data );
jQuery.cookie('o99_post_vote<?php echo absint( $post_id ); ?>', 'voted', { expires: 7 });
alert( 'your vote was accepted!' );
};
}
}
);
});
});
</script>
<?php
}
?>
This is a very old function that was hacked a long time ago, it should still work, but maybe need a bit of polish ..
Edit I
some examples of more complex plugins :
http://wordpress.org/extend/plugins/post-ratings/
http://wordpress.org/extend/plugins/post-ratings/screenshots/
http://wordpress.org/extend/plugins/gd-star-rating/
and if you will search the codex you will find much more ..
Those listed above support custom post types .
I have a custom post type in which I have custom taxonomies setup.
I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though.
Here is my code to output a list of the categories the custom post type is filed under:
<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>
How do I exclude a specific category?
Thanks.
You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.
Give this a try:
function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$excluded_terms[] = '' . $term->name . '';
}
}
$excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );
return $before . join( $sep, $excluded_terms ) . $after;
}
and then use it like this:
<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>
I'm new to WordPress and just installed version 3.3.1.
I did some googling regarding this question and found some answers but they were relevant to version 2.7 and were 2-3 years old.
Basically, the wp_title function works fine on every page except my home page where it returns blank and I get no title whatsoever. I could just hard code the title in but I'd rather not do that.
Guilty line of code:
<title><?php wp_title ( '| So Fresh n\' So Clean', true,'right' ); ?></title>
I couldn't find anything regarding this problem happening in 3.3.1 so clearly I've done something wrong.
Here's is what I read from Codex:
If you are using a custom homepage with custom loops and stuff, you
will have an empty wp_title. Here goes a neat hack to add the
description/tagline at the wp_title place on homepage:
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
So use is_front_page() to get the title on homepage, the way it is suggested in above code.
But if you use a static home page, this is the code:
<title><?php bloginfo('name'); ?> » <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
Update
for WordPress versions (>= 4.4)
Try this
function some_name(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'some_name' );
Do this in functions.php and remove 'title' tag from head...
Working off of Amna's answer, I came up with the following code which should display the page title when there is one, followed by the site name.
<?php wp_title(' - ',TRUE,'right'); bloginfo('name'); ?>
Post/Page Outputs: "The Page Title - Site Name"
Home Page Outputs: "Site Name"
Obviously, this can also be swapped to display the site name first.
<?php bloginfo('name'); wp_title(' - '); ?>
Post/Page Outputs: "Site Name - The Page Title"
Home Page Outputs: "Site Name"
This can also be combined with a conditional to display the site description when viewing the home page.
<?php bloginfo('name'); echo ' - '; is_front_page() ? bloginfo('description') : wp_title(''); ?>
Post/Page Outputs: "Site Name - The Page Title"
Home Page Outputs: "Site Name - The Site Description"
For google search on wordpress wp_title empty this is the first result. So I thought that I might share the most elegant solution for this.
In functions.php add a filter for wp_title.
function custom_wp_title( $title, $sep ) {
if ( is_feed() ) {
return $title;
}
global $page, $paged;
// Add the blog name
$title .= get_bloginfo( 'name', 'display' );
// Add the blog description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title .= " $sep $site_description";
}
// Add a page number if necessary:
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) );
}
return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );
The new hack from Codex is as follows:
<title><?php wp_title(''); ?></title>
Then in your "functions.php" from theme file :
add_filter( 'wp_title', 'baw_hack_wp_title_for_home' );
function baw_hack_wp_title_for_home( $title )
{
if( empty( $title ) && ( is_home() || is_front_page() ) ) {
return __( 'Home', 'theme_domain' ) . ' | ' . get_bloginfo( 'description' );
}
return $title;
}
I use this one and it never failed:
function pageTitle($echo){
$title = "";
global $paged;
if (function_exists('is_tag') && is_tag()) {
$title .= single_tag_title(__("Tag Archive for "" , 'circle'),false);
$title .= '" - ';
}
elseif (is_archive()) {
$title .= wp_title('',true);
//$title .= __(' Archive - ' , 'circle');
$title .= __(' - ' , 'circle');
}
elseif (is_search()) {
$title .= __('Search for "' , 'circle') . esc_html(get_search_query()).'" - ';
}
elseif (!(is_404()) && (is_single()) || (is_page())) {
$title .= wp_title('',true);
$title .= ' - ';
}
elseif (is_404()) {
$title .= __('Not Found - ' , 'circle');
}
if (is_home()) {
$title .= get_bloginfo('name');
$title .= ' - ';
$title .= get_bloginfo('description');
}
else {
$title .= get_bloginfo('name');
}
if ($paged>1) {
$title .= ' - page ' . $paged;
}
if ( !$echo ) return $title;
echo $title;
}
Note that there are translation domains in it that you might want to change.
no need. Just add the <? Php wp_head ();?> Code at the end of the header.php
good luck.
I use this method in my WordPress site
//Meta Header
if ( ! function_exists( 'dima_wp_title' ) ) :
function dima_wp_title( $title ) {
if ( is_front_page() ) {
return get_bloginfo( 'name' ) . ' | ' . get_bloginfo( 'description' );
} elseif ( is_feed() ) {
return ' | RSS Feed';
} else {
return trim( $title ) . ' | ' . get_bloginfo( 'name' );
}
}
add_filter( 'wp_title', 'dima_wp_title' );
endif;
Late to the conversation...
But if you want to use the actual title of the page that you are using for the static front page, you can use the following:
if (is_front_page())
{
$title = single_post_title( '', false );
}
Although, in the actual source for wp_title(), there is the following, specificaly disabling this for the static front page:
if ( is_single() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ) {
$title = single_post_title( '', false );
}
I suspect there is good reason for this. So, proceed with caution.
My 2 cents for "misty lake" theme which had no title on home page and added incorrect title on all other pages.
Just removing the following line from header.php solves the issue, since Wordpress now injects the tag by itself:
<title><?php wp_title( '|', true, 'right' ); ?></title>
I consulted the following page – https://make.wordpress.org/themes/2015/08/25/title-tag-support-now-required/
You could also put something like this inside your title tag:
<?php
if (is_front_page()) { ?>
Home | <?php bloginfo('description');
} else {
wp_title('|', 'true', 'right'); bloginfo('description');
} ?>