简体   繁体   English

WordPress的类别缩略图列表插件

[英]Wordpress Category Thumbnail List Plugin

I'm trying to tweak a Wordpress Plugin - Category Thumbnail List to display "Coming Soon" when the shortcode calls for posts in a category but there are none. 我正在尝试调整Wordpress插件-类别缩略图列表,以在短代码要求分类中的帖子时显示“即将推出”,但没有。

I've tried contacting the developer but had no luck. 我曾尝试与开发人员联系,但没有运气。

I've modified the original code using the following but it doesn't display. 我使用以下代码修改了原始代码,但未显示。 Any ideas? 有任何想法吗?

foreach($myposts as $post) :
        setup_postdata($post);
        if ( has_post_thumbnail() ) {
        $link = get_permalink($post->ID);
        $thmb = get_the_post_thumbnail($post->ID,'thumbnail');
        $title = get_the_title();
        $output .= '<div class="categoryThumbnailList_item">';
            $output .= '<a href="' .$link . '" title="' .$title . '">' .$thmb . '</a><br/>';
            $output .= '<a href="' .$link . '" title="' .$title . '">' .$title . '</a>';
        $output .= '</div>';
        } else {
                $output .= '<p>Coming Soon</p>';
                }
    endforeach;

Here's the full code: 这是完整的代码:

<?php
/*
Plugin Name: Category Thumbnail List
Plugin URI: http://jonk.pirateboy.net/blog/category/bloggeriet/wordpress/plugins/
Description: Creates a list of thumbnail images, using the_post_thumbnail() in WordPress 2.9 and up.
Version: 1.11
Author: Jonk
Author URI: http://jonk.pirateboy.net
*/
$categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
if ($categoryThumbnailList_Order == '') {
    $categoryThumbnailList_Order = 'date';
}
$categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );
if ($categoryThumbnailList_OrderType == '') {
    $categoryThumbnailList_OrderType = 'DESC';
}

$categoryThumbnailList_Path = get_option('siteurl')."/wp-content/plugins/categoy-thumbnail-list/";

define("categoryThumbnailList_REGEXP", "/\[categorythumbnaillist ([[:print:]]+)\]/");

define("categoryThumbnailList_TARGET", "###CATTHMBLST###");

function categoryThumbnailList_callback($listCatId) {
    global $post;
    global $categoryThumbnailList_Order;
    global $categoryThumbnailList_OrderType;
    $tmp_post = $post; 
    $myposts = get_posts('numberposts=-1&&category='.$listCatId[1].'&&orderby='.$categoryThumbnailList_OrderType.'&&order='.$categoryThumbnailList_Order);
    $output = '<div class="categoryThumbnailList">';
    foreach($myposts as $post) :
        setup_postdata($post);
        if ( has_post_thumbnail() ) {
        $link = get_permalink($post->ID);
        $thmb = get_the_post_thumbnail($post->ID,'thumbnail');
        $title = get_the_title();
        $output .= '<div class="categoryThumbnailList_item">';
            $output .= '<a href="' .$link . '" title="' .$title . '">' .$thmb . '</a><br/>';
            $output .= '<a href="' .$link . '" title="' .$title . '">' .$title . '</a>';
        $output .= '</div>';
        } else {
                $output .= '<p>Coming Soon</p>';
                }
    endforeach;
    $output .= '</div>';
    $output .= '<div class="categoryThumbnailList_clearer"></div>';
    $post = $tmp_post;
    wp_reset_postdata();
    return ($output);
    $output = '';
}

function categoryThumbnailList($content) {
    return (preg_replace_callback(categoryThumbnailList_REGEXP, 'categoryThumbnailList_callback', $content));
}

function categoryThumbnailList_css() {
    global $categoryThumbnailList_Path;
    echo "
<style type=\"text/css\">
    @import url(\"".$categoryThumbnailList_Path."categoy-thumbnail-list.css\");
</style>
";
}
add_action('wp_head', 'categoryThumbnailList_css');
add_filter('the_content', 'categoryThumbnailList',1);
?>
<?php
add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {
  add_options_page('Category Thumbnail List Options', 'Category Thumbnail List', 'manage_options', 'category-thumbnail-list', 'my_plugin_options');
}

function my_plugin_options() {
    global $categoryThumbnailList_Order;
    global $categoryThumbnailList_OrderType;

    if( $_POST['save_category-thumbnail-list_settings'] ) {
        // update order type
        if( !$_POST['category-thumbnail-list_ordertype'] )
        {
            $_POST['category-thumbnail-list_ordertype'] = 'date';
        }
        update_option('category-thumbnail-list_ordertype', $_POST['category-thumbnail-list_ordertype'] );

        // update order
        if( !$_POST['category-thumbnail-list_order'] )
        {
            $_POST['category-thumbnail-list_order'] = 'DESC';
        }
        update_option('category-thumbnail-list_order', $_POST['category-thumbnail-list_order'] );

        $categoryThumbnailList_Order = stripslashes( get_option( 'category-thumbnail-list_order' ) );
    $categoryThumbnailList_OrderType = stripslashes( get_option( 'category-thumbnail-list_ordertype' ) );

    echo "<div id=\"message\" class=\"updated fade\"><p>Your settings are now updated</p></div>\n";

    }   
    ?>
  <div class="wrap">
    <h2>Category Thumbnail List Settings</h2>
    <form method="post">
        <table class="form-table">
            <tr valign="top">
                <th scope="row">Order by</th>
                <td>
                    <select name="category-thumbnail-list_ordertype" id="category-thumbnail-list_ordertype">
                            <option <?php if ($categoryThumbnailList_OrderType == 'date') { echo 'selected="selected"'; } ?> value="date">Date</option>
                            <option <?php if ($categoryThumbnailList_OrderType == 'title') { echo 'selected="selected"'; } ?> value="title">Title</option>
                    </select>
                </td> 
            </tr>
            <tr valign="top">
                <th scope="row">Display order</th>
                <td>
                    <select name="category-thumbnail-list_order" id="category-thumbnail-list_order">
                            <option <?php if ($categoryThumbnailList_Order == 'DESC') { echo 'selected="selected"'; } ?> value="DESC">Descending (z-a/9-1/2010-2001)</option>
                            <option <?php if ($categoryThumbnailList_Order == 'ASC') { echo 'selected="selected"'; } ?> value="ASC">Ascending (a-z/1-9/2001-2010)</option>
                    </select>
                </td> 
            </tr>
        </table>

        <div class="submit">
            <!--<input type="submit" name="reset_category-thumbnail-list_settings" value="<?php _e('Reset') ?>" />-->
            <input type="submit" name="save_category-thumbnail-list_settings" value="<?php _e('Save Settings') ?>" class="button-primary" />
        </div>
        <div>
            <a href="options-media.php">Update the thumbnail sizes here</a>
        </div>
        <div>
            <a href="plugin-editor.php?file=categoy-thumbnail-list/categoy-thumbnail-list.css&plugin=categoy-thumbnail-list/categoy-thumbnail-list.php">You may need to update your css when changing the thumbnail size</a>
        </div>

    </form>
  </div>
<?php
}
?>

Many Thanks, 非常感谢,

Mike 麦克风

Haven't tested your code, but a quick glance shows at least one problem... you're using this outside of the loop. 尚未测试您的代码,但快速浏览至少会看到一个问题……您正在循环外使用此代码。 When not used in the loop, has_post_thumbnail() doesn't know what post you're checking. 在循环中不使用时,has_post_thumbnail()不知道您要检查的帖子。 With just that modification, your code should be this: 只需进行修改,您的代码应为:

foreach($myposts as $post) :
    setup_postdata($post);
    if ( has_post_thumbnail( $post->ID ) ) {
        $link = get_permalink($post->ID);
        $thmb = get_the_post_thumbnail($post->ID,'thumbnail');
        $title = get_the_title();
        $output .= '<div class="categoryThumbnailList_item">';
            $output .= '<a href="' .$link . '" title="' .$title . '">' .$thmb . '</a><br/>';
            $output .= '<a href="' .$link . '" title="' .$title . '">' .$title . '</a>';
        $output .= '</div>';
    } else {
        $output .= '<p>Coming Soon</p>';
    }
endforeach;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM