grottopress/wordpress-posts

查询并显示WordPress中的帖子

v1.0.0 2023-05-31 18:05 UTC

README

查询并显示WordPress中的帖子。

安装

通过composer安装

composer require grottopress/wordpress-posts

使用

<?php
declare (strict_types = 1);

use GrottoPress\WordPress\Posts;

// Instantiate Posts
$posts = new Posts([
    'image' => [
        'size' => 'some-size', //could be array (eg: array(150,150)) or string (eg: 'post-thubnail')
        'margin' => '0 10px 0 0',
    ],
    'excerpt' => [
        'length' => 30, // number of words. use -1 for all
        'after' => [
            'types' => ['category', 'post_tag']
        ]
    ],
    'title' => [
        'position' => 'top' // either 'top' or 'side' of image
        'tag' => 'h1', // 'h2' by default,
        'before' => [
            'types' => ['share_link', 'tweet_link'],
        ],
        'after' => [
            'types' => ['author_name', 'published_date', 'comments_link'],
        ]
    ],
    'wp_query' => [ // WP_Query args
        // See https://codex.wordpress.org/Class_Reference/WP_Query
    ]
]);

// Display posts
echo $posts->render();

您可以在您的主题(或插件)中使用 dist/css 中定义的样式

\add_action('wp_enqueue_scripts', function () {
    $css = \is_rtl() ? 'posts-rtl.css' : 'posts.css';

    \wp_enqueue_style(
        'wordpress-posts',
        \get_stylesheet_directory_uri()."/vendor/grottopress/wordpress-posts/dist/css/$css"
    );
});

参数

以下为参数的完整列表,包括它们的默认值:

$args = [
    'class' => 'small', // Wrapper HTML classes
    'tag' => 'div', // Wrapper HTML tag. Use 'ul' for list posts.
    'layout' => 'stack', // 'stack' or 'grid'
    'text_offset' => 0, // Distance from image side to text (title, excerpt)
    'related_to' => 0, // Post ID. Use this for related posts
    'image' => [
        'size' => '',
        'align' => '', // 'left', 'right' or 'none'
        'margin' => '',
        'link' => true,
    ],
    'excerpt' => [
        'length' => 0, // Number of words. Use -1 for full excerpt, -2 for full content
        'paginate' => 1, // If showing full content, whether or not to paginate.
        'more_text' => \esc_html__('read more'),
        'after' => [
            'types' => [], // Info to display after content/excerpt
            'separator' => '|',
            'before' => '<small class="entry-meta after-content">',
            'after' => '</small>',
        ],
    ],
    'title' => [
        'tag' => 'h2',
        'position' => '', // Relative to image: 'top' or 'side'
        'length' => -1, // Number of words. Use -1 for full length
        'link' => true,
        'before' => [
            'types' => [], // Info to display before title
            'separator' => '|',
            'before' => '<small class="entry-meta before-title">',
            'after' => '</small>',
        ],
        'after' => [
            'types' => [], // Info to display after title
            'separator' => '|',
            'before' => '<small class="entry-meta after-title">',
            'after' => '</small>',
        ],
    ],
    'pagination' => [
        'position' => [], // 'top' and/or 'bottom'
        'key' => '', // URL query key to use for pagination. Defaults to 'pag'.
        'mid_size' => null,
        'end_size' => null,
        'prev_text' => \esc_html__('&larr; Previous'),
        'next_text' => \esc_html__('Next &rarr;'),
    ],
    'wp_query' => [ // WP_Query args
        // See https://codex.wordpress.org/Class_Reference/WP_Query
    ]
]

帖子信息

以下为可以提供给 $args['title']['before']['types']$args['title']['after']['types']$args['excerpt']['after']['types'] 的可能值

  • avatar__<size> 例如:avatar__40
  • updated_agoupdated_ago__actualupdated_ago__difference
  • published_agopublished_ago__actualpublished_ago__difference
  • 作者名称
  • 评论链接
  • 更新日期
  • 更新时间
  • 发布日期
  • 发布时间
  • category_listcategory
  • tag_listpost_tag
  • 编辑链接
  • 删除链接
  • 推文按钮
  • 分享此按钮
  • 分享链接
  • 推文链接
  • 推送链接
  • 领英链接
  • buffer链接
  • digg链接
  • tumblr链接
  • reddit链接
  • blogger链接
  • pocket链接
  • skype链接
  • viber链接
  • whatsapp链接
  • telegram链接
  • vk链接
  • 一个过滤钩子名称。然后应该定义并添加到该过滤器中。函数参数:string $output, int $post_id, string $separator
  • 一个帖子元数据键。这将显示该键的单个元数据值。
  • 一个分类名称。这将显示帖子所属的分类的所有术语列表。

社交媒体图标

如果您想显示社交媒体链接的图标,您需要安装 font awesome v5

开发

使用 composer run test 运行测试。

贡献

  1. 分支它
  2. 切换到 master 分支: git checkout master
  3. 创建您的功能分支: git checkout -b my-new-feature
  4. 进行更改,根据需要更新更改日志和文档。
  5. 提交更改: git commit
  6. 推送到分支: git push origin my-new-feature
  7. GrottoPress:master 分支提交新的 Pull Request