创建带有自定义查询的WordPress页面模板

One of my main goals with the redesign was to make it easier for visitors to find the information that was most popular on my site. Not to my surprise, posts about MooTools, jQuery, and CSS were at the top of the list. What do those three topics have in common? The fact that I provide a sweet demo for each of them. Thus I set out to create an easy way to check out all of my demos -- the Demos & Downloads page.

重新设计的主要目的之一是使访问者更容易找到我的网站上最受欢迎的信息。 不足为奇的是,有关MooTools,jQuery和CSS的帖子位于列表的顶部。 这三个主题有什么共同点? 我为他们每个人提供了一个很好的演示。 因此,我着手创建一种简单的方法来检出我的所有演示-“演示和下载”页面。

I could address my Demos and Downloads link to "/index.php?s=view+demo" but that's ugly -- not SEO friendly nor is it an address anyone will remember. The solution was to create a new page template that would act similar to a search results page but afford me much more flexibility.

我可以将“演示和下载”链接指向“ /index.php?s=view+demo”,但这很丑陋-不适合SEO,也不是任何人都会记住的地址。 解决方案是创建一个新的页面模板,该模板的作用类似于搜索结果页面,但为我提供了更大的灵活性。

策略 (The Strategy)

The first step was to identify posts that had a demo. If I were starting my blog today I would add a custom field called "demo" and give all posts containing a demo a value of "1". The issue is that I have 2 years worth of posts and no desire to add a custom field to all of those posts that contain demos. I do know, however, that the label of all of my demos is "View Demo" so any post containing that string would likely contain a demo. Problem solved: I will need a search parameter for my custom query that looks for the "view demo" string.

第一步是确定具有演示的帖子。 如果今天开始我的博客,我将添加一个名为“ demo”的自定义字段,并将所有包含演示的帖子的值设置为“ 1”。 问题是我有2年的帖子,不希望向所有包含演示的帖子添加自定义字段。 但是,我确实知道所有演示的标签均为“查看演示”,因此任何包含该字符串的帖子都可能包含一个演示。 解决问题的方法:对于自定义查询,我将需要一个搜索参数来查找“ view demo”字符串。

WP_Query (WP_Query)

WP_Query is WordPress' awesome database interaction class. WP_Query accepts dozens of helpful parameters in the form of a request string or an array to grab the posts you need (writing raw MySQL statements sucks). With my strategy mapped out and WP_Query researched, it was GO time.

WP_Query是WordPress的出色数据库交互类。 WP_Query以请求字符串或数组的形式接受许多有用的参数 ,以获取您需要的帖子(编写原始MySQL语句很烂)。 制定了我的策略并研究了WP_Query之后 ,该走了。

页面模板PHP (The Page Template PHP)


<?php
/*
Template Name: Demos and Downloads
*/

/* helper:  does regex */
function get_content_match($regex,$content) {
	preg_match($regex,$content,$matches);
	return $matches[1];
}

/* list of "view demo" posts */
$paged = (get_query_var('paged')) ? (int) get_query_var('paged') : 1;
$demoPosts = new WP_Query('s=view+demo&showposts=10&order=desc&post_status=publish&paged='.$paged);

?>
<?php get_header(); ?>

<h1>Demos & Downloads</h1>

<?php while ($demoPosts->have_posts()) : $demoPosts->the_post(); ?>
<?php 
	$content = get_the_content();
	if(strstr(strtolower($content),'href="https://davidwalsh.name/demo/')):
?>
	<h1><a href="<?php the_permalink(); ?>"><?php the_title(''); ?></a></h1>
	<p style="padding-bottom:3px;padding-left:5px;">
	<?php
		$intro = get_content_match('/<p>(.*)<\/p>/isU',$content);
		$image = get_content_match('/src="(.*)" class="image"/isU',$content);
		$link = strip_tags(get_content_match('/href="http:\/\/davidwalsh.name\/demo\/(.*)">/isU',$content));
		if($image) { echo '<img src="'.$image.'" class="image" alt="Tutorial Demo" />'; }
		echo $intro;
	?>
	</p>
	<div class="demo-actions">
		<a href="<?php the_permalink(); ?>" class="conred">Continue Reading »</a>
		<a href="https://davidwalsh.name/demo/<?php echo $link; ?>" class="demo">View Demo</a>
	</div>
<?php endif; ?>
<?php endwhile; ?>

<div class="next-prev-links no-print">
	<div class="nav-left">
		<a href="/demos/page/<?php echo $paged + 1; ?>">« Older Posts</a>
	</div>
	<?php if($paged != 1): ?>
	<div class="nav-right">
		<a href="/demos/page/<?php echo $paged - 1; ?>">Newer posts »</a>
	</div>
	<?php endif; ?>
	<div class="clear"></div>
</div>

<!-- <?php trackback_rdf(); ?> -->
<?php get_footer(); ?>


Notice that in order to use pagination within the demos section, I needed to set my own $paged variable and add it to the WP_Query parameters string. The other parameters should be self explanatory.

注意,为了在demos部分中使用分页,我需要设置自己的$ paged变量并将其添加到WP_Query参数字符串中。 其他参数应该可以自我解释。

Once I had the posts for the page, I'd cycle throw each post and use some custom PHP (which you couldn't easily do on the search page) to parse the post content to and pull out a supporting image and link to the demo. The result is my Demos and Downloads page.

找到该页面的帖子后,我将循环抛出每个帖子,并使用一些自定义PHP(您在搜索页面上很难做到这一点)将帖子内容解析为内容,并提取支持图像并链接到演示 结果是我的演示和下载页面。

Beautiful! Don't be afraid to create custom page templates with content generated from your own custom queries. Have any other pages that you think would make the site better? Let me know!

美丽! 不要害怕使用自定义查询生成的内容来创建自定义页面模板。 您是否还有其他页面可以使网站变得更好? 让我知道!

翻译自: https://davidwalsh.name/wp_query

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值