php [SmartCrawl]通过为MarketPress和WooCommerce产品生成和呈现产品类型OpenGraph数据来补充SmartCrawl OpenGraph
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [SmartCrawl]通过为MarketPress和WooCommerce产品生成和呈现产品类型OpenGraph数据来补充SmartCrawl OpenGraph相关的知识,希望对你有一定的参考价值。
<?php
/*
* Plugin Name: Product OpenGraph Data
* Plugin URI: https://premium.wpmudev.org/project/smartcrawl-wordpress-seo/
* Description: Complement SmartCrawl by generating WooCommerce OpenGraph data for SEO and Social Media
* Author: Lindeni Mahlalela @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/mahlamusa
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Product_OpenGraph_Meta_Data' ) ) {
class Product_OpenGraph_Meta_Data {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new Product_OpenGraph_Meta_Data();
}
return self::$_instance;
}
private function __construct() {
add_action( 'wp_head', array($this, 'add_custom_opengraph_meta') );
}
function add_custom_opengraph_meta(){
global $post;
if ( $post->post_type == 'product' ) {
$excerpt = get_the_excerpt();
if ( function_exists( 'wc_get_product' ) ) {
$product = wc_get_product( $post );
$title = $product->name;
$product_price = $product->get_price();
$excerpt = ( $excerpt != "" ) ? $excerpt : substr( $product->description, 0, 250);
$currency = get_woocommerce_currency();
$stock_status = $product->stock_status;
} else if ( class_exists( 'MP_Product' ) ) {
$product = new MP_Product( $product_id );
$title = $product->post_title;
$product_price = $product->get_price( 'before_coupon' );
$excerpt = ( $excerpt != "" ) ? $excerpt : substr( $product->post_content, 0, 250);
$currency = ( is_multisite() && mp_cart()->is_global ) ? mp_get_network_setting( 'global_currency', 'USD' ) : mp_get_setting( 'currency', 'USD' );
$stock_status = $product->in_stock() ? "instock" : "outofstock";
}
?>
<meta property="og:type" content="product" />
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:description" content="<?php echo esc_html( $excerpt ); ?>" />
<meta property="og:url" content="<?php echo get_the_permalink( $post ); ?>"/>
<meta property="og:site_name" content="<?php echo get_bloginfo( 'name' ); ?>" />
<meta property="product:price:amount" content="<?php echo $product_price ?>" />
<meta property="product:price:currency" content="<?php echo $currency ?>" />
<meta property="og:availability" content="<?php echo $stock_status; ?>" />
<?php }
}
}
add_action( 'plugins_loaded', 'plugin_load_name');
function plugin_load_name(){
$GLOBALS['Product_OpenGraph_Meta_Data'] = Product_OpenGraph_Meta_Data::get_instance();
}
}
以上是关于php [SmartCrawl]通过为MarketPress和WooCommerce产品生成和呈现产品类型OpenGraph数据来补充SmartCrawl OpenGraph的主要内容,如果未能解决你的问题,请参考以下文章
11.4 测试Market
行人重识别多个数据集格式统一为market1501格式
AppStore / Market:迁移现有用户群
Stripe Connect (Market Place) 多次拆分到关联账户
[CSP-S模拟测试]:Market(背包DP)
Android:为 Android Market 等 Endless List 实现进度条和“正在加载...”