简体   繁体   中英

Issue with code used to pull data from posts in wordpress

A developer of ours wrote some template code for Wordpress and when this particular template is applied to a wordpress page it essentially takes the wording and presents it on the end-user page in a tidy fashion.

At the minute, the script can only display one 'Press Release' per month, we would like to display multiple per month however cannot do so.

A clear screenshot of the issue can be found here http://i.stack.imgur.com/tQtlD.png

    <?php
/*
Template Name: Press Releases (BETA)
*/
?>
<?php
get_header();
$press_releases = getPressReleases($post->post_content);
?>

<div class="container rayures">
  <?php require('search_bar.php'); ?>
  <ol class="breadcrumb">
    <li><a href="<?php echo get_site_url(); ?>">Home</a></li>
    <li><!--[if lt IE 8]>&nbsp;/&nbsp;<![endif]--><a href="#">News</a></li>
    <li><!--[if lt IE 8]>&nbsp;/&nbsp;<![endif]--><a href="#">Press Releases</a></li>
  </ol>  
  <div class="panel panel-tab panel-color3">
    <div class="panel-heading">
      <h3><span class="icon-fleche"></span>Press Releases</h3>
    </div>
  </div>
  <div class="row">
     <?php if($press_releases){ ?>
        <?php foreach($press_releases as $year => $releases){ ?>
          <!-- Pour chaque annee -->
          <div class="col-sm-12">
             <div id="date<?php echo $year; ?>">
                <div class="panel panel-tab panel-color3">
                  <div class="panel-heading">
                     <h3 class="panel-title">
                        <a data-toggle="collapse" data-parent="#date<?php echo $year; ?>" href="#collapse<?php echo $year; ?>">
                          <strong><?php echo $year; ?></strong>
                          <span class="glyphicon glyphicon-chevron-down pull-right"></span>
                        </a>
                     </h3>
                  </div>
                  <div id="collapse<?php echo $year; ?>" class="panel-collapse collapse ">
                     <div class="panel-body">
                        <ul class="panel-list small-text">
                          <?php foreach($releases as $k => $v){ ?>
                             <li><div class="deco"></div><?php echo $v; ?></li>
                          <?php } ?>
                          <a href="#">
                        </ul>
                     </div>
                  </div>
                </div>
             </div>
          </div>
        <?php } ?>
     <?php }else{ ?>
        <p>No Press Releases</p>
     <?php } ?>
  </div>
</div>
<?php get_footer(); ?>

FYI: Here is what I have defined in functions.php

function getPressReleases(){
    global $wpdb,$post;
    $temp_return = $return = array();
    $month=array('January','February','March','April','May','June','July','August','September','October','November','December','June');
    preg_match_all('|<a href="(.*)">(.*)</a>|', $post->post_content, $output);
    if(!empty($output[2])){
        for($i=0;$i<count($output[2]);$i++){
            $temp = explode(' ',$output[2][$i]);
            for($t=0;$t<count($temp);$t++){
                // Annees
                if(!array_key_exists($temp[1],$temp_return))$temp_return[$temp[1]]=array();
                // Mois
                if(!array_key_exists($temp[0],$temp_return[$temp[1]]))$temp_return[$temp[1]][$temp[0]] = $output[0][$i];
            }
        }
        // On trie le retour
        krsort($temp_return);
        foreach($temp_return as $k => $v){
            for($i=0;$i<count($month);$i++){
                if(!isset($return[$k]))$return[$k]=array();
                //if(!array_key_exists($month[$i],$return[$k]) && array_key_exists($month[$i],$temp_return[$k])){//
                if(!array_key_exists($month[$i],$return[$k])){
                    $return[$k][$month[$i]]=$temp_return[$k][$month[$i]];
                }
            }
        }
        return $return;
    }else{
        return FALSE;
    }
}

Any pointers would be greatly appreciated!

I think you didn't declare the $post as a global variable in your template. Try to declare "global $post;" before this statement $press_releases = getPressReleases($post->post_content);

Hope it will work..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM