WordPress特色图片的用户定制位置

时间:2017-05-15 作者:Ryan Coolwebs

有没有可能在Wordpress中建立一个选项,允许用户自定义特色图像的位置?

目前,我正在构建的主题是将特色图片作为横幅放置在帖子顶部。让一些请求选项的用户能够将特色图像显示在文章的右上角,并在其周围环绕文本。

不确定如何处理此问题。我的第一个想法是在customiser中添加一个选项,但我担心这将适用于所有博客帖子,而不是针对个人。

另一个想法是在后期编辑屏幕(feat.image框下方)中构建一个元框,然后构建一个函数来钩住wp post。

我在google上搜索过如何做到这一点,但到目前为止,我只能找到有关如何编辑内容的信息。php可以普遍更改/编辑所有特色图像的位置。

1 个回复
SO网友:Ben HartLenn

您正接近第二个猜测,但我认为更好的方法是创建自己的自定义字段复选框,该复选框显示在existing featured image meta box. 从可用性的角度来看,这使得功能变得很好和明显,并且可以根据需要逐篇操作。

我把下面的代码作为一个插件放在一起,但出于您的目的,听起来您想将下面的代码(在我的插件注释下面)添加到您的主题函数中。php文件,因为它的功能似乎更适合您的主题。总之,我总结了以下内容:

<?php
/*
 * Plugin Name: Featured Image Toggle
 * Description: Adds a checkbox to your featured image meta box that you can use to customize how your featured image is displayed
 * Version: 1.0
 * Author: Ben HartLenn
 * Author URI: https://benhartlenn.com
 * License: N/A
 * Text Domain: featured-image-toggle
 */

// Enable featured images in theme, if they are NOT already enabled
if ( !current_theme_supports(\'post-thumbnails\') ) {
    add_theme_support(\'post-thumbnails\');
}

// Add checkbox custom field html to the bottom of the existing featured image meta box in post editor
add_filter(\'admin_post_thumbnail_html\', \'fit_output_featured_image_checkbox\', 10, 2);
function fit_output_featured_image_checkbox($content, $post_id) {
    $field_id = \'toggle_featured_image\';
    $field_value = esc_attr(get_post_meta($post_id, $field_id, true));
    $field_text = esc_html__(\'Show Featured Image in Post Content.\', \'featured-image-toggle\');
    $field_state = checked($field_value, 1, false);

    $field_label = sprintf(
            \'<p><label for="%1$s"><input type="checkbox" name="%1$s" id="%1$s" value="%2$s" %3$s> %4$s</label></p>\', $field_id, $field_value, $field_state, $field_text
    );

    return $content .= $field_label;
}

// Save checkbox custom field data with value as 1 for checked, and 0 for unchecked
add_action(\'save_post\', \'fit_save_featured_image_checkbox\', 10, 3);
function fit_save_featured_image_checkbox($post_ID, $post, $update) {
    $field_id = \'toggle_featured_image\';
    $field_value = isset($_REQUEST[$field_id]) ? 1 : 0;

    update_post_meta($post_ID, $field_id, $field_value);
}

// Prepend featured image to first paragraph of a posts content, if checkbox is checked, i.e. value == 1
add_filter(\'the_content\', \'fit_insert_featured_image\', 20);
function fit_insert_featured_image($content) {
    global $post;
    $featured_post_toggle = get_post_meta($post->ID, \'toggle_featured_image\', true);

    if( has_post_thumbnail($post) && $featured_post_toggle == 1 ) {
        // NOTE: Adding \'medium\' sized image to post content
        $content = preg_replace("/<p>/", "<p>" . get_the_post_thumbnail($post->ID, \'medium\', [\'class\' => \'post-content-featured-image\']), $content, 1);
        return $content;
    }
}

// If our checkbox is checked, also add a little css to float the image to the right, make the image 33% width of parent element, and thus make it responsive.
add_action( \'wp_head\', \'fit_css\');
function fit_css() {
    global $post;
    $featured_post_toggle = get_post_meta($post->ID, \'toggle_featured_image\', true);

    if( is_singular(\'post\') && $featured_post_toggle == 1 ) {
        echo \'<style type="text/css">\';
        // NOTE: Adjust the following css as wanted.
        echo \'img.post-content-featured-image { float: right; width: 33%; height: auto; margin: 0 0 2% 2%; }\';
        echo \'</style>\';
    }
}
听起来您已经有了代码,很可能在主题标题中。php文件,如果设置了该文件,则会显示帖子的特色图像。您需要稍微修改一下,以检查在正常显示特征图像之前是否未选中我们创建的复选框,因此您的新代码可能如下所示:

<?php
  $featured_post_toggle = get_post_meta($post->ID, \'toggle_featured_image\', true);
  // $featured_post_toggle == 0 makes sure our checkbox is not checked
  if ( is_singular(\'post\') && has_post_thumbnail() && $featured_post_toggle == 0 ) :
?>
    <div id="post-image">
      <?php the_post_thumbnail(\'large\', [\'class\' => \'post-featured-image\'] ); ?>
    </div>
<?php endif; ?>
正如我在评论中所指出的,您可能需要调整帖子内容中特色图像的css,您可以拉入different sized featured images 当它们显示在帖子内容中时。

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在