2016-07-16 16 views
0

Es gibt eine Handvoll Dinge, die ich gerne mit diesem Shortcode machen würde, an dem ich gerade arbeite. Mein Wissen darüber ist nicht das Beste, aber ich versuche es zu lernen.WordPress - Wie erstelle ich einen Shortcode, der einen CPT ausgibt

/** 
* Recent Project Shortcode 
*/ 
function project_query() { 
    $args = array(
     'posts_per_page' => 1, 
     'post_type'  => 'projects', 
     'order'   => 'ASC', 
    ); 
    $projects_query = new WP_Query($args); 
    if ($projects_query->have_posts()) : 
     // var_dump(the_post_thumbnail_url("full")); exit; 
     $html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">'; 
     while ($projects_query->have_posts()) : 
      $projects_query->the_post(); 
      // Do stuff with each post here 

      $title = get_the_title(); 
      $link = get_the_permalink(); 
      $featured_img = get_the_post_thumbnail_url($post->ID, 'full'); 

      $html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>'; 
     endwhile; 
     $html_out .= '</article>'; 
    else : // No results 
     echo "Nothing to show"; 
    endif; 
    wp_reset_query(); 
    return $html_out; 
} 
add_shortcode('show_project', 'project_query'); 

Es gibt ein paar Probleme hier. Was funktioniert, ist, dass es am Front-End den Projektnamen zieht, was süß ist, und der Button verweist auf die entsprechende Seite.

So würde ich gerne den Shortcode bei der Verwendung aussehen: [show_projects posts_per_page="3" order="ASC"] Ich möchte es "einfach" für den Benutzer machen, um die $args zu ändern. Die zweite Sache, die nicht funktioniert, ist die Hintergrund-URL, die ich versuche zu tun. Gerade jetzt im Frontend wird alles außer dieser Hintergrund-URL ausgegeben.

Antwort

0

Hey Darren das Problem ist, dass Sie die $featured_img Variable erstellen, nachdem Sie es verwenden. Es sollte in while Schleife sein.

Bitte versuchen Sie diesen Code

/** 
* Recent Project Shortcode 
**/ 
function project_query($atts) { 
    $atts = shortcode_atts(
     array(
      'example_attribute' => 'example_value', 
     ), 
     $atts, 'example' 
    ); 

    //if you want to use the attribute you should use $atts['example_attribute'] for example 

    $args = array(
     'posts_per_page' => 1, 
     'post_type'  => 'projects', 
     'order'   => 'ASC', 
    ); 
    $posts = get_posts($args); 
    if (!empty($posts)) : 
     $post = array_shift($posts); 
     $title = get_the_title($post->ID); 
     $link = get_the_permalink($post->ID); 
     $featured_img = get_the_post_thumbnail_url($post->ID, 'full' ); 

     $html_out = '<article class="recent-project" style="background: url(' . $featured_img . ') no-repeat center center; background-size: cover;">'; 
      // Do stuff with each post here 

     $html_out .= '<h5>Latest Project</h5>' . '<h2>' . $title . '</h2>' . '<a class="btn btn-lg btn-tertiary" href="' . $link . '">' . 'Discover' . '</a>'; 
     $html_out .= '</article>'; 
    else : // No results 
     echo "Nothing to show"; 
    endif; 

    return $html_out; 
} 
add_shortcode('show_project', 'project_query'); 
+0

Dies mir der Hintergrund img, süß erhalten können! Wie kann ich die Parameter extrahieren, damit ich sie als Attribute im Shortcode verwenden kann? –

+0

Als erstes sollten Sie den '$ atts'-Parameter zur Shortcode-Funktion hinzufügen, dann können Sie ihn wie folgt verwenden: $ atts ['example_attribute']'. Ich habe den obigen Code bearbeitet, damit Sie ihn einfach verwenden können. –

Verwandte Themen