dozan0001 2015-06-08 08:53
浏览 30
已采纳

按页面ID检索* number *页面

Hello all,

Resolved

I am currently trying to develop a plugin for wordpress to output three pages into one page, the reason for this is to create something like below.

How i would want the plugin to output the information

I have looked around and not seen something similar to what i am wanting to do.

Below is my code & output. EDITED BELOW CODE: as according to 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

This continues further down the page another 5/6 times.

I wondering why:

  1. I have multiple entry's
  2. They're not the pages I requested

Could anybody possibly help, fix the probelms?

  • 写回答

2条回答 默认 最新

  • dongzhizhai4070 2015-06-08 11:55
    关注

    Resolved

    What I done:

    1. Removed the second foreach ( creating twice as many duplicates)
    2. Adjusted the global output ( Changed it to a less guessable output )
    3. Changed the posts_per_page ( As its in a foreach it would get looped, there is no need for three, 1 will do. user can then specify how many pages to display.)
    4. Adjusted the placement of the main container divs classed "page-information && page-information-color"

    Please feel free to manipulate below in your own answer for more efficiency or for different adaptions.

    <?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' );
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值