PHP 新手,但我已经了解它的工作原理(我的意思是,我可以编写 PHP 并执行 YouTube 教程中的操作)。不过,我很难将它应用到实际使用中:
我想缩短我需要放在 frontpage.php 上的 php 数量(WordPress,但我听说这个问题不是 WordPress 问题;只是一个 php 问题)。
我将多次调用同一个 php 以每次显示 1 个帖子,只需更改要在 php 中显示的类别 - 所以,现在我有:
lots of php cat=33 lots of php
我想创建一个函数,以便在我的 frontpage.php 上,我只需要写:
whatever cat=33 whatever
或者只是
whatever 33
如果有我的代码有帮助,它是(这有标签,但我在这里和那里同时使用标签和猫):
<?php
$args=array(
'tag' => 'feature-left',
'showposts'=>1,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
$offset = 3;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" itemscope="itemscope" itemtype="http://schema.org/BlogPosting" itemprop="blogPost">
<h4><?php the_excerpt(); ?> </h4>
</a>
<a href="<?php the_permalink() ?>" >
<div class="front-first-article-title" itemprop="headline"><h2 style="color:#00589C; margin-bottom:5px;"><b><?php the_title(); ?></b></h2>
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
最佳答案
在您的主题文件夹中,找到您的 functions.php 文件。
在该文件中,将此代码(来自您的问题)放入函数中,如下所示:
function my_custom_loop($category) {
$args=array(
'tag' => 'feature-left',
// showposts has been replaced, use 'posts_per_page' instead
// 'showposts' =>1,
'posts_per_page' => 1,
// this has been replaced, use 'ignore_sticky_posts'
// 'caller_get_posts' => 1,
'ignore_sticky_posts' => true,
'cat' => $category
);
$my_query = new WP_Query($args);
$offset = 3;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" itemscope="itemscope" itemtype="http://schema.org/BlogPosting" itemprop="blogPost">
<h4><?php the_excerpt(); ?> </h4>
</a>
<a href="<?php the_permalink() ?>" >
<div class="front-first-article-title" itemprop="headline"><h2 style="color:#00589C; margin-bottom:5px;"><b><?php the_title(); ?></b></h2>
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
}
现在,在您的主页文件中,您可以放入 my_custom_loop(33)让它输出您的自定义后循环。
注意
循环中的 HTML 存在一些问题。你不应该把 <div>或 <h2>或 <h4> <a> 中的元素标签。另外,你的 <a>标签没有正确关闭。最后,我建议在您的 <h2> 上使用类/CSS 而不是内联样式,因为它会多次输出到屏幕,多次输出完全相同的内联 CSS 有点傻。
编辑
根据您最近的评论,是的,您可以使该函数也处理标签,并使“数字”成为动态的。请注意,有多种方法可以解决此类问题,但最直接的(给定您现有的功能)是这样的:
/* Note the "default values" for $tag and $offset.
* You can call this function in many ways:
* my_custom_loop(33); just get the categories, with an offset of 3
* my_custom_loop(33, NULL, 5); get the categories, offset of 5
* my_custom_loop(NULL, 'feature-left'); get the tags, offset of 3
* my_custom_loop(NULL, 'feature-left', 5); get the tags, offset of 5
*/
function my_custom_loop($category, $tag = NULL, $offset = 3) {
$args=array(
// showposts has been replaced, use 'posts_per_page' instead
// 'showposts' =>1,
'posts_per_page' => 1,
// this has been replaced, use 'ignore_sticky_posts'
// 'caller_get_posts' => 1,
'ignore_sticky_posts' => true,
);
if ($category) {
$args['cat'] = $category;
}
if ($tag) {
$args['tag'] = 'feature-left';
}
$my_query = new WP_Query($args);
// ... rest of function to output loop
}
关于php - 将 php 放入一个快捷方式的函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34456976/
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin