0

I have a simple html structure like this.

<div class="ideaPost">

    <a href="<?php echo get_permalink($post->ID); ?>">  
        <div class="ideaPost__Info">
            <?php

                $attachments = get_children(array(
                    'post_type' => 'attachment',
                    'post_mime_type' => 'image',
                    'post_parent' => $post->ID
                ));

                if(count($attachments) > 0) {
                    echo '<span class="glyphicon glyphicon-paperclip"></span>';
                }

            ?>
        </div>
        <h3><?php echo the_title(); ?></h3>
        <h4>by </h4>

        <?php echo the_excerpt(); ?>
    </a>
</div>

I'm using this on a number of pages so I want to add it to functions.php and call the function on the page.

How do I add this as a function to generate this structure. I tried this.

function ideas(id){
  echo '<div class="ideaPost">';
  echo '<a href="'. get_permalink(id); .'">';   
  echo '<div class="ideaPost__Info">';

  $attachments = get_children(array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID
  ));

  if(count($attachments) > 0) {
    echo '<span class="glyphicon glyphicon-paperclip"></span>';
  }

  echo '</div>';
  echo '<h3>'.the_title(); .'</h3>';

  echo '<h4>by </h4>';

  echo the_excerpt();

  echo '</a>';
  echo '</div>';

}

and in the page

if(function_exists('idea')){
  idea();
}
1
  • I thought it would be easier to have function that I could call when I needed it. Could you explain what you mean by dynamic page template?
    – user51590
    Commented Jun 23, 2014 at 11:08

1 Answer 1

0

This all boils down to what needs to go into your functions.php and what not. This might just be personal preference here, but I would strongly advice you to not add elements from your loop to a function. Yes, you don't like the repetition. Things like custom pagination functions, add these to your functions.php inside a function and then call that function inside your template.

I would suggest to have a look at other alternatives to tackle this problem. In a case like this, the most useful alternative to use here is to create a content.php type of template that you can call into your page template via get_template_part.

As an example:

Create a file in the root of your theme and call it something like content-custom.php. Add all the above code in this template. You can now just simply call this template where needed in your page templates as follows

<?php get_template_part( 'content', 'custom' ); ?>

From my comment that I deleted now

Just a thought here, I would stay with the structure that you have. I would try to reduce the amount of templates though, try to optimize them rather. One or two conditional statement in a page template can reduce the need for a couple of extra templates. I would also have a look at creating a dynamic page template. Seriously, I would not do what you are trying to do

As I stated above, it seems that you are using quite a lot of page templates on your site, you need to have a look at and see where you can optimize (if possible) a couple of page templates with the use of conditional statements/tags to cut out the need for such a big amount of templates.

I would also look at dynamic page templates here. Actually I call them dynamic page templates, as this involves one page template that can be reused many times. Options on what exactly to display on the page is selectable from a metabox in the page editor screen. You can check this post to see what I mean.

EDIT

I forgot one thing regarding your code, you don't need to echo when you use the_title() or the_excerpt()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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