Forums

Link to Last Post By Custom Taxonomy Term (5 posts)

  1. dallasmoore
    Member
    Posted 1 year ago #

    Okay,

    So I need to be able to insert a link into my post, by using shortcode or enabling php in the post editor, that will link to the most recent post with a specified custom term. It is a regular post_type, and the custom taxonomy is 'models'. Just for examples sake, we'll say the term is 'Laptop' and some other terms are 'Computer' 'Notebook' and 'PC'.

    So putting the shortcode/php enabling aside, how do I create a custom loop pulling content with only a specific custom term?

    -Dallas

  2. MichaelH
    Volunteer
    Posted 1 year ago #

    <?php
    $args=array(
      'models' => 'laptop',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  3. dallasmoore
    Member
    Posted 1 year ago #

    Thanks a lot! Any recommendations on turning this into shortcode, where the only variable is the term?

  4. dallasmoore
    Member
    Posted 1 year ago #

    Okay,

    Have the shortcode down. Only problem is, whether I use the shortcode or not, the above code gives me the latest posts of that taxonomy, regardless of what term I enter. Any thoughts?

    Here is the code, shortcode and all:

    function np_coupon_shortcode($atts) {
        extract(shortcode_atts(array(
            "mod" => 'lenovo-thinkpad-coupon-code-2'
            ), $atts));
    $args=array(
      'Models' => '$mod',
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p>Get the latest <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo strip_tags(get_the_term_list( $post->ID, 'Models', '', ', ', ' Coupon Code' )) ?></a>.</p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    
    add_shortcode ('coup','np_coupon_shortcode');

    This is the shortcode I am inserting into the post:

    [coup mod="thinkpad-edge-coupon-code-2"]

    I am using the correct slugs for the terms as well, but I've also tried using the term name to no avail.

  5. dallasmoore
    Member
    Posted 1 year ago #

    Sorry for the triple post, but it is also inserting the output above the post content. Can I get it to output where I insert the shortcode?

Topic Closed

This topic has been closed to new replies.

About this Topic