admin管理员组文章数量:1289542
I have a simple WP_Query that displays posts:
<?php
function vertical_posts( $title, $category, $num_of_posts ) { ?>
<section class="vertical-posts">
<h2 class="title"><?php echo $title; ?></h2>
<div class="post-grid">
<?php
$args = array('category_name' => $category, 'posts_per_page' => $num_of_posts);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<article class="post">
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
<img src="<?php the_post_thumbnail_url('landscape-medium-1x'); ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>" loading="lazy">
</a>
<header class="entry-header">
<?php the_title('<h3 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h3>'); ?>
<div class="entry-meta">
<?php
posted_on();
posted_by();
?>
</div>
</header>
</article>
<?php
}
}
else {
echo '<p>Please add some posts.</p>';
}
wp_reset_postdata();
?>
</div>
</section>
<?php
}
I'm calling this throughout my theme like so:
<?php vertical_posts( 'Answers', 'answers', 4 ); ?>
I'd like to have the ability to call this function with a shortcode in the block or classic editor.
I've created the shortcode via my functions:
function register_shortcodes(){
add_shortcode('vertical-posts', 'vertical_posts');
}
add_action( 'init', 'register_shortcodes');
Is it possible to pass the function arguments to my shortcode?
I'm thinking something along the lines of:
[vertical-posts 'Answers', 'answers', 4]
I have a simple WP_Query that displays posts:
<?php
function vertical_posts( $title, $category, $num_of_posts ) { ?>
<section class="vertical-posts">
<h2 class="title"><?php echo $title; ?></h2>
<div class="post-grid">
<?php
$args = array('category_name' => $category, 'posts_per_page' => $num_of_posts);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<article class="post">
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
<img src="<?php the_post_thumbnail_url('landscape-medium-1x'); ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>" loading="lazy">
</a>
<header class="entry-header">
<?php the_title('<h3 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h3>'); ?>
<div class="entry-meta">
<?php
posted_on();
posted_by();
?>
</div>
</header>
</article>
<?php
}
}
else {
echo '<p>Please add some posts.</p>';
}
wp_reset_postdata();
?>
</div>
</section>
<?php
}
I'm calling this throughout my theme like so:
<?php vertical_posts( 'Answers', 'answers', 4 ); ?>
I'd like to have the ability to call this function with a shortcode in the block or classic editor.
I've created the shortcode via my functions:
function register_shortcodes(){
add_shortcode('vertical-posts', 'vertical_posts');
}
add_action( 'init', 'register_shortcodes');
Is it possible to pass the function arguments to my shortcode?
I'm thinking something along the lines of:
[vertical-posts 'Answers', 'answers', 4]
Share
Improve this question
edited Jul 29, 2021 at 9:10
Sam
asked Jul 28, 2021 at 19:08
SamSam
2,1963 gold badges30 silver badges59 bronze badges
2 Answers
Reset to default 0You can absolutely pass arguments to your shortcode, but not in the method you described. Please see the Shortcode API (https://codex.wordpress/Shortcode_API)
Your shortcode with arguments will look more like:
[vertical_posts label=Answers category=answers number=4]
I've created a new function for my shortcode:
<?php
function shortcode_posts( $atts ) {
$sc = shortcode_atts( array(
'title' => 'Answers',
'category' => 'answers',
'num_of_posts' => 4
), $atts );
?>
<section class="vertical-posts">
<h2 class="title"><?php echo $sc['title']; ?></h2>
<div class="post-grid">
<?php
$args = array('category_name' => $sc['category'], 'posts_per_page' => $sc['num_of_posts']);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
?>
<article class="post">
<a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
<img src="<?php the_post_thumbnail_url('landscape-medium-1x'); ?>" alt="<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>" loading="lazy">
</a>
<header class="entry-header">
<?php the_title('<h3 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h3>'); ?>
<div class="entry-meta">
<?php
posted_on();
posted_by();
?>
</div>
</header>
</article>
<?php
}
}
else {
echo '<p>Please add some posts.</p>';
}
wp_reset_postdata();
?>
</div>
</section>
<?php
}
Here's how I register the shortcode:
function register_shortcodes(){
add_shortcode('posts', 'shortcode_posts');
}
add_action( 'init', 'register_shortcodes');
I'd then call the shortcode like so:
[posts title="Answers" category="answers" num_of_posts="4"]
There's a great article from Kinsta which goes into detail about this.
本文标签: Passing function arguments via a shortcode
版权声明:本文标题:Passing function arguments via a shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741414638a2377439.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论