Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to WP_Query a custom post type. I would like to display the post_title s in a list. Here is my code, it doesn't seem to work:

<?php
add_action( 'init', 'custom_faq_type_mp' );
function custom_faq_type_mp() {
    register_post_type( 'faq_type_mp', array(
        'labels' => array(
            'name' => 'View current FAQs ',
            'singular_name' => 'FAQ',
            'add_new' => 'Add New FAQ',
            'add_new_item' => 'Add New FAQ',
            'edit' => 'Edit',
            'edit_item' => 'Edit FAQ',
            'new_item' => 'New FAQ',
            'view' => 'View',
            'view_item' => 'View FAQ',
            'search_items' => 'Search FAQs',
            'not_found' => 'No FAQs found', 'not_found_in_trash' => 'No Forms found in Trash',
            'parent' => 'Parent Form'
        ),
        'public' => true,
        'menu_position' => 15,
        'supports' => array( 'title', 'editor' ),
        'taxonomies' => array( '' ),
        'menu_icon' =>
        plugins_url( 'images/image.png', FILE ),
        'has_archive' => true
    ) );
} 

// change the new title line to
add_filter( 'enter_title_here', 'change_default_title' );
function change_default_title( $title ){
    $screen = get_current_screen();
    if ( $screen->post_type == 'faq_type_mp' ) {
        return 'Enter New FAQ Here';
    }
} 

// Display the FAQs using a shortcode
add_action( 'init', ' mp_faq_register_shortcodes' );
function mp_faq_register_shortcodes() {
    /* register the [display_faqs] shortcode */

    add_shortcode( 'display_faqs', 'mp_faq_shortcode' );
}

function mp_faq_shortcode() {
    /* Query albums from the database */
    $loop = new WP_Query( array(
        'post_type' => 'faq_type_mp',
        'orderby' => 'title',
        'order' => 'ASC',
        'post_per_page' => -1,
    ) ); 
    /* check if any faqa were returned */
    if ($loop->have_posts() ) {
        /*open an unordered list */
        $output = '';
        / loop through the faqs (the loop) */
        while ($loop->have_posts() ) {
            $loop->the_post();
            /*display the faq question */
            $output .= post_title() ,
        } /*close the unordered list */
        $output .= '';
    } /*if no faqs were found */
    else {
        $output = 'No FAQa have been published.';
    } /*return the faq list */
    return $output;
} 

My error is below the // Display the FAQs using a shortcode comment. Have you got any solutions? Thank-you.

share|improve this question
    
Sorry @Nic , didn't mean to reject your modification. Still getting use to this site. Silly me. –  mattnewbie May 9 '13 at 18:27
2  
This isnt a WP question - this is just bad PHP syntax. /*display the faq question */ $output .= post_title() , will error - why is the comma there? Your whole code is also missing a closing bracket. And why are you nesting functions at all? –  vancoder May 9 '13 at 19:19
    
Fixed the code block for readers. –  t31os May 10 '13 at 9:18

1 Answer 1

up vote 1 down vote accepted

Write code for human beings to read it. If you indent your code, add white space and use good variable names it will be more readable to human beings. The human being you are writing for is usually yourself in 4 to 6 months.

For example, your first function could look like this:

add_action( 'init', 'custom_faq_type_mp' );

function custom_faq_type_mp() {

    register_post_type(

        'faq_type_mp',
        array(
            'labels'  => array(
                'name'          => 'View current FAQs ',
                'singular_name' => 'FAQ',
                'add_new'       => 'Add New FAQ',
                'add_new_item'  => 'Add New FAQ',
                'edit'          => 'Edit',
                'edit_item'     => 'Edit FAQ',
                'new_item'      => 'New FAQ',
                'view'          => 'View',
                'view_item'     => 'View FAQ',
                'search_items'  => 'Search FAQs',
                'parent'        => 'Parent Form'
                'not_found'     => 'No FAQs found',
                'not_found_in_trash' => 'No Forms found in Trash',
            ),
            'public'        => true,
            'menu_position' => 15,
            'supports'      => array( 'title', 'editor' ),
            'taxonomies'    => array( '' ),
            'menu_icon'     => plugins_url( 'images/image.png', FILE ),
            'has_archive'   => true
        )
    );
}

There is no reason to add a comment ending the function because the ending brace is in the same column as the function definition. If you use a text editor that can match braces ( {}, [], (), etc ) it may highlight (or find) the opening brace for you.

Here is another example from your code:

// Display the FAQs using a shortcode
add_action( 'init', ' mp_faq_register_shortcodes' );

function mp_faq_register_shortcodes() {

    /* register the [display_faqs] shortcode */
    add_shortcode( 'display_faqs', 'mp_faq_shortcode' );

With the guts of the function indented, it becomes pretty obvious that the function closing brace is missing. That could be purposeful, but it is usually a syntax error.

After spending a few hundred hours debugging your code, your eyes will be drawn to any code line that does not end in a semicolon (;). Adding white space (blank lines, tabs etc.) can make some syntax errors more obvious. Like the one pointed out in the comments.

// Loop through the faqs (the loop).
while ( $loop->have_posts() ) {

    $loop->the_post();

    // Display the faq question.
    $output .= post_title() ,

}

Variable names can make some code self documenting. Instead of using $output as the report name, use $faq_list. Then your last comment is unnecessary. Here is similar code with two variable name changes ($loop became faqs).

// Query FAQs from the database.
$faqs = new WP_Query(
    array(
        'post_type' => 'faq_type_mp',
        'orderby'   => 'title',
        'order'     => 'ASC',
        'post_per_page' => -1,
    )
);

// Check if any faqs were returned.
if ( $faqs->have_posts() ) {

    // Open an unordered list.
    $faq_list = '<ul class="faq-list">';

    // Loop through the faqs (the loop).
    while ( $faqs->have_posts() ) {

        // Set up post data.
        $loop->the_post();

        // Add the faq question to the list.
        $faq_list .= sprintf( '<li>%s</li>', get_the_title() );

    }

    // Close the unordered list.
    $faq_list .= '</ul>';

} else {
     // No faqs were found.
    $faq_list = 'No FAQs have been published.';
}

return $faq_list;

You might also like to read the WordPress PHP Coding Standards guide and PHP programming guides. Don't be intimidated; Writing readable code is a huge subject with lots of opinions.

share|improve this answer
    
I've read the php coding standards. Thanks. Still a newbie. –  mattnewbie May 11 '13 at 23:06
    
We have all been new. Glad I could help. –  Charles Clarkson May 11 '13 at 23:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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