按页面ID

时间:2015-06-08 08:53:25

标签: php wordpress wordpress-plugin

大家好

解决

我目前正在尝试为wordpress开发一个插件,将三个页面输出到一个页面,其原因是创建如下所示的内容。

How i would want the plugin to output the information

我环顾四周,没有看到类似于我想做的事情。

以下是我的代码&输出。 以下编辑:根据Pieter Goosen的说法

<?php
function page_list( $atts ) {

        //extracting input parameters (attributes of shortcode)
        shortcode_atts( array(
            'pages' => ''
        ), $atts );

/***************************************************Out***********************************************************/

$i = '';

// Get the page id by the page name

$page_names = explode(",", $atts['pages']);

foreach($page_names as $page_name) {


    $page = get_page_by_title( ''.$page_name.'', OBJECT, 'page' );

        //argument it

        $args = array(
            'post_type' => 'page',
            'posts_per_page' => 3,
            'page_id' => ''.$page->ID.''
        );

        // The Query
        $the_query = new WP_Query( $args );

        // The Loop
        if ( $the_query->have_posts() ) {

            $i .= '<div class="page-information">';

            $i .= '<div class="page-information-color">';

            while ( $the_query->have_posts() ) {

                $the_query->the_post();

                $i .= '<div class="page-information-widget" style="padding-left: 20px;">';

                $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><img class="alignnone size-medium wp-image-81" src="'.wp_get_attachment_url( get_post_thumbnail_id($the_query->ID,'thumbnail') ).'" alt="" width="auto" height="70" /></a></div>';

                $i .= '<div><h4>'.get_the_title().'</h4></div>';

                $i .= '<div> '.wp_trim_words( get_the_excerpt(), 19, '...').' </div>';

                $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><button class="news-widget-button" type="button">More info &#x3e;</button></a></div>';

                $i .= '</div>';


            }

            $i .= '</div>';

            $i .= '</div>';

        } else {

            echo 'No Pages Found! :(';

        }

        /* Restore original Post Data */
        wp_reset_postdata();

}

//returning the output to page
return $i;
}
//instantiate the shortcode
add_shortcode( 'list_pages', 'page_list' );

?>

enter image description here

这继续在页面的另外5/6次继续。

我想知道为什么

  1. 我有多个条目
  2. 他们不是我要求的网页
  3. 任何人都可以提供帮助,解决问题吗?

2 个答案:

答案 0 :(得分:1)

这一行错了:

'page__in' => ''.$page->ID.''
    {li>

    page__inWP_Query

    中不存在
  • 无论''.$page->ID.''是什么,没有人知道。这是给你解释你如何达到这个

以上一行应为

 'page_id' => $page_id->ID,

修改

您的代码非常草率且容易出错,很遗憾地说

  • 永远不要使用extract()。它差不多两年前从核心被删除了非常具体的原因。有关如何在没有extract()

  • 的情况下正确使用属性,请参阅Shortcode API
  • global $out编码真的很糟糕。创建全局变量是邪恶的,应该避免。给你的全局变量容易变量名称如$out甚至是更大的罪恶和罪恶。请记住,如果您使用$out外部环境,则会破坏全局

  • 您的变量名称令人困惑,$page vs $Page。你应该避免这种情况。这在一年的时间里看起来像拼写错误,你知道这会导致什么。由于变量名称的相似性,这可能非常糟糕并且令人沮丧

答案 1 :(得分:0)

<强>解决

我做了什么:

  1. 删除了第二个foreach(创建两倍重复)
  2. 调整全局输出(将其更改为不太可猜测的输出)
  3. 更改了posts_per_page(因为它在foreach中会被循环,不需要三个,1会这样做。用户可以指定要显示的页数。)
  4. 调整主要容器div的分类&#34;页面信息&amp;&amp;页信息色&#34;
  5. 请随意在您自己的答案中操作,以提高效率或进行不同的调整。

    <?php
    function page_list( $atts ) {
    
        //extracting input parameters (attributes of shortcode)
        shortcode_atts( array(
                    'pages' => ''
                ), $atts );
    
        /***************************************************Out***********************************************************/
    
        $i = '';
    
        // Get the page id by the page name
    
        $page_names = explode(",", $atts['pages']);
    
        $i .= '<div class="page-information">';
    
        $i .= '<div class="page-information-color">';
    
        foreach($page_names as $page_name) {
    
    
            $page = get_page_by_title( ''.$page_name.'', OBJECT, 'page' );
    
            //argument it
    
            $args = array(
                'post_type' => 'page',
                'posts_per_page' => 1,
                'page_id' => ''.$page->ID.''
            );
    
            // The Query
            $the_query = new WP_Query( $args );
    
            // The Loop
            if ( $the_query->have_posts() ) {
    
                while ( $the_query->have_posts() ) {
    
                    $the_query->the_post();
    
                    $i .= '<div class="page-information-widget" style="padding-left: 20px;">';
    
                    $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><img class="alignnone size-medium wp-image-81" src="'.wp_get_attachment_url( get_post_thumbnail_id($the_query->ID,'thumbnail') ).'" alt="" width="auto" height="70" /></a></div>';
    
                    $i .= '<div><h4>'.get_the_title().'</h4></div>';
    
                    $i .= '<div> '.wp_trim_words( get_the_excerpt(), 19, '...').' </div>';
    
                    $i .= '<div><a href="'.get_permalink( $the_query->ID).'"><button class="news-widget-button" type="button">More info &#x3e;</button></a></div>';
    
                    $i .= '</div>';
    
    
                }
    
            } else {
    
                echo 'No Pages Found! :(';
    
            }
    
            /* Restore original Post Data */
            wp_reset_postdata();
    
        }
    
        $i .= '</div>';
    
        $i .= '</div>';
    
        //returning the output to page
        return $i;
        }
         //instantiate the shortcode
        add_shortcode( 'list_pages', 'page_list' );
    
        ?>