mgsisk / webcomic

Comic publishing power for the web. Turn your WordPress-powered site into a comic publishing platform with Webcomic.
http://wordpress.org/plugins/webcomic
GNU General Public License v2.0
110 stars 29 forks source link

Webcomic_list_storylines add additional information #277

Closed ashikai closed 8 years ago

ashikai commented 8 years ago

Hello again!

I'm working on a new (updated and pretty) theme here, and what I'd like to do is add a button to the output of webcomic_list_storylines that links to the first page in that storyline. I know that the storyline cover and title already do that, but I want like a READ NOW button to appear under the description of each storyline. I tried using the $after parameter, but that only adds content after ALL the storylines rather than each one individually. What I'm looking for is something that behaves more like the $feed parameter and adds text after each storyline.

I'm cool with modifying core files or finding a way to hook into the webcomic_list_storylines tag if needed. I've been messing with line 382 of shortcodes.php with no luck. ^^;

(I've attached an image so you can see what I'm talking about) d5fab01a6930a47d3428cffe1349a98288ef0ed2d2b74c2c09 pimgpsh_fullsize_distr

Whatever help you're able to offer would be greatly appreciated! :+1: Thanks!

ashikai commented 8 years ago
<?php
$terms = get_terms('webcomic1_storyline');
   foreach ($terms as $term) {
      $wpq = array ('taxonomy'=>'webcomic1_storyline','term'=>$term->slug);
      $myquery = new WP_Query ($wpq);
      $article_count = $myquery->post_count;
      echo wp_get_attachment_image( $term->image_id, 'thumbnail');
      echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
        echo $term->name;
      echo "</h3>";
      echo $term->description;
      echo '<a href="'.$term->slug.'">Clickme</a>';
   } ?>

This is as far as I've got. I can successfully pull the storyline name, description and slug with this, but I can't seem to target the cover image or the url for the first webcomic (which is what I want to use in place of the slug). It's progress though. Any idea on what calls I could use instead?

mgsisk commented 8 years ago

Hey @ashikai! That's a good start with the get_terms loop, and one of the better ways to do this. You could also modify some core files, but there's actually an argument on webcomic_list_terms that should do what you want: the walker argument. The walker argument lets you provide a custom "walker," which is what actually generates the output for that template tag. The walker itself is just a class with a few methods that extends WordPress' own Walker class.

Here's an example that I think will give you the output you're looking for, or close to it. You can put this just about anywhere (your theme's functions.php file may be good):

if (!class_exists('Walker_WebcomicCustomTerm_List')) {
  /**
   * A cusotmized term walker.
   */
  class Walker_WebcomicCustomTerm_List extends Walker {
    /**
     * What the class handles.
     *
     * @var string
     */
    public $tree_type = 'webcomic_term';

    /**
     * Database fields to use while walking the tree.

     * @var array
     */
    public $db_fields = array (
      'id' => 'term_id',
      'parent' => 'parent'
    );

    /**
     * Start level output.
     *
     * @param string $output Walker output string.
     * @param integer $depth Depth the walker is currently at.
     * @param array $args Arguments passed to the walker.
     */
    public function start_lvl(&$output, $depth = 0, $args = array()) {
      // $lit = 'ul';
      //
      // if ($args['ordered']) {
      //   $lit = 'ol';
      // }
      //
      // if ($args['hierarchical']) {
      //   $output .= '<'.$lit.' class="children">';
      // }
    }

    /**
     * End level output.
     *
     * @param string $output Walker output string.
     * @param integer $depth Depth the walker is currently at.
     * @param array $args Arguments passed to the walker.
     */
    public function end_lvl(&$output, $depth = 0, $args = array()) {
      // $lit = 'ul';
      //
      // if ($args['ordered']) {
      //   $lit = 'ol';
      // }
      //
      // if ($args['hierarchical']) {
      //   $output .= '</'.$lit.'>';
      // }
    }

    /**
     * Start element output.
     *
     * @param string $output Walker output string.
     * @param object $term Current term being handled by the walker.
     * @param integer $depth Depth the walker is currently at.
     * @param array $args Arguments passed to the walker.
     * @uses WebcomicTag::get_relative_webcomic_link()
     */
    public function start_el(&$output, $term, $depth = 0, $args = array(), $current = 0) {
      global $post; $temp_post = $post;

      extract($args, $args['hierarchical'] ? EXTR_SKIP : EXTR_OVERWRITE);

      if (empty($show_image)) {
        $show_image = 'thumbnail';
      }

      $term_collection = preg_replace('/_(storyline|character)$/', '', $term->taxonomy);
      $term_link = WebcomicTag::get_relative_webcomic_link($target, $term->term_id, false, $term->taxonomy, $term_collection);

      $output .= wp_get_attachment_image($term->webcomic_image, $show_image);
      $output .= '<h3>'.$term->name.'</h3>';
      $output .= $term->description;
      $output .= '<a href="'.$term_link.'">Read Now</a>';
    }

    /**
     * End element output.
     *
     * @param string $output Walker output string.
     * @param object $term Current term being handled by the walker.
     * @param integer $depth Depth the walker is currently at.
     * @param array $args Arguments passed to the walker.
     */
    public function end_el(&$output, $term, $depth = 0, $args = array()) {
      // $output .= '</li>';
    }
  }
}

The walker has four methods, but for your purposes the only one that matters is start_el, which generates all of the HTML output (I based it off your get_terms loop example) and can be modified to suit your needs. I left the other three there in case you wanted to use them for something, but they're mostly for actual list output (e.g. spitting out the opening and closing list tags, as well as the closing list item tag).

Once you have that somewhere, you should be able to use it like this:

<?php webcomic_list_storylines(array('walker' => 'Walker_WebcomicCustomTerm_List')); ?>
ashikai commented 8 years ago

WALKERS! YES. Right, well I feel a bit sheepish; that should have occurred to me straight out. Guess I like doing shit the hard way. T^T

Thanks for all the help! <3 (I still can't believe I didn't think of this... orz)

ashikai commented 8 years ago

Hmm I've been playing with this walker, and something isn't quite right. The walker extension either doesn't display, or throws Fatal error: Using $this when not in object context in /public_html/wp-includes/class-wp-walker.php on line 199

Nooooooo idea why I can't get it to work and i'm brain dead so I went through your walker code and the /-/php/tags.php walkers to modify my original code snippet:

<?php               
$terms = get_terms('webcomic1_storyline');
   foreach ($terms as $term) {
      $wpq = array ('taxonomy'=>'webcomic1_storyline','term'=>$term->slug);
      $myquery = new WP_Query ($wpq);
      $article_count = $myquery->post_count;    
      echo wp_get_attachment_image( $term->webcomic_image, 'thumbnail' );
      echo "<h3>";
        echo $term->name;
      echo "</h3>";
      echo $term->description;
                $term_collection = preg_replace('/_(storyline|character)$/', '', $term->taxonomy);
        $term_link = WebcomicTag::get_relative_webcomic_link($target, $term->term_id, false, $term->taxonomy, $term_collection);
      echo '<a href="'.$term_link.'">Clickme</a>';
   }
?>

This works 100% so I think I'm just gonna do this for now. Thanks so much for your help! ^___^ [edit] I forgot to add a line! All fixed now.