从首页上的帖子显示小部件

时间:2017-01-27 16:11:18

标签: javascript php jquery wordpress shortcode

在我的每个帖子中,我都将商店中的帖子小部件放入其中。邮件小部件插件php的商店在下面提供。有了这个插件,你只需要在你的post html中放一个简短的短代码。 (商店小部件网站在您生成小部件后为您提供此代码)

您将放入html后的示例短代码 - [show_shopthepost_widget id =“2371382”]

小部件的外观 -

enter image description here

我想要发生的是将帖子小部件显示在帖子标题和摘录下的首页上。我尝试通过添加echo do_shortcode()来做到这一点。但那没用。有没有人有任何解决方案?我很难过,并且会感激任何帮助。

*我会为每个帖子设置不同的小部件ID,因为每个帖子都会有一个不同的商店帖子小部件。

所以我的首页发布预览看起来像这样 - enter image description here

这里是插件代码(我用于小部件的网站还有许多其他购物小部件,如精品店和lookbook,这就是为什么你会看到除了商店之外的其他一些小部件的帖子小部件,这就是我的'与...合作)

<?php
/*
Plugin Name: rewardStyle Widget
Plugin URI: http://www.rewardstyle.com
Description: The rewardStyle plugin allows bloggers to use rewardStyle widgets on their WordPress blog
Author: rewardStyle
Author URI: http://www.rewardstyle.com
Version: 1.53
*/

require 'plugin-updates/plugin-update-checker.php';
$ExampleUpdateChecker = new PluginUpdateChecker_1_5(
    'http://www.rewardstyle.com/assets/info.json',
    __FILE__,
    'rewardstyle-widgets',
    1
);

/**
 * Add these to the KSES 'Allowed Post Tags' global
 * var. Keeps these tags from being removed in the
 * save/update process.
 */
$GLOBALS['allowedposttags']['iframe'] = array(
    'id'           => TRUE,
    'class'        => TRUE,
    'title'        => TRUE,
    'style'        => TRUE,
    'align'        => TRUE,
    'frameborder'  => TRUE,
    'height'       => TRUE,
    'longdesc'     => TRUE,
    'marginheight' => TRUE,
    'marginwidth'  => TRUE,
    'name'         => TRUE,
    'scrolling'    => TRUE,
    'src'          => TRUE,
    'width'        => TRUE
);
$GLOBALS['allowedposttags']['script'] = array(
    'id'    => TRUE,
    'class' => TRUE,
    'src'   => TRUE,
    'type'  => TRUE,
    'name'  => TRUE
);

/**
 * Add these to the Tiny MCE whitelist of acceptable Tags.
 * This keeps the values available when loading the page,
 * and when switching from Visual/Text Tabs
 */
function unfilter_iframe($initArray) {
  if (isset($initArray['extended_valid_elements'])) {
    $initArray['extended_valid_elements'] .= ",+iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]";
  }
  else {
    $initArray['extended_valid_elements'] = "+iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]";
  }
  return $initArray;
}
function unfilter_script($initArray) {
  if (isset($initArray['extended_valid_elements'])) {
    $initArray['extended_valid_elements'] .= ",+script[id|class|src|type|name]";
  }
  else {
    $initArray['extended_valid_elements'] = "+script[id|class|src|type|name]";
  }
  return $initArray;
}
add_filter('tiny_mce_before_init', 'unfilter_iframe');
add_filter('tiny_mce_before_init', 'unfilter_script');

// Add shortcode support to completely bypass the iframe filter
add_shortcode( 'show_rs_widget', 'rs_show_widget');
function rs_show_widget($atts, $content = null) {
    extract(shortcode_atts(array(
        'wid' => '',
        'blog' => '',
        'product_ids' => '',
        'rows' => '',
        'cols' => '',
        'brand' => '',
        'price' => '',
        'hover' => ''
    ), $atts));

$h = $rows * 120;
$w = ($cols * 110) + 50;

$magic_num = 0;
$how_tall = '120';
$prod_box = 'show';
if ($brand == 1) {
    $magic_num++;
}
if ($price == 1) {
    $magic_num++;
}
if ($hover == 1) {
    $magic_num = 0;
    $prod_box = 'hover-info';
}
if ($magic_num == 1) {
    $how_tall = '162';
} else if ($magic_num == 2) {
    $how_tall = '195';
}

$out = "<div style='width: ".$w."px; height: ".$how_tall."px; margin: 0px auto; background:white;'>
           <iframe frameborder='0' width='".$w."px' height='".$how_tall."px' scrolling='no' src='http://currentlyobsessed.me/api/v1/get_widget?wid=".$wid."&blog=".$blog."&product_ids=".$product_ids."&rows=".$rows."&cols=".$cols."&brand=".$brand."&price=".$price."&hover=".$hover."'></iframe>
        </div>";

return $out;
}

function ms_show_widget($atts) {
    extract(shortcode_atts(array(
        'id'       => '0',
        'image_id' => '0',
        'width'    => '0',
        'height'   => '0',
        'adblock'  => 'Disable your ad blocking software to view this content.',
        'enableJs' => 'JavaScript is currently disabled in this browser.  Reactivate it to view this content.'
    ), $atts));

    $out = '<div class="moneyspot-widget" data-widget-id="'.$id.'">
                <script type="text/javascript" language="javascript">
                    !function(d,s,id){
                        var e, p = /^http:/.test(d.location) ? \'http\' : \'https\';
                        if(!d.getElementById(id)) {
                            e     = d.createElement(s);
                            e.id  = id;
                            e.src = p + \'://widgets.rewardstyle.com/js/widget.js\';
                            d.body.appendChild(e);
                        }
                        if(typeof(window.__moneyspot) === \'object\') {
                            if(document.readyState === \'complete\') {
                                window.__moneyspot.init();
                            }
                        }
                    }(document, \'script\', \'moneyspot-script\');
                </script>
                <div class="rs-adblock">
                    <img src="//images.rewardstyle.com/img?v=2.11&ms='.$id.'&aspect" onerror="this.parentNode.innerHTML=\'Turn off your ad blocker to view content\'" />
                    <noscript>'.$enableJs.'</noscript>
                </div>
            </div>';

    return $out;
}
add_shortcode('show_ms_widget', 'ms_show_widget');

function ltk_show_widget($atts) {
    extract(shortcode_atts(array(
        'user_id'    => '0',
        'rows'       => '1',
        'cols'       => '6',
        'show_frame' => 'true',
        'padding'    => '0'
    ), $atts));

    $out = '<div class="ltkwidget-widget" data-rows="'.$rows.'" data-cols="'.$cols.'" data-show-frame="'.$show_frame.'" data-user-id="'.$user_id.'" data-padding="'.$padding.'">
                <script type="text/javascript" language="javascript">
                    !function(d,s,id){
                        var e, p = /^http:/.test(d.location) ? \'http\' : \'https\';
                        if(!d.getElementById(id)) {
                            e     = d.createElement(s);
                            e.id  = id;
                            e.src = p + \'://widgets.rewardstyle.com/js/ltkwidget.js\';
                            d.body.appendChild(e);
                        }
                    }(document, \'script\', \'ltkwidget-script\');
                    if(typeof(window.__ltkwidget) === \'object\'){
                        if (document.readyState === \'complete\') {
                            __ltkwidget.init();
                        }
                    }
                </script>
                <div class="rs-adblock">
                    <img src="//assets.rewardstyle.com/images/search/350.gif" onerror="this.parentNode.innerHTML=\''.$adblock.'\'" />
                    <noscript>'.$enableJs.'</noscript>
                </div>
            </div>';
    return $out;
}
add_shortcode('show_ltk_widget', 'ltk_show_widget');

function ltk_widget_version_two($atts) {
    extract(shortcode_atts(array(
        'app_id'     => '0',
        'user_id'    => '0',
        'rows'       => '1',
        'cols'       => '6',
        'show_frame' => 'true',
        'padding'    => '0'
    ), $atts));
    $out = '<div id="ltkwidget-version-two'.$app_id.'" data-appid="'.$app_id.'" class="ltkwidget-version-two">
                <script>var rsLTKLoadApp="0",rsLTKPassedAppID="'.$app_id.'";</script>
                <script type="text/javascript" src="//widgets-static.rewardstyle.com/widgets2_0/client/pub/ltkwidget/ltkwidget.js"></script>
                <div widget-dashboard-settings="" data-appid="'.$app_id.'" data-userid="'.$user_id.'" data-rows="'.$rows.'" data-cols="'.$cols.'" data-showframe="'.$show_frame.'" data-padding="'.$padding.'">
                    <div class="rs-ltkwidget-container">
                        <div ui-view=""></div>
                    </div>
                </div>
            </div>';
    return $out;
}
add_shortcode('show_ltk_widget_version_two', 'ltk_widget_version_two');

function lookbook_show_widget($atts) {
    extract(shortcode_atts(array(
        'id'    => '0',
        'adblock'  => 'Turn off your ad blocker to view content',
        'enableJs' => 'Turn on your JavaScript to view content'
    ), $atts));

    $out = '<div class="lookbook-widget" data-widget-id="'.$id.'">
                <script type="text/javascript" language="javascript">
                    !function(d,s,id){
                        var e, p = /^http:/.test(d.location) ? \'http\' : \'https\';
                        if(!d.getElementById(id)) {
                            e     = d.createElement(s);
                            e.id  = id;
                            e.src = p + \'://widgets.rewardstyle.com/js/lookbook.js\';
                            d.body.appendChild(e);
                        }
                        if(typeof(window.__lookbook) === \'object\') if(d.readyState === \'complete\') {
                            window.__lookbook.init();
                        }
                    }(document, \'script\', \'lookbook-script\');
                </script>
                <div class="rs-adblock">
                    <img src="//assets.rewardstyle.com/images/search/350.gif" style="width:15px;height:15px;" onerror="this.parentNode.innerHTML=\''.$adblock.'\'" />
                    <noscript>'.$enableJs.'</noscript>
                </div>
            </div>';
    return $out;
}
add_shortcode('show_lookbook_widget', 'lookbook_show_widget');

function shopthepost_show_widget($atts) {
    extract(shortcode_atts(array(
        'id'    => '0',
        'adblock'  => 'Turn off your ad blocker to view content',
        'enableJs' => 'Turn on your JavaScript to view content'
    ), $atts));

    $out = '<div class="shopthepost-widget" data-widget-id="'.$id.'">
                <script type="text/javascript" language="javascript">
                    !function(d,s,id){
                        var e, p = /^http:/.test(d.location) ? \'http\' : \'https\';
                        if(!d.getElementById(id)) {
                            e     = d.createElement(s);
                            e.id  = id;
                            e.src = p + \'://widgets.rewardstyle.com/js/shopthepost.js\';
                            d.body.appendChild(e);
                        }
                        if(typeof window.__stp === \'object\') if(d.readyState === \'complete\') {
                            window.__stp.init();
                        }
                    }(document, \'script\', \'shopthepost-script\');
                </script>
                <div class="rs-adblock">
                    <img src="//assets.rewardstyle.com/images/search/350.gif" style="width:15px;height:15px;" onerror="this.parentNode.innerHTML=\''.$adblock.'\'" />
                    <noscript>'.$enableJs.'</noscript>
                </div>
            </div>';
    return $out;
}
add_shortcode('show_shopthepost_widget', 'shopthepost_show_widget');

function boutique_show_widget($atts) {
    extract(shortcode_atts(array(
        'id'    => '0',
        'adblock'  => 'Turn off your ad blocker to view content',
        'enableJs' => 'Turn on your JavaScript to view content'
    ), $atts));

    $out = '<div class="boutique-widget" data-widget-id="'.$id.'">
                <script type="text/javascript" language="javascript">
                    !function(d,s,id){
                        var e, p = /^http:/.test(d.location) ? \'http\' : \'https\';
                        if(!d.getElementById(id)) {
                            e     = d.createElement(s);
                            e.id  = id;
                            e.src = p + \'://widgets.rewardstyle.com/js/boutique.js\';
                            d.body.appendChild(e);
                        }
                        if(typeof window.__boutique === \'object\') if(d.readyState === \'complete\') {
                            window.__boutique.init();
                        }
                    }(document, \'script\', \'boutique-script\');
                </script>
                <div class="rs-adblock">
                    <img src="//assets.rewardstyle.com/images/search/350.gif" style="width:15px;height:15px;" onerror="this.parentNode.innerHTML=\''.$adblock.'\'" />
                    <noscript>'.$enableJs.'</noscript>
                </div>
            </div>';
    return $out;
}
add_shortcode('show_boutique_widget', 'boutique_show_widget');

add_filter('widget_text', 'do_shortcode');
add_filter('the_excerpt', 'do_shortcode');

?>

然后是我的front-page.php

<?php
/*
 * Template Name:
 */

get_header();
get_template_part ('inc/carousel');

$the_query = new WP_Query( [
    'posts_per_page' => 14,
    'paged' => get_query_var('paged', 1)
] );

if ( $the_query->have_posts() ) { ?>
    <div id="ajax">
    <?php
    $i = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();

        if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
        <article <?php post_class( 'col-sm-12 col-md-12' ); ?>>
            <div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
                </div>
            <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <div class="front-post-info">
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php get_template_part ('front-page-shop'); ?>
                                 <?php get_template_part( 'share-buttons' ); ?>
                <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
                <?php echo do_shortcode("show_shopthepost_widget"); ?>
            </article><?php

        } else { // Small posts ?>
            <article <?php post_class( 'col-sm-6 col-md-4' ); ?>>
                <div class="front-thumbnail-image"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></div>
                <a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php get_template_part ('front-page-shop'); ?>
                                 <?php get_template_part( 'share-buttons' ); ?>
                <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
             <?php echo do_shortcode("show_shopthepost_widget"); ?>
            </article>
            <?php
        }
        $i++;
    }?>
    </div>
    <?php if(get_query_var('paged') < $the_query->max_num_pages) {
       load_more_button();
    }
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
    echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();

更新了front-page.php

<?php
/*
 * Template Name:
 */

get_header();
get_template_part ('inc/carousel');

$the_query = new WP_Query( [
    'posts_per_page' => 14,
    'paged' => get_query_var('paged', 1)
] );

if ( $the_query->have_posts() ) { ?>
    <div id="ajax">
    <?php
    $i = 0;
    while ( $the_query->have_posts() ) { $the_query->the_post();

        if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
        <article <?php post_class( 'col-sm-12 col-md-12' ); ?>>
            <div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
                </div>
            <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
            <div class="front-post-info">
            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php get_template_part ('front-page-shop'); ?>
                                 <?php get_template_part( 'share-buttons' ); ?>
                <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
                <?php echo do_shortcode("show_shopthepost_widget"); ?>
                <?php if (shortcode_exists('show_shopthepost_widget')) {

    // Get the value of the custom field defined in the editor
    $widgetId = get_post_meta(get_the_ID(), 'shortcode_id', true);

    // Check if a value is set
    if (!empty($widgetId)) {

        $shortcode = '[show_shopthepost_widget id="'.$widgetId.'"]';

    } else {

        // or use a default widget
        $shortcode = '[show_shopthepost_widget id="12345_some_id"]';
    }

    do_shortcode($shortcode);
} ?>
            </article><?php

        } else { // Small posts ?>
            <article <?php post_class( 'col-sm-6 col-md-4' ); ?>>
                <div class="front-thumbnail-image"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></div>
                <a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
                <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                <?php get_template_part ('front-page-shop'); ?>
                                 <?php get_template_part( 'share-buttons' ); ?>
                <div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
             <?php if (shortcode_exists('show_shopthepost_widget')) {

    // Get the value of the custom field defined in the editor
    $widgetId = get_post_meta(get_the_ID(), 'shortcode_id', true);

    // Check if a value is set
    if (!empty($widgetId)) {

        $shortcode = '[show_shopthepost_widget id="'.$widgetId.'"]';

    } else {

        // or use a default widget
        $shortcode = '[show_shopthepost_widget id="12345_some_id"]';
    }

    do_shortcode($shortcode);
} ?>
            </article>
            <?php
        }
        $i++;
    }?>
    </div>
    <?php if(get_query_var('paged') < $the_query->max_num_pages) {
       load_more_button();
    }
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
    echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();

2 个答案:

答案 0 :(得分:1)

实际上,do_shortcode()正是您所需要的,除非您需要按原样输入整个短代码而不改变它(即删除[]

这应该显示上面的小部件:

echo do_shortcode('[show_shopthepost_widget id="2371382"]');

此外,我建议将其包装在shortcode_exists()支票中,否则如果插件因任何原因被禁用或不可用,它将在页面上吐出短代码。 请注意,此函数要求在没有[]的情况下传递短代码。

if (shortcode_exists('show_shopthepost_widget')) {

    echo do_shortcode('[show_shopthepost_widget id="2371382"]');
}

如果您遇到WP,Wordpress Stack Exchange网站可能会为您提供更多答案。希望这有帮助!

修改

动态解决方案

如果您需要在每个页面上显示不同的短代码,则可以修改上述代码以使用自定义字段显示与页面关联的任何窗口小部件,或者如果您未设置任何窗口小部件,则显示默认窗口小部件。

在内容编辑器中

为此,您需要使用您要在页面上显示的小部件的ID,在您的网站wp-adminadd a custom field中修改您的网页,并将其命名为像shortcode_id这样明智的东西。

在您的主题中

您需要修改逻辑以使用与当前页面关联的shortcode_id键获取post meta,并创建相应显示的短代码。此外,如果您没有为页面设置任何内容,则可以设置默认小部件。

if (shortcode_exists('show_shopthepost_widget')) {

    // Get the value of the custom field defined in the editor
    $widgetId = get_post_meta(get_the_ID(), 'shortcode_id', true);

    // Check if a value is set
    if (!empty($widgetId)) {

        $shortcode = '[show_shopthepost_widget id="'.$widgetId.'"]';

    } else {

        // or use a default widget
        $shortcode = '[show_shopthepost_widget id="12345_some_id"]';
    }

    echo do_shortcode($shortcode);
}

答案 1 :(得分:0)

do_shortcode必须与

类似

<?php do_shortcode("[show_shopthepost_widget]"); ?> - 您的代码没有[]