Проверьте запрашиваемую ссылку на текстовый шаблон, затем сделайте что-нибудь - PullRequest
0 голосов
/ 18 декабря 2018

Я использую приведенный ниже код для выполнения шорткода видео на моем сайте WordPress, но некоторые страницы уже содержат видео, добавленное вручную, что приведет к дублированию при использовании кода.

Как включить проверку, если на странице уже есть встроенная вставка YouTube или ссылка на видео, и исключить страницы, на которых уже есть видео, вот что я имею ниже:

if (is_single() && in_category(1) ) 
{
 echo '<h4 class="post-title entry-title">Video</h4>' ;
 echo do_shortcode( '[yotuwp type="keyword" id="'.get_post_field( 'post_title', $post_id, 'raw' ).'" player="mode=large" template="mix" column="1" per_page="1"]' );
}

Я хочувключая ссылку на YouTube, проверьте здесь:

 if (is_single() && in_category(1)

Вот что я могу найти Здесь , но при этом сканируется запрошенный URL-адрес вместо содержимого на нем:

<?php
  if (stripos($_SERVER['REQUEST_URI'],'tout') == true && stripos($_SERVER['REQUEST_URI'],'dedans') == true) 
    {echo '<div class="clear"></div><a href="http://www.example.com/cakes/" class="btn"> >> View all Cakes</a>';}
?>

Ответы [ 3 ]

0 голосов
/ 26 декабря 2018

Поскольку у вас уже есть $post_id, я предлагаю вам получить объект Post и сопоставить регулярное выражение для 'youtube' или короткую версию URL 'youtu.be'.Смотрите пример кода:

$post = get_post($post_id);
$content = apply_filters('the_content', $post->post_content);

if (is_single() && in_category(1) && !preg_match('/youtu\.?be/', $content)) {
    echo '<h4 class="post-title entry-title">Video</h4>';
    echo do_shortcode('[yotuwp type="keyword" id="' . get_post_field('post_title', $post_id, 'raw') . '" player="mode=large" template="mix" column="1" per_page="1"]');
}
0 голосов
/ 27 декабря 2018

Я бы порекомендовал вам подключить фильтр the_content для проверки текущего содержимого, если уже есть требуемый шорткод, если нет, добавьте его.

add_filter( 'the_content', 'func_53829055', 10 );
/**
 * Check if the shortcode exists, if not, add it
 *
 * @param string $post_content
 *
 * @return string
 */
function func_53829055( $post_content ) {
    // Add your custom conditions
    if( ! is_single() && ! in_category( 1 ) ) {
         return $post_content;
    }

    // Use a regex or strpos depending on the needs and post_content length/complexity
    if ( false !== strpos( $post_content, '[yotuwp' ) ) {
        // It already has the wanted shortcode, then return the post content
        return $post_content;
    }

    // If add the video before post content
    $before  = '<h4 class="post-title entry-title">Video</h4>';
    $before .= do_shortcode( 'my_shortcode' );
    $before .= '<br />';

    $post_content = $before . $post_content;

    // If add the video after post content
    $post_content .= '<br />';
    $post_content .= '<h4 class="post-title entry-title">Video</h4>';
    $post_content .= do_shortcode( 'my_shortcode' );

    return $post_content;
}

Обратите внимание, что приоритет должен быть ниже, чем11, потому что шорткоды заменяются на 11: add_filter( 'the_content', 'do_shortcode', 11 );

0 голосов
/ 26 декабря 2018

Используйте file_get_contents , например:

if (strpos(file_get_contents($_SERVER['REQUEST_URI']), 'youtube') === false) {
    // you can add your video
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...