无法将WP_QUERY类的对象转换为短码内部的INT

时间:2016-06-19 作者:James

我有以下代码,用于按自定义分类法显示下载帖子。我收到错误

类WP\\u Query的对象无法转换为int

当我启用了查询监视器,但在实际代码工作并在正确的分组中显示正确的文件时,我很难准确地找出是哪个位导致了它。

我想知道它是否需要以某种方式简化,但如果有人能帮我发现这个问题,我将非常感激。

提前感谢

function downloads_shortcode( $atts ) {

extract(shortcode_atts(array(
    \'type\' => \'assoc-downloads\',
    \'posts\' => -1,
    \'download_type\' => \'\',
    \'category\' => \'\',
    \'post_id\' => \'\',
), $atts));


$taxonomies = get_object_taxonomies( $type ); ?>
<article class="page type-page status-publish entry"><?php
foreach( $taxonomies as $taxonomy ) :

    $terms = get_terms( $taxonomy, array (\'order\' => \'DESC\', \'orderby\' =>\'title\'));

    foreach( $terms as $term ) :
                $args = array(
                    \'post_type\' => $type,
                    \'posts_per_page\' => $posts,  //show all posts
                    \'order\' => \'ASC\',
                    \'orderby\' => \'title\',
                    \'download_type\' => $download_type,
                    \'category_name\' => $category,
                    \'post_id\' => $post_id,
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => $taxonomy,
                            \'field\' => \'slug\',
                            \'terms\' => $term->slug,
                        )
                    )
                );
                $posts = new WP_Query($args);

                if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();

                    $attachment_id = get_field(\'document\');
                    $url = wp_get_attachment_url( $attachment_id );
                    $title = get_the_title();
                    // part where to get the filesize
                    $filesize = filesize( get_attached_file( $attachment_id ) );
                    $filesize = size_format($filesize, 2); ?>

                    <section class="category-section">
                        <div class="row">
                            <div class="span12">
                                <h1 class="mid-heading"><?php echo $term->name; ?></h1>
                            </div>

                    <?php echo \'<div class="ecd-single"><p><i style="font-size:120%; margin: 0px 6px 0px 3px;opacity:0.5" class="fa fa-file"></i><a href="\'.$url.\'" title="\'.$title.\'" class="ecd-download-file">\' .$title.\' </a> (\'.$filesize.\') <i  style="font-size:120%; margin: 2px 0 0 5px;opacity:0.5" class="fa fa-download"></i> <a href="\'.$url.\'">Download</a></p></div></div></section>\';
                endwhile; endif;
    endforeach;
endforeach;
echo \'</article>\';}

1 个回复
SO网友:James

我在网上看到的其他一些帖子的基础上重写了这个函数,现在有了以下代码,可以正常工作,没有任何错误。

我仍然愿意接受关于优化此代码的最佳方法的任何进一步建议,甚至可以进一步简化。

再次感谢

function downloads_shortcode($atts){

extract(shortcode_atts(array(
    \'download_type\' => \'\'
), $atts));

// get the terms
$terms = get_terms( \'download_type\' );

// no terms?  bail.
if( ! $terms ) return \'\';

$out = \'\';

// loop through the terms
foreach( $terms as $term )
{
    // get videos in each term
    $downloads = get_posts(array(
        \'post_type\' => \'assoc-downloads\',
        \'download_type\' => $download_type,
        \'tax_query\' => array(
            array(
                \'taxonomy\'  => \'download_type\',
                \'field\'     => \'id\',
                \'terms\'     => $term->term_id,
                \'operator\'  => \'IN\'
            )
        )
    ));

    // no downloads? continue!
    if( ! $downloads ) continue;

    $out .= \'<h2>\' . esc_html( $term->name ) . \'</h2>\';
    $out .= \'<ul>\';
    // loop through the download posts
    foreach( $downloads as $d )
    {
        $attachment_id = get_field(\'document\', $d);
        $url = wp_get_attachment_url( $attachment_id );
        $title = get_the_title($d);
        // part where to get the filesize
        $filesize = filesize( get_attached_file( $attachment_id, $d ) );
        $filesize = size_format($filesize, 2);

        $out .= \'<div class="ecd-single"><p><i style="font-size:120%; margin: 0px 6px 0px 3px;opacity:0.5" class="fa fa-file"></i><a href="\'.$url.\'" title="\'.$title.\'" class="ecd-download-file">\' .$title.\' </a> (\'.$filesize.\') <i  style="font-size:120%; margin: 2px 0 0 5px;opacity:0.5" class="fa fa-download"></i> <a href="\'.$url.\'">Download</a></p></div>\';
    }
    $out .= \'</ul>\';
}
return $out;}

相关推荐

我可以将参数传递给Add_ShortCode()函数吗?

正如标题所述,我需要向add_shortcode() 作用换句话说,我传递的那些参数将在的回调函数中使用add_shortcode(). 我该怎么做?请注意,这些与以下结构无关[shortcode 1 2 3] 哪里1, 2, 和3 是用户传递的参数。在我的情况下,参数仅用于编程目的,不应由用户负责。谢谢