简体   繁体   English

如何将该PHP代码段转换为Wordpress短代码?

[英]How can I turn this PHP snippet into a Wordpress shortcode?

This is a music website with multiple artist pages - one page per artist. 这是一个音乐网站,其中有多个歌手页面-每个歌手一个页面。 New content is added as a post with a Wordpress tag to denote the artist. 新内容将添加为带有Wordpress标签的帖子,以表示艺术家。 This is so that I can add a Wordpress loop on each artist page to show all the posts filtered with that artist's tag. 这样一来,我可以在每个艺术家页面上添加一个Wordpress循环,以显示所有用该艺术家的标签过滤的帖子。

I've got the filtered loop working correctly, but unfortunately it's currently hardwritten inside the page template's HTML, so it's only filtering for one tag. 我已经使过滤后的循环正常工作,但是不幸的是,该循环当前已在页面模板的HTML中进行了手写,因此只能过滤一个标签。 I don't want to create a new page template for each artist, so I'd like to add it to my functions.php file instead, where I can instead create a new shortcode for each artist. 我不想为每个艺术家创建一个新的页面模板,所以我想将其添加到我的functions.php文件中,而我可以在其中为每个艺术家创建一个新的简码。

Here's the current code in my page template, which filters the loop for all posts with our seefour tag: 这是我的页面模板中的当前代码,该代码使用seefour标记过滤所有帖子的循环:

<?php
query_posts( "tag=seefour" );
if ( have_posts() ) { ?>
  <?php while ( have_posts() ) { ?>
    <?php the_post(); { ?>
    <div class="jd-box">
      <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail( ); ?>
        <div class="jd-overlay"></div>
        <div class="jd-overlay-text">
          <?php the_title(); ?>
        </div>
      </a>
    </div>
<?php } ?>
<?php } ?>
<?php } ?>

I'm assuming the best option is to turn this into a seefour shortcode inside my functions.php file - how can I achieve this? 我假设最好的选择是将其转换为我的functions.php文件中的seefour短代码-如何实现此目的?

Bonus question: is this sustainable in the long run (with 30-50+ artists) or will it cause a lot of redundant code? 额外的问题:从长远来看(30至50名以上的艺术家),这是否可持续?还是会导致大量冗余代码? Open to suggestions... 接受建议...

PS I know this kind of question has been answered already (starting with raw PHP), but since I'm starting with a mix of HTML/PHP (and I'm a PHP newb), I just can't get it to work. PS:我知道这种问题已经得到了解决(从原始PHP开始),但是由于我是从HTML / PHP混合开始(而且我是PHP newb),所以我无法使其正常工作。 So my apologies for asking again. 因此,我很抱歉再次询问。

First of all, you should never ever use query_posts() . 首先,永远不要使用query_posts() It is internal WordPress function to create and maintain the main WordPress cycle. WordPress的内部功能是创建和维护主要的WordPress周期。 Using it, you can crash your site in unpredictable manner. 使用它,您可能会以无法预测的方式使站点崩溃。 You should use get_posts() or WP_Query instead. 您应该改用get_posts()WP_Query

To have your custom shortcode, add the following to your functions.php: 要获得自定义的简码,请将以下内容添加到您的functions.php中:

function showtag_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'tag' => '', // Default value.
    ), $atts );

    $posts = get_posts( 'tag=' . $atts['tag'] );
    if ( $posts ) {
        $output = '';
        foreach ( $posts as $post ) {
            setup_postdata( $post );
            $output .= '<div class="jd-box">';
            $output .= '<a href="' . get_the_permalink( $post ) . '">';
            $output .= get_the_post_thumbnail( $post );
            $output .= '<div class="jd-overlay"></div>';
            $output .= '<div class="jd-overlay-text">';
            $output .= get_the_title( $post );
            $output .= '</div>';
            $output .= '</a>';
            $output .= '</div>';
        }
    } else {
        $output = 'no data';
    }
    wp_reset_postdata();

    return $output;
}

add_shortcode( 'showtag', 'showtag_shortcode' );

This function created [showtag] shortcode with one parameter: tag . 此函数使用一个参数创建了[showtag]tagtag You can use this shortcode on any page as follows: 您可以在任何页面上使用此短代码,如下所示:

[showtag tag="seefour"]
[showtag tag="disco"]

etc. You will have posts with relevant tags to be shown in place of your shortcode. 等等。您将看到带有相关标签的帖子,以代替您的短代码。

Putting a whole loop in a shortcode will make it messy, I know you can use shortcodes in Widgets etc as well but I guess that's not what you're after. 将整个循环放在一个简码中会使它变得混乱,我知道您也可以在Widgets等中使用简码,但是我想那不是您想要的。

If so, the best option would be to make this code a page template say Artist and pass a variable in URL ie http://example.com/artist?t=seefour and then use the following code in the page template. 如果是这样,最好的选择是使此代码成为页面模板,例如Artist并在URL中传递一个变量,即http://example.com/artist?t=seefour ,然后在页面模板中使用以下代码。

<?php
/**
 * Template Name: Artist
 */

query_posts( "tag=" . $_GET['t'] );
if ( have_posts() ) {
?>
  <?php while ( have_posts() ) { ?>
    <?php the_post(); { ?>
    <div class="jd-box">
      <a href="<?php the_permalink(); ?>">
        <?php the_post_thumbnail( ); ?>
        <div class="jd-overlay"></div>
        <div class="jd-overlay-text">
          <?php the_title(); ?>
        </div>
      </a>
    </div>
<?php
    }
  }
}
?>

This can be used for any number of artists, totally flexible because it is dependant on a variable that is provided on run time (when accessed). 它可以用于任何数量的艺术家,完全灵活,因为它依赖于运行时(访问时)提供的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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