Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 自定义SEO Yoast的元标签_Php_Wordpress_Seo_Meta Tags - Fatal编程技术网

Php 自定义SEO Yoast的元标签

Php 自定义SEO Yoast的元标签,php,wordpress,seo,meta-tags,Php,Wordpress,Seo,Meta Tags,我正在使用woocommerce和seo yoast。我想在产品详细信息页面显示自定义分类seo详细信息,如果产品seo没有完成 以下是我正在尝试的: function custom_metas() { if(is_product()){ global $post; $prod_meta_title = get_post_meta($post->ID, '_yoast_wpseo_title', true); $prod_meta_d

我正在使用woocommerce和seo yoast。我想在产品详细信息页面显示自定义分类seo详细信息,如果产品seo没有完成

以下是我正在尝试的:

function custom_metas()
{
    if(is_product()){
        global $post;
        $prod_meta_title = get_post_meta($post->ID, '_yoast_wpseo_title', true);
        $prod_meta_desc = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
        $prod_meta_kw = get_post_meta($post->ID, '_yoast_wpseo_focuskw', true);

        if(empty($prod_meta_desc) && empty($prod_meta_title) && empty($prod_meta_kw))
        {
            $terms = get_the_terms( $post->ID, 'brand' );
            $meta   = get_option( 'wpseo_taxonomy_meta' );
            $terms = array_reverse($terms);
            if(!empty($terms))
            {
                $set_meta = 0;
                foreach($terms as $term)
                {
                    if($set_meta == 1)
                    {
                        break;
                    }
                    $term_meta_title =  $meta["brand"][$term->term_id]['wpseo_title'];
                    $term_meta_desc =  $meta["brand"][$term->term_id]['wpseo_desc'];
                    $term_meta_keywords =  $meta["brand"][$term->term_id]['wpseo_focuskw'];
                    if(!empty($term_meta_title) || !empty($term_meta_desc) || !empty($term_meta_keywords))
                    {
                        echo "coming";
                        //wpseo_replace_vars( $term_meta_title, get_post( $post_id, ARRAY_A ) )
                        apply_filters( 'wpseo_title', "tseting");
                        apply_filters( 'wpseo_metadesc', trim( $term_meta_desc ) );
                        apply_filters( 'wpseo_metakey', trim( $term_meta_keywords ) );
                        $set_meta = 1;
                    }
                }
            }
        }
    }
}
add_action("wp_head","custom_metas");
我的自定义分类法是brand。问题是seo细节并没有覆盖现有的细节

我怎样才能做到这一点?

我找到了解决办法

在single.php文件中,我在调用头之前使用了filter函数

这是我的single.php

    <?php 

    if ( ! defined( 'ABSPATH' ) ) {
      exit; // Exit if accessed directly
    }
    global $post, $product;
    if(is_product()){
        global $post;
        $prod_meta_title = get_post_meta($post->ID, '_yoast_wpseo_title', true);
        $prod_meta_desc = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
        $prod_meta_kw = get_post_meta($post->ID, '_yoast_wpseo_focuskw', true);

        if(empty($prod_meta_desc) && empty($prod_meta_title) && empty($prod_meta_kw))
        {
            add_filter('wpseo_metadesc', 'custom_metadesc', 100,1);
            add_filter('wpseo_metakey', 'custom_metakeywords', 100,1);
        }
    }
    get_header('shop'); ?>

尝试玩动作优先?谢谢你的回复NerijusMasikonis。你的意思是说使用添加操作代替应用过滤器?不,尝试将添加操作更改为添加操作(“wp\u head”,“custom\u metas”,99999);那么您确定您的更改是最后执行的?那有用吗?不:(…它不起作用.)还有什么我可以试试的吗?
function custom_metadesc($desc){
    global $post;
    $terms = get_the_terms( $post->ID, 'brand' );
    $meta   = get_option( 'wpseo_taxonomy_meta' );
    $terms = is_array($terms) ? array_reverse($terms) : $terms;
    if(!empty($terms))
    {
        $set_meta = 0;
        foreach($terms as $term)
        {
            if($set_meta == 1)
            {
                break;
            }
            $term_meta_desc =  $meta["brand"][$term->term_id]['wpseo_desc'];
            if(!empty($term_meta_desc))
            {
                $desc = $term_meta_desc;
                $set_meta = 1;
            }
        }
    }
    return trim($desc);
}

function custom_metakeywords($keywords){
    global $post;
    $terms = get_the_terms( $post->ID, 'brand' );
    $meta   = get_option( 'wpseo_taxonomy_meta' );
    $terms = array_reverse($terms);
    if(!empty($terms))
    {
        $set_meta = 0;
        foreach($terms as $term)
        {
            if($set_meta == 1)
            {
                break;
            }
            $term_meta_keywords =  $meta["brand"][$term->term_id]['wpseo_focuskw'];
            if(!empty($term_meta_keywords))
            {
                $keywords = $term_meta_keywords;
                $set_meta = 1;
            }
        }
    }
    return trim($keywords);
}