简体   繁体   English

使用WP类别帖子列表小部件动态显示特定类别(WP / PHP)

[英]Using WP Category Posts List Widget To Dynamically Display Specific Category (WP/PHP)

I am trying to use the Category Posts (WP-CPL) plug-in on a blog I'm working on to filter 'Recent Posts' by category. 我正在尝试使用我正在处理的博客上的类别帖子(WP-CPL)插件来按类别过滤“最近的帖子”。 Basically, when someone clicks on the category name on the blog, I would like it to display the posts from that category. 基本上,当有人点击博客上的类别名称时,我希望它显示该类别的帖子。 This would be through the 'archives.php' file of the Life Is Simple template. 这将通过Life Is Simple模板的'archives.php'文件。

The shortcode for the plug-in is: 插件的短代码是:

[wp_cpl_sc cat_id=40 list_num=4 css_theme=2 sticky_post="79"]

This is just an example where 'cat_id' represents the category that the plugin will display. 这只是“cat_id”表示插件将显示的类别的示例。 I don't want to display just one category, I want it to display the appropriate category when someone clicks on the link. 我不想只显示一个类别,我希望它在有人点击链接时显示相应的类别。 How can I get the plug-in to recognize which category is being requested and display the appropriate posts? 如何让插件识别正在请求的类别并显示相应的帖子?

I know that the category title is: 我知道类别标题是:

<?php single_cat_title(); ?>

But how do I find the category ID number in this fashion? 但是,我如何以这种方式找到类别ID号? I've included the PHP for the plug-in's file titled 'wp_cpl_shortcode.php' below if that needs to be edited. 如果需要编辑,我已经将PHP包含在下面标题为'wp_cpl_shortcode.php'的插件文件中。 I would prefer to use shortcode in the actual coding of the site for simplicity's purpose. 为了简单起见,我宁愿在网站的实际编码中使用短代码。

<?php
/**
 * shortcode
 * The library of shortcode class
 * @author Swashata <swashata4u@gmail.com>
 * @subpackage WP Category Post List Plugin
 * @version 2.0.0
 */

/**
 * The WP CPL shorttag support
 * @since 1.1.0
 * This was started from the version 1.1.0 and was finished by 2.0.0
 */
class itgdb_wp_cpl_shortcode {
    /**
     * The wp_cpl_shortcode_handler function
     * This function is responsible for converting shortcodes into dynamic contents
     * @package WordPress
     * @subpackage WordPress Category Post List plugin
     * @since 1.1.0
     * @param array $atts The attributes passed through the shortcode
     * @param string $content The string passed through the shortcode. Used for generating title
     * @return string The modified content
     */
    public function wp_cpl_shortcode_handler($atts, $content = null) {
        /** first extract the attributes */
        $op = shortcode_atts(array(
        'cat_id'            => 1,
            'css_theme'                 => 0,
        'is_thumb'          => 'true',
        'list_num'          => 10,
        'show_comments'     => 'true',
        'sort_using'        => 1,
        'sort_order'        => 'asc',
        'exclude_post'      => '',
        'sticky_post'       => '',
            'show_date'                 => 'true',
            'show_author'               => 'true',
            'show_excerpt'              => 'true',
            'excerpt_length'            => 150,
            'optional_excerpt'          => 'false',
            'read_more'                 => __('Continue Reading', itgdb_wp_cpl_loader::$text_domain),
        ), $atts);

        /** Sanitize some of the user datas */
        $cat_id = (int) $op['cat_id'];
        $i = 0;
        /** Done, now the main thing */
        include_once itgdb_wp_cpl_loader::$abs_path . '/includes/wp_cpl_output_gen.php';
        $output_gen = new itgdb_wp_cpl_output_gen();
        return $output_gen->shortcode_output_gen($op);
    }
}

Sorry if this question is convulated, I'm still learning and think I've twisted my brain around today. 对不起,如果这个问题被卷入,我仍在学习,并认为我今天扭曲了我的大脑。 Thanks for any help! 谢谢你的帮助!


The plug-in page is here: 插件页面在这里:

http://wordpress.org/extend/plugins/wp-category-posts-list/ http://wordpress.org/extend/plugins/wp-category-posts-list/

PS I will also post this in the wordpress.stackexchange.com , I just thought maybe this was a good PHP coding question to ask on this forum as well. PS我也会在wordpress.stackexchange.com上发布这个,我想也许这也是一个很好的PHP编码问题,也可以在这个论坛上提问。


EDIT 编辑

I tried several things. 我尝试了几件事。

Number one: 第一:

<?php $categoryid = get_the_category($post->ID);
echo do_shortcode( '[wp_cpl_sc cat_id=".$categoryid." list_num=4 css_theme=2 sticky_post="79"]'); 
?>

This didn't do anything. 这没有做任何事情。 It just displayed the first four posts. 它只显示了前四个帖子。

Number two (I found a different PHP function in WordPress): 第二个(我在WordPress中找到了不同的PHP函数):

<?php $category_current = get_query_var($cat);
echo do_shortcode('[wp_cpl_sc cat_id="$category_current" list_num=4 css_theme=2 sticky_post="79"]');
?>

I go this idea from here http://www.wpsite.net/how-to-get-category-id-current-category/ . 我从这里开始这个想法http://www.wpsite.net/how-to-get-category-id-current-category/ I also tried it as get_query_var('cat') as it says on the site but that didn't work either. 我也尝试过它作为get_query_var('cat'),就像它在网站上说的那样但是也没有用。 Am I close? 我接近了吗? Is it a slight matter of syntax? 这是语法上的一个小问题吗? Basically just need to grab the current category ID number and pass it into the 'cat_id' part of the shortcode. 基本上只需要获取当前类别ID号并将其传递到短代码的“cat_id”部分。 Thanks. 谢谢。

You can get current category ID by using get_the_category($post->ID); 您可以使用get_the_category($post->ID);获取当前类别ID get_the_category($post->ID); . This will give you the ID of the category the post you're viewing is assigned. 这将为您提供您正在查看的帖子所分类的类别的ID。

How do you display your plugin? 你如何展示你的插件? Do you write your shortcode somewhere in your files? 你在文件的某处写了你的短代码吗? If yes, try do_shortcode . 如果是,请尝试do_shortcode

There's a much easier way to do what I was looking to do (for all those in the future that are looking at this post). 有一种更简单的方法可以做我想做的事情(对于将来看这篇文章的所有人)。 I just dynamically called the different elements from the blog through PHP. 我只是通过PHP动态调用博客中的不同元素。 I set it up first like this: 我首先这样设置:

<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
foreach($myposts as $post) :
setup_postdata($post);
?>

And then the different elements to call are the thumbnail image: <?php the_post_thumbnail(); ?> 然后调用的不同元素是缩略图: <?php the_post_thumbnail(); ?> <?php the_post_thumbnail(); ?>

You can also size the thumbnail like so: <?php set_post_thumbnail_size( 300, 300 ); ?> 您还可以像这样调整缩略图的大小: <?php set_post_thumbnail_size( 300, 300 ); ?> <?php set_post_thumbnail_size( 300, 300 ); ?>

The title: <?php the_title(); ?> 标题: <?php the_title(); ?> <?php the_title(); ?>

And the excerpt: <?php the_excerpt(); ?> 摘录: <?php the_excerpt(); ?> <?php the_excerpt(); ?>

They can be styled by including these elements in divs and styling the divs with sizes thusly. 它们可以通过在div中包含这些元素来设置样式,从而为div设置尺寸。 Much easier than trying to change a plugin that's already hard coded. 比尝试更改已经硬编码的插件容易得多。

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

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