如何改进/缩短PHP代码语法? Wordpress示例

时间:2015-09-24 08:57:52

标签: php wordpress templates

我多次使用browserstack来获取想法和错误。我现在处于好的水平,但我喜欢磨练自己的技能。

以下代码是我对模板所做的自定义编码。它完美地运作。我该如何改进这段代码?这似乎是我渴望实现的目标。如何缩短此代码?

// Display treatments specialism icons that a location is practising

// Fetch all the entries from WP database
function get_specs($location_id) {

    //Register globals
    global $wpdb;

    // The query
    $sql = "SELECT * FROM $wpdb->postmeta";

    // Get the results array from the query
    $results = $wpdb->get_results($sql) or die(mysql_error());

    // Assign the results array and iterate 1 by one
    foreach( $results as $results ) {
    $meta_key = $results->meta_key; 

    // Every time the key value is a 'naam' (name) check wether this is the name of a specialism.
    // The names are not gotten by certain id's because or flexibility (Client add more/change spacialisms later without programming intervention)
    if ($meta_key == "naam") {

    // The name value to be compared
    $specialismen_naam = $results->meta_value;

    // Store the ID connected to the found naam for later use
    $specialismen_id = $results->post_id;

    // Fetch the specialism meta_value array of all the locations, all specialisms practised are in 1 array
    $specialismen = get_post_meta($location_id, 'specialiteiten', true);

    // Search location specialism array and compare it with the name value. if it has a match continue
    if (in_array($specialismen_naam, $specialismen)) {

            // Get the specialism image from the earlyer stored id
            $thumbnail_image = get_post_meta( $specialismen_id, 'thumbnail_image', true );
            // Get the link to the specialism page for if clicked
            $link = get_page_link(get_post_meta( $specialismen_id, 'link', true ));
            //display the specialism icon with link to specialism page
            echo '<li><a href="'. $link .'">'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</a></li>';
            }
        }
    }
}

// Get the Locations 

// Fetch the data
function get_location_fp($object_ids) {

    // register globals
    global $wpdb;

    // Query
    $sql = "SELECT * FROM $wpdb->postmeta WHERE post_id = $object_ids";

    // Results
    $results = $wpdb->get_results($sql) or die(mysql_error());

    foreach( $results as $results ) {
    $meta_key = $results->meta_key; 
    $meta_value = $results->meta_value; 

    // If there is a location thumbnail assigned (has to be), process
    if ($meta_key == "lokatie_thumbnail") {

        // fetch displayed data from event in variable. Telephone, address, name, link to location page
            $telefoonnummer = get_post_meta( $object_ids, 'telefoonnummer', true );
            $adres = get_post_meta( $object_ids, 'adres', true );
            $naam = get_post_meta( $object_ids, 'naam', true );
            $link = get_page_link(get_post_meta( $object_ids, 'link', true ));

        // Fetch thumbnail image of each location
            $thumbnail_image = get_post_meta( $object_ids, 'lokatie_thumbnail', true );

        // Display the location
            echo '<div id="lokatie-fp"><a href="'. $link .'"><div class="lokatie-image">'. wp_get_attachment_image( $thumbnail_image ) .'</div></a><div class="lokatie-gegevens"><h3>'. $naam .'</h3><p>'. $telefoonnummer .'</p><p>'. $adres .'</p>';
        // Add all specialisms that are practised at this location
            echo '<ul>'. get_specs($object_ids) .'</ul></div></div></a>';
        }

    }

}

// Called somewhere in template:
<?php get_location_fp(62);  ?>
<?php get_location_fp(67);  ?>
<?php get_location_fp(105);  ?>
<?php get_location_fp(104);  ?>

2 个答案:

答案 0 :(得分:1)

一致地缩进代码会使读取/调试更加容易

// Display treatments specialism icons that a location is practising

// Fetch all the entries from WP database
function get_specs($location_id) {

    //Register globals
    global $wpdb;

    // The query
    $sql = "SELECT * FROM $wpdb->postmeta";

    // Get the results array from the query
    $results = $wpdb->get_results($sql) or die(mysql_error());

    // Assign the results array and iterate 1 by one
    foreach( $results as $results ) {
        $meta_key = $results->meta_key; 

        // Every time the key value is a 'naam' (name) check wether this is the name of a specialism.
        // The names are not gotten by certain id's because or flexibility (Client add more/change spacialisms later without programming intervention)
        if ($meta_key == "naam") {

            // The name value to be compared
            $specialismen_naam = $results->meta_value;

            // Store the ID connected to the found naam for later use
            $specialismen_id = $results->post_id;

            // Fetch the specialism meta_value array of all the locations, all specialisms practised are in 1 array
            $specialismen = get_post_meta($location_id, 'specialiteiten', true);

            // Search location specialism array and compare it with the name value. if it has a match continue
            if (in_array($specialismen_naam, $specialismen)) {

                // Get the specialism image from the earlyer stored id
                $thumbnail_image = get_post_meta( $specialismen_id, 'thumbnail_image', true );
                // Get the link to the specialism page for if clicked
                $link = get_page_link(get_post_meta( $specialismen_id, 'link', true ));
                //display the specialism icon with link to specialism page
                echo '<li><a href="'. $link .'">'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</a></li>';
            }
        }
    }
}

// Get the Locations 

// Fetch the data
function get_location_fp($object_ids) {

    // register globals
    global $wpdb;

    // Query
    $sql = "SELECT * FROM $wpdb->postmeta WHERE post_id = $object_ids";

    // Results
    $results = $wpdb->get_results($sql) or die(mysql_error());

    foreach( $results as $results ) {
    $meta_key = $results->meta_key; 
    $meta_value = $results->meta_value; 

    // If there is a location thumbnail assigned (has to be), process
    if ($meta_key == "lokatie_thumbnail") {

        // fetch displayed data from event in variable. Telephone, address, name, link to location page
        $telefoonnummer = get_post_meta( $object_ids, 'telefoonnummer', true );
        $adres = get_post_meta( $object_ids, 'adres', true );
        $naam = get_post_meta( $object_ids, 'naam', true );
        $link = get_page_link(get_post_meta( $object_ids, 'link', true ));

        // Fetch thumbnail image of each location
        $thumbnail_image = get_post_meta( $object_ids, 'lokatie_thumbnail', true );

        // Display the location
        echo '<div id="lokatie-fp"><a href="'. $link .'"><div class="lokatie-image">'. wp_get_attachment_image( $thumbnail_image ) .'</div></a><div class="lokatie-gegevens"><h3>'. $naam .'</h3><p>'. $telefoonnummer .'</p><p>'. $adres .'</p>';
        // Add all specialisms that are practised at this location
        echo '<ul>'. get_specs($object_ids) .'</ul></div></div></a>';
    }
}

// Called somewhere in template:
<?php get_location_fp(62);  ?>
<?php get_location_fp(67);  ?>
<?php get_location_fp(105);  ?>
<?php get_location_fp(104);  ?>

如果可以避免使用嵌套的if语句,有时可以更容易阅读,例如:

if ($meta_key != "naam") {
    continue;
}

直接echo输出有时会有点混乱,并且可能导致在不需要时发送输出(例如,如果发生错误)。我很想在渲染之前构建一个输出数组:

$output = array();
$output[] = '<li><a href="'. $link .'">'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</a></li>';
...
...
echo implode(PHP_EOL,$output);

除了那些小事之外,我会说它很好。它非常好,冗长,易于阅读,文档齐全。

至于Jalpa的评论我会说,在不同的行上进行SQL查询和执行它是一种很好的做法,它提高了可读性。 <{1}}之前不需要if条件,它可以自行处理。

修改 你提到你的foreach正在提前呈现。从你的函数get_specs()你当然不想渲染任何东西,但你可以li输出到return调用函数。

get_location_fp()

通过返回function get_specs($loctation_id) { ... $output = array(); ... $output[] = '<li><a href="'. $link .'">'. wp_get_attachment_image( $thumbnail_image, array(30,30) ) .'</a></li>'; ... return implode(PHP_EOL,$output); } function get_location_fp($object_ids) { ... $output[] = '<ul>'. get_specs($object_ids) .'</ul></div></div></a>'; ... } 的输出而不是渲染它,它将被插入get_specs()之间而不是之前。

答案 1 :(得分:0)

我认为您需要添加以下更改:

1)请写这样的$wpdb->get_results("SELECT * FROM $wpdb->postmeta")查询,不需要写两行。

2)请在foreach之前的条件不为空或者count()数组&gt; 0