2

I would like to list the specified pages with my shortcode, but the problems are with the '$' charachters.

function get_page_func( $atts )
    {
        extract(shortcode_atts( array(
                'title' => ''
        ), $atts ) );
        $page = get_page_by_title($title);

        $the_query = new WP_Query( 'page_id='.$page->ID );

        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                $html = '<div class="row page-waypoint" id="'.$page->post_name.'"><div class="col-md-12"><h2>'.get_the_title().'</h2></div>'.get_the_content().'</div>';

            }
        }

        return do_shortcode($html);

        wp_reset_postdata();
    }

    add_shortcode( 'get_page', 'get_page_func' );

If I use [get_page title="Questions&Answers"], the shortcode doesn't list the content of the Questions&Answers page, but lists every blog posts. Do I use the wp_reset_postdata(); function properly?

Where is the problem?

  • can you check if you get the page from title or not, try printing the $page variable before, $the_query = new WP_Query( 'page_id='.$page->ID );. because if there is no value for page_id, you'll get all the posts. – Kumar Mar 28 '14 at 16:40
  • it's NULL, that's the problem :/ ..but why does it list the posts? – user1452062 Mar 28 '14 at 16:45
  • 1
    It lists the post because you get page_id = '', as argument in WP_Query, which is simply ignored and you get all the posts because of default args. – Kumar Mar 28 '14 at 16:46
  • I understand. Thank you. What do you think how can I use this shortcode with page title or better to use page id as argument? – user1452062 Mar 28 '14 at 16:53
  • you can try using page slug or page id, get_page_by(); function would be of help to you. – Kumar Mar 28 '14 at 16:59
3

Your ampersand is getting encoded, try:

get_page_by_title( html_entity_decode( $title ) );
| improve this answer | |
0

I had to use get_page_by_title( htmlspecialchars( $title ) ); to convert & to &amp;.

get_page_by_title( html_entity_decode( $title ) ); did not work for me

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.